Securely trigger personal scripts from incoming HTTP requests
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watchfifo.sh 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/bash
  2. ################################################################################
  3. #
  4. # This file is part of the package personal-webhooks. It is subject to
  5. # the license terms in the LICENSE file found in the top-level directory
  6. # of this distribution and at:
  7. #
  8. # git://git.devalot.com/personal-webhooks.git
  9. #
  10. # No part of this package, including this file, may be copied, modified,
  11. # propagated, or distributed except according to the terms contained in
  12. # the LICENSE file.
  13. #
  14. ################################################################################
  15. #
  16. # Execute a command for each line read from a FIFO.
  17. #
  18. # For example, to use in conjunction with the download-video.sh script:
  19. #
  20. # watchfifo.sh -f /tmp/download.fifo -- download-video.sh -d ~/Downloads
  21. #
  22. # Lines read from the FIFO are piped into the given command's stdin.
  23. #
  24. ################################################################################
  25. set -e
  26. option_fifo_file=""
  27. option_force=0
  28. option_group=""
  29. ################################################################################
  30. usage () {
  31. cat <<EOF
  32. Usage: watchfifo.sh [options] -- command [arg1, arg2, ...]
  33. -f FILE The FIFO file to create and manage
  34. -F Force. If FIFO file exists, remove it
  35. -g GRP Set the FIFO file's group to GRP
  36. -h This message
  37. EOF
  38. }
  39. ################################################################################
  40. while getopts "f:Fg:h" o; do
  41. case "${o}" in
  42. f) option_fifo_file=$OPTARG
  43. ;;
  44. F) option_force=1
  45. ;;
  46. g) option_group=$OPTARG
  47. ;;
  48. h) usage
  49. exit
  50. ;;
  51. *) exit 1
  52. ;;
  53. esac
  54. done
  55. shift $((OPTIND-1))
  56. ################################################################################
  57. die() {
  58. >&2 echo "ERROR: " "$@"
  59. exit 1
  60. }
  61. ################################################################################
  62. prepare() {
  63. mkfifo -m 0620 "$option_fifo_file"
  64. if [ -n "$option_group" ]; then
  65. chgrp "$option_group" "$option_fifo_file"
  66. fi
  67. }
  68. ################################################################################
  69. cleanup() {
  70. rm -f "$option_fifo_file"
  71. }
  72. ################################################################################
  73. subcommand() {
  74. json=$1
  75. shift
  76. if ! echo "$json" | "$@"; then
  77. >&2 echo "ERROR: child process failed"
  78. fi
  79. }
  80. ################################################################################
  81. # Safety checks.
  82. if [ -z "$option_fifo_file" ]; then
  83. die "you must use -f to specify a file path"
  84. fi
  85. if [ -r "$option_fifo_file" ]; then
  86. if [ "$option_force" -eq 1 ]; then
  87. rm "$option_fifo_file"
  88. else
  89. die "FIFO file exists, remove it first: $option_fifo_file"
  90. fi
  91. fi
  92. if [ $# -le 0 ]; then
  93. die "please provide a command to run after -- "
  94. fi
  95. ################################################################################
  96. export IFS=$'\n'
  97. trap cleanup EXIT
  98. prepare
  99. while :; do
  100. read -r json < "$option_fifo_file"
  101. subcommand "$json" "$@" &
  102. done