saturnin.sh 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #!/bin/bash
  2. shellfu import exit
  3. shellfu import inigrep
  4. shellfu import pretty
  5. #
  6. # Path where saturnin__conf should look for files
  7. #
  8. # If filename does not contain slash, it is looked up in each
  9. # (or all, based on strategy--see saturnin__conf() doc) path in this
  10. # list. The list is colon-separated and non-dirs as well as
  11. # empty strings are silently ignored.
  12. #
  13. SATURNIN_CONF_PATH="${SATURNIN_CONF_PATH:-}"
  14. #
  15. # Expected config filename extension (for guessing from path head)
  16. #
  17. # If no filename to read is given, saturnin__conf() will guess
  18. # filename as the path head plus this suffix (e.g. `foo.ini` for
  19. # `saturnin__conf foo.bar.baz`)
  20. #
  21. SATURNIN_CONF_SUFFIX="${SATURNIN_CONF_SUFFIX:-.ini}"
  22. saturnin__conf() {
  23. #
  24. # inigrep smart loader
  25. #
  26. # Usage:
  27. # saturnin__conf [-j] [inigrep-query] [-- [file]..]
  28. #
  29. #
  30. # File arguments
  31. # ==============
  32. #
  33. # If omitted, *file* argument is inferred by taking part of
  34. # kpath name before first dot and appending value of
  35. # `$SATURNIN_CONF_SUFFIX`, (".ini" by default).
  36. #
  37. # Each *file* argument is then processed as follows:
  38. #
  39. # * `-` (single dash) is interpreted as reading from
  40. # STDIN.
  41. #
  42. # * If argument contains slash, it is expanded as a regular
  43. # path (relative or absolute).
  44. #
  45. # * Otherwise, it is taken as filename and searched for
  46. # in directories given in `$SATURNIN_CONF_PATH`. (This can
  47. # yield more than one path, which is equivalent as if
  48. # all paths were provided.)
  49. #
  50. # Not all files expanded based on `$SATURNIN_CONF_PATH`
  51. # are read by default; reading is governed by "merge
  52. # strategy": the default strategy "first" reads only
  53. # the first existing file.
  54. #
  55. # "join" strategy on the other hand, means that any
  56. # files are simply concatenated and prefixed with
  57. # comment (visible only in raw mode) containing path
  58. # to the file.
  59. #
  60. # This means that if a section is queried that is
  61. # present in both files, it is effectively concatenated
  62. # as well.
  63. #
  64. # Following calls are equivalent
  65. #
  66. # saturnin__conf foo.bar.baz
  67. # saturnin__conf foo.bar.baz foo.ini
  68. #
  69. # and both result in reading of key *baz* from section *foo.bar*
  70. # in file *foo.ini*, which is selected from *SATURNIN_CONF_PATH*.
  71. # Should there be more foo.ini's, the first is selected.
  72. # Using `-j` switch
  73. #
  74. # saturnin__conf -j foo.bar.baz
  75. #
  76. # would cause all foo.ini's on *SATURNIN_CONF_PATH* be
  77. # concatenated instead.
  78. #
  79. local ig_mode # retrieval mode
  80. local ig_query # keypath or section name (when listing keys)
  81. local ig_limit # line limit
  82. local files=() # file specification
  83. local Strategy=first # merge strategy
  84. while true; do case $1:$2 in
  85. "":*) break ;;
  86. -j:*) Strategy=join; shift 1 ;;
  87. -1:*) ig_limit=$1; shift 1 ;;
  88. -e:*.*) ig_mode=$1; ig_query=$2; shift 2; break ;;
  89. -r:*.*) ig_mode=$1; ig_query=$2; shift 2; break ;;
  90. -K:*) ig_mode=$1; ig_query=$2; shift 2; break ;;
  91. -S:*) ig_mode=$1; ig_query=""; shift 1; break ;;
  92. -P:*) ig_mode=$1; ig_query=""; shift 1; break ;;
  93. .*:*) warn "bad syntax: $*"; return 2 ;;
  94. *.*:*) ig_mode=-e; ig_query=$1; shift 1; break ;;
  95. *) warn "bad syntax: $*"; return 2 ;;
  96. esac done
  97. test -n "$ig_mode" || { warn "could not determine inigrep mode"; return 2; }
  98. debug -v ig_limit ig_query ig_mode Strategy
  99. if test -n "$*";
  100. then
  101. files=("$@")
  102. elif test -n "$ig_query";
  103. then
  104. files=("${ig_query%%.*}$SATURNIN_CONF_SUFFIX")
  105. else
  106. warn "dunno what to load"
  107. return 2
  108. fi
  109. debug -v files
  110. #shellcheck disable=SC2086
  111. _saturnin__conf__load "${files[@]}" | inigrep $ig_limit $ig_mode $ig_query
  112. }
  113. saturnin__get() {
  114. #
  115. # Show Saturnin Internal info by key $1 and exit
  116. #
  117. # Key $1 can be whole `--saturnin-get-stuff` argument or just
  118. # the part after `--saturnin-get-`.
  119. #
  120. # This is aimed to help debugging and testing the app (or
  121. # Saturnin itself) by showing packaging and deployment related
  122. # info.
  123. #
  124. local key=${1#--saturnin-get-}
  125. case "$key" in
  126. shellfu-path) echo "$SHELLFU_PATH" ;;
  127. saturnin-conf-path) echo "$SATURNIN_CONF_PATH" ;;
  128. app-git-hash) echo "$SATURNIN_APP_GIT_HASH" ;;
  129. app-version) echo "$SATURNIN_APP_VERSION" ;;
  130. cache-home) echo "$SATURNIN_CACHE_HOME" ;;
  131. libexec) echo "$SATURNIN_LIBEXEC" ;;
  132. libexec-prefix) echo "$SATURNIN_LIBEXEC_PREFIX" ;;
  133. *) warn "unknown devel key: $key"
  134. exit "$EXIT_USAGE" ;;
  135. esac
  136. exit "$EXIT_OK"
  137. }
  138. saturnin__help() {
  139. saturnin__usage --wanted
  140. }
  141. saturnin__lssc() {
  142. #
  143. # List subcommands
  144. #
  145. echo conf
  146. find "$SATURNIN_LIBEXEC" \
  147. -mindepth 1 \
  148. -maxdepth 1 \
  149. -executable \
  150. | sed -e "s|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
  151. | sort
  152. }
  153. saturnin__main() {
  154. local subcommand
  155. while true; do case $1 in
  156. -d|--debug) export PRETTY_DEBUG=true; shift ;;
  157. -v|--verbose) export PRETTY_VERBOSE=true; shift ;;
  158. -h|--help) saturnin__help; exit ;;
  159. --version) saturnin__version; exit ;;
  160. --version-semver) saturnin__get app-version ;;
  161. --saturnin-get-*) saturnin__get "$1" ;;
  162. -*) saturnin__usage; ;;
  163. --*) saturnin__usage; ;;
  164. --) shift; break ;;
  165. "") saturnin__usage; ;;
  166. *) break; ;;
  167. esac done
  168. subcommand="$1"; shift
  169. debug -v SHELLFU_PATH SATURNIN_LIBEXEC SATURNIN_CONF_PATH
  170. case "$subcommand" in
  171. conf) saturnin__conf "$@" ;;
  172. _ls_sc) saturnin__lssc ;;
  173. _lsfun) shellfu-get lsfun ;;
  174. _lsmod) shellfu-get lsmod ;;
  175. *) saturnin__runsc "$subcommand" "$@" ;;
  176. esac
  177. }
  178. saturnin__conf_mkpath() {
  179. #
  180. # Assemble SATURNIN_CONF_PATH from locations $@
  181. #
  182. # For each location, print colon-delimited list of
  183. # directories. If location ends with "/ini.d", list of
  184. # subfolders, sorted by C locale is printed--this allows
  185. # for modular configuration. Otherwise the location
  186. # is printed. Non-existent or non-directory locations
  187. # are silently ignored.
  188. #
  189. local location # one location argument
  190. local path # one path listed
  191. for location in "$@";
  192. do
  193. test -d "$location" || continue
  194. case "$location" in
  195. */ini.d) # modular location--sort subfolders
  196. find -L "$location" -mindepth 1 -maxdepth 1 -type d \
  197. | LC_ALL=C sort
  198. ;;
  199. *)
  200. echo "$location"
  201. ;;
  202. esac
  203. done \
  204. | _saturnin__nl2colon
  205. }
  206. saturnin__runhook() {
  207. #
  208. # Run custom hook
  209. #
  210. local hname="$1"
  211. local hook_code
  212. test -n "$SATURNIN_SUBCOMMAND" || {
  213. warn "unknown subcommand, ignoring hook: $hname"
  214. return 0
  215. }
  216. hook_code="$(saturnin__conf -j "hook.$SATURNIN_SUBCOMMAND.$hname")"
  217. debug -v SATURNIN_SUBCOMMAND hook_code hname
  218. bash -n <<<"$hook_code" || {
  219. warn "syntax errors, ignoring hook: $hname"
  220. return 0
  221. }
  222. eval "$hook_code"
  223. }
  224. saturnin__runsc() {
  225. #
  226. # Run subcommand $SATURNIN_SUBCOMMAND
  227. #
  228. local subcommand="$1"; shift
  229. local binpath # path to subcommand's binary
  230. binpath+="$SATURNIN_LIBEXEC/"
  231. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  232. debug -v binpath
  233. debug "\$*='$*'"
  234. test -x "$binpath" || {
  235. warn "invalid sub-command: $subcommand"
  236. saturnin__usage
  237. }
  238. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  239. }
  240. saturnin__usage() {
  241. local es_param # exit-status related arguments
  242. test "$1" == "--wanted" && es_param="-e 0"
  243. #shellcheck disable=SC2086 disable=SC2046
  244. mkusage $es_param \
  245. "[options] COMMAND [ARG...]" \
  246. -o \
  247. "--debug turn on debugging" \
  248. "--help show this help message and exit" \
  249. "--verbose turn on verbosity" \
  250. "--version show version and exit" \
  251. -c \
  252. $(saturnin__lssc)
  253. }
  254. saturnin__version() {
  255. #
  256. # Print version info
  257. #
  258. local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
  259. local maybe_codename=""
  260. test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
  261. echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
  262. return "$EXIT_OK"
  263. }
  264. saturnin__wraphook() {
  265. #
  266. # Wrap command "$@" in hooks
  267. #
  268. # Run pre hook, then "$@", then post hook. Always exit
  269. # with status of "$@", even if hooks fail. Ignore
  270. # post-hook if "$@" failed.
  271. #
  272. local es=0
  273. saturnin__runhook pre
  274. "$@" || return $?
  275. es=$?
  276. saturnin__runhook post
  277. return $es
  278. }
  279. # # that what you see below this line #
  280. # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
  281. # # use in your code to anger the divine #
  282. _saturnin__conf__merge() {
  283. #
  284. # Take paths and applying merge strategy, load file(s)
  285. #
  286. local path
  287. while read -r path;
  288. do
  289. test -f "$path" || continue
  290. case $Strategy in
  291. first)
  292. debug "winner: $path"
  293. cat "$path"
  294. cat >/dev/null # throw away rest of paths
  295. ;;
  296. join)
  297. echo "# file: ${path/$HOME/~}"
  298. cat "$path" 2>/dev/null
  299. ;;
  300. esac
  301. done
  302. }
  303. _saturnin__conf__load() {
  304. #
  305. # Print contents of files specified in $@
  306. #
  307. # Each argument means possible file candidate. If candidate
  308. # contains slash, it's treated as file path and is printed
  309. # directly. If it's single dash, standard input is copied.
  310. #
  311. # In all other cases, filename is searched in all elements
  312. # of SATURNIN_CONF_PATH; output then depends on chosen $Strategy:
  313. # with 'first' strategy, first existing file is printed, with
  314. # 'join' strategy. all existing files are printed.
  315. #
  316. local arg trydir trypath
  317. for arg in "$@";
  318. do
  319. case $arg in
  320. -|*/*) # stdin, or path (with slash)
  321. cat "$arg"
  322. ;;
  323. *) # name given, find all its incarnations
  324. debug -v SATURNIN_CONF_PATH
  325. echos "$SATURNIN_CONF_PATH" \
  326. | tr ':' '\n' \
  327. | while read -r trydir;
  328. do
  329. test -n "$trydir" || continue
  330. trypath="$trydir/$arg"
  331. echos "$trypath"
  332. done \
  333. | _saturnin__conf__merge
  334. ;;
  335. esac
  336. done
  337. true
  338. }
  339. _saturnin__nl2colon() {
  340. #
  341. # Convert newline-based list of paths to colon:based:list
  342. #
  343. # Empty paths must not be included in the resulting list,
  344. # so we need to drop them and also get the colons right.
  345. #
  346. local idx=0 # current item index (zero-based)
  347. local path
  348. while read -r path;
  349. do
  350. test -z "$path" && continue
  351. test $idx -gt 0 && echo -n ':'
  352. echo -n "$path"
  353. ((idx++))
  354. done
  355. }