saturnin.sh.skel 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #!/bin/bash
  2. shellfu import exit
  3. shellfu import inigrep
  4. shellfu import pretty
  5. #
  6. # Git commit hash of application source tree
  7. #
  8. # This is supposed to be set by your build scripts when building your
  9. # application. The string is returned by calling your meta-command with
  10. # option --saturnin-app-git-hash.
  11. #
  12. # Look for 'satcmd' template for a working example.
  13. #
  14. SATURNIN_APP_GIT_HASH=${SATURNIN_APP_GIT_HASH:-}
  15. #
  16. # Your application version
  17. #
  18. # This is supposed to be set by your build scripts when building your
  19. # application. The string is returned by calling your meta-command with
  20. # options --version, --version-semver or --saturnin-app-version.
  21. #
  22. # Look for 'satcmd' template for a working example.
  23. #
  24. SATURNIN_APP_VERSION=${SATURNIN_APP_VERSION:-}
  25. #
  26. # Path to user cache
  27. #
  28. SATURNIN_CACHE_HOME=${SATURNIN_CACHE_HOME:-}
  29. #
  30. # Path where saturnin__conf should look for files
  31. #
  32. # If filename does not contain slash, it is looked up in each (or all,
  33. # based on strategy--see saturnin__conf() doc) path in this list. The
  34. # list is colon-separated and non-dirs as well as empty strings are
  35. # silently ignored.
  36. #
  37. SATURNIN_CONF_PATH="${SATURNIN_CONF_PATH:-}"
  38. #
  39. # Expected config filename extension (for guessing from path head)
  40. #
  41. # If no filename to read is given, saturnin__conf() will guess filename
  42. # as the path head plus this suffix (e.g. `foo.ini` for `saturnin__conf
  43. # foo.bar.baz`)
  44. #
  45. SATURNIN_CONF_SUFFIX="${SATURNIN_CONF_SUFFIX:-.ini}"
  46. #
  47. # Directory where to look for subcommands
  48. #
  49. # Files here starting with $SATURNIN_LIBEXEC_PREFIX are considered
  50. # subcommands
  51. #
  52. SATURNIN_LIBEXEC="${SATURNIN_LIBEXEC:-}"
  53. #
  54. # Subcommand file prefix
  55. #
  56. # This is recommended to be set to meta-command name plus dash. For
  57. # example, if your meta-command is `mykit`, this should be set to
  58. # `mykit-`.
  59. #
  60. SATURNIN_LIBEXEC_PREFIX="${SATURNIN_LIBEXEC_PREFIX:-}"
  61. #
  62. # Meta-command help mode
  63. #
  64. # This controls what is displayed when user calls meta-command with --help
  65. # argument. Following formats are supported:
  66. #
  67. # +HELPFILE
  68. # =HELPFILE
  69. #
  70. # In both cases, HELPFILE must be absolute path to a file containing
  71. # human-readable description of the meta-command, which will be directly
  72. # presented to user.
  73. #
  74. # If the leading character is '=' (equal sign), the help text consists of,
  75. # and only of the HELPFILE. If the character is '+' (plus sign), the help
  76. # text is pre-pended with auto-generated usage message.
  77. #
  78. # If the value is empty, only the auto-generated usage message is printed.
  79. #
  80. SATURNIN_META_HELP="${SATURNIN_META_HELP:-}"
  81. saturnin__bug() {
  82. #
  83. # Warn about bug in your software
  84. #
  85. # Issue warning using warn() from pretty but also add application
  86. # version. This is useful when an assertion in your application fails
  87. # such that it is certain that there is a bug inside it.
  88. #
  89. # In such cases, it makes sense to print also version information to
  90. # help users with reporting.
  91. #
  92. local msg # message core
  93. for msg in "$@";
  94. do
  95. warn "bug: $msg"
  96. done
  97. warn "bug in $(basename "$0") version: $SATURNIN_APP_VERSION"
  98. }
  99. saturnin__conf() {
  100. #
  101. # inigrep smart loader
  102. #
  103. # Usage:
  104. # saturnin__conf [-j] [inigrep-query] [-- [file]..]
  105. #
  106. #
  107. # File arguments
  108. # ==============
  109. #
  110. # If omitted, *file* argument is inferred by taking part of kpath name
  111. # before first dot and appending value of `$SATURNIN_CONF_SUFFIX`,
  112. # (".ini" by default).
  113. #
  114. # Each *file* argument is then processed as follows:
  115. #
  116. # * `-` (single dash) is interpreted as reading from standard input.
  117. #
  118. # * If argument contains slash, it is expanded as a regular path
  119. # (relative or absolute).
  120. #
  121. # * Otherwise, it is taken as filename and searched for in directories
  122. # given in `$SATURNIN_CONF_PATH`. (This can yield more than one
  123. # path, which is equivalent as if all paths were provided.)
  124. #
  125. # Not all files expanded based on `$SATURNIN_CONF_PATH` are read by
  126. # default; reading is governed by "merge strategy": the default
  127. # strategy "first" reads only the first existing file.
  128. #
  129. # "join" strategy on the other hand, means that any files are simply
  130. # concatenated and prefixed with comment (visible only in raw mode)
  131. # containing path to the file.
  132. #
  133. # This means that if a section is queried that is present in both
  134. # files, it is effectively concatenated as well.
  135. #
  136. # Following calls are equivalent
  137. #
  138. # saturnin__conf foo.bar.baz
  139. # saturnin__conf foo.bar.baz foo.ini
  140. #
  141. # and both result in reading of key *baz* from section *foo.bar* in file
  142. # *foo.ini*, which is selected from *SATURNIN_CONF_PATH*. Should there
  143. # be more foo.ini's, the first is selected. Using `-j` switch
  144. #
  145. # saturnin__conf -j foo.bar.baz
  146. #
  147. # would cause all foo.ini's on *SATURNIN_CONF_PATH* be concatenated
  148. # instead.
  149. #
  150. local ig_mode # retrieval mode
  151. local ig_query # keypath or section name (when listing keys)
  152. local ig_limit # line limit
  153. local files=() # file specification
  154. local Strategy=first # merge strategy
  155. while true; do case $1:$2 in
  156. "":*) break ;;
  157. -j:*) Strategy=join; shift 1 ;;
  158. -1:*) ig_limit=$1; shift 1 ;;
  159. -e:*.*) ig_mode=$1; ig_query=$2; shift 2; break ;;
  160. -r:*.*) ig_mode=$1; ig_query=$2; shift 2; break ;;
  161. -K:*) ig_mode=$1; ig_query=$2; shift 2; break ;;
  162. -S:*) ig_mode=$1; ig_query=""; shift 1; break ;;
  163. -P:*) ig_mode=$1; ig_query=""; shift 1; break ;;
  164. .*:*) _saturnin__conf_usage -w "bad syntax: $*" ;;
  165. *.*:*) ig_mode=-e; ig_query=$1; shift 1; break ;;
  166. --help:*) _saturnin__conf_usage -e 0 ;;
  167. *) _saturnin__conf_usage -w "bad syntax: $*" ;;
  168. esac done
  169. test -n "$ig_mode" || _saturnin__conf_usage -w "could not determine inigrep mode"
  170. debug -v ig_limit ig_query ig_mode Strategy
  171. if test -n "$*";
  172. then
  173. files=("$@")
  174. elif test -n "$ig_query";
  175. then
  176. files=("${ig_query%%.*}$SATURNIN_CONF_SUFFIX")
  177. else
  178. _saturnin__conf_usage -w "dunno what to load"
  179. fi
  180. debug -v files
  181. #shellcheck disable=SC2086
  182. _saturnin__conf__load "${files[@]}" | inigrep $ig_limit $ig_mode "$ig_query"
  183. return "${PIPESTATUS[0]}"
  184. }
  185. saturnin__conf_find() {
  186. #
  187. # Find all existing instances of sub-path $1 on $SATURNIN_CONF_PATH
  188. #
  189. # Go through all elements of $SATURNIN_CONF_PATH, looking for file on
  190. # sub-path $1. Print each existing path, ignore rest.
  191. #
  192. # If at least one path was found, return zero. Otherwise, return one,
  193. # or more in case of error.
  194. #
  195. local file=$1 # sub-path to find
  196. local trydir # each item of $SATURNIN_CONF_PATH
  197. local trypath # each combined path
  198. debug -v SATURNIN_CONF_PATH
  199. echos "$SATURNIN_CONF_PATH" \
  200. | tr ':' '\n' \
  201. | while read -r trydir;
  202. do
  203. test -n "$trydir" || continue
  204. trypath="$trydir/$file"
  205. test -e "$trypath" || continue
  206. echos "$trypath"
  207. done \
  208. | grep .
  209. }
  210. saturnin__get() {
  211. #
  212. # Show Saturnin internal info by key $1 and exit
  213. #
  214. # Key $1 can be whole `--saturnin-get-stuff` argument or just the part
  215. # after `--saturnin-get-`.
  216. #
  217. # This is aimed to help debugging and testing the app (or Saturnin
  218. # itself) by showing packaging and deployment related info.
  219. #
  220. local key=${1#--saturnin-get-} # internal info key
  221. case "$key" in
  222. saturnin-conf-path) echo "$SATURNIN_CONF_PATH" ;;
  223. saturnin-version) echo "__MKIT_PROJ_VERSION__" ;;
  224. app-git-hash) echo "$SATURNIN_APP_GIT_HASH" ;;
  225. app-version) echo "$SATURNIN_APP_VERSION" ;;
  226. cache-home) echo "$SATURNIN_CACHE_HOME" ;;
  227. libexec) echo "$SATURNIN_LIBEXEC" ;;
  228. libexec-prefix) echo "$SATURNIN_LIBEXEC_PREFIX" ;;
  229. *) warn "unknown devel key: $key"
  230. exit "$EXIT_USAGE" ;;
  231. esac
  232. exit "$EXIT_OK"
  233. }
  234. saturnin__lssc() {
  235. #
  236. # List subcommands
  237. #
  238. find "$SATURNIN_LIBEXEC" \
  239. -mindepth 1 \
  240. -maxdepth 1 \
  241. -executable \
  242. -name "$SATURNIN_LIBEXEC_PREFIX*" \
  243. | sed -e "s|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
  244. | sort
  245. }
  246. saturnin__main() {
  247. #
  248. # Main meta-command entry function
  249. #
  250. # After setting all mandatory environment variables, call this from your
  251. # main meta-command script.
  252. #
  253. local subcommand # subcommand to execute (first non-option)
  254. test -n "$SATURNIN_CACHE_HOME" || die "SATURNIN_CACHE_HOME is not set"
  255. test -n "$SATURNIN_LIBEXEC" || die "SATURNIN_LIBEXEC is not set"
  256. test -n "$SATURNIN_LIBEXEC_PREFIX" || die "SATURNIN_LIBEXEC_PREFIX is not set"
  257. while true; do case $1 in
  258. -D|--full-debug) export PRETTY_DEBUG=true
  259. export PRETTY_DEBUG_EXCLUDE=""
  260. shift ;;
  261. -d|--debug) export PRETTY_DEBUG=true; shift ;;
  262. -v|--verbose) export PRETTY_VERBOSE=true; shift ;;
  263. -h|--help) saturnin__help; exit ;;
  264. --version) saturnin__version; exit ;;
  265. -V|--version-semver) saturnin__get app-version ;;
  266. --saturnin-get-*) saturnin__get "$1" ;;
  267. -*) saturnin__usage -w "unknown argument: $1" ;;
  268. --*) saturnin__usage -w "unknown argument: $1" ;;
  269. --) shift; break ;;
  270. "") saturnin__usage -w "too few arguments" ;;
  271. *) break; ;;
  272. esac done
  273. subcommand="$1"; shift
  274. debug -v SATURNIN_APP_VERSION SATURNIN_CONF_PATH
  275. case "$subcommand" in
  276. conf) saturnin__conf "$@" ;;
  277. *) saturnin__runsc "$subcommand" "$@" ;;
  278. esac
  279. }
  280. saturnin__help() {
  281. #
  282. # Print meta-command help text
  283. #
  284. # See $SATURNIN_META_HELP for details.
  285. #
  286. local introline # introduction line
  287. introline=$(basename "$0")
  288. test -n "$SATURNIN_APP_TAGLINE" \
  289. && introline+=" - $SATURNIN_APP_TAGLINE"
  290. case "$SATURNIN_META_HELP" in
  291. "")
  292. echo "$introline"$'\n' >&2
  293. saturnin__usage -E -e 0
  294. ;;
  295. +/*)
  296. echo "$introline"$'\n' >&2
  297. saturnin__usage -E
  298. echo >&2
  299. _saturnin__cat_helpfile "${SATURNIN_META_HELP:1}"
  300. ;;
  301. =/*)
  302. _saturnin__cat_helpfile "${SATURNIN_META_HELP:1}"
  303. ;;
  304. *)
  305. echo "$introline"$'\n' >&2
  306. saturnin__usage -E
  307. saturnin__bug "malformed SATURNIN_META_HELP: $SATURNIN_META_HELP"
  308. return 3
  309. ;;
  310. esac
  311. }
  312. saturnin__conf_mkpath() {
  313. #
  314. # Assemble SATURNIN_CONF_PATH from locations $@
  315. #
  316. # For each location, print colon-delimited list of directories. If
  317. # location ends with "/ini.d", list of subfolders, sorted by C locale is
  318. # printed--this allows for modular configuration. Otherwise the
  319. # location is printed. Non-existent or non-directory locations are
  320. # silently ignored.
  321. #
  322. local location # one location argument
  323. local path # one path listed
  324. for location in "$@";
  325. do
  326. test -d "$location" || continue
  327. case "$location" in
  328. */ini.d) # modular location--sort subfolders
  329. find -L "$location" -mindepth 1 -maxdepth 1 -type d \
  330. | LC_ALL=C sort
  331. ;;
  332. *)
  333. echo "$location"
  334. ;;
  335. esac
  336. done \
  337. | _saturnin__nl2colon
  338. }
  339. saturnin__runhook() {
  340. #
  341. # Run custom hook named $1 from respective configuration section
  342. #
  343. # Will load joined multi-line key "hook.$SATURNIN_SUBCOMMAND.$1" and
  344. # unless syntax check fails, execute it as Bash code (in separate
  345. # process).
  346. #
  347. local name="$1" # hook name
  348. local code # ... code
  349. test -n "$SATURNIN_SUBCOMMAND" || {
  350. warn "unknown subcommand, ignoring hook: $name"
  351. return 0
  352. }
  353. code="$(saturnin__conf -j "hook.$SATURNIN_SUBCOMMAND.$name")"
  354. debug -v SATURNIN_SUBCOMMAND code name
  355. bash -n <<<"$code" || {
  356. warn "syntax errors, ignoring hook: $name"
  357. return 0
  358. }
  359. bash <<<"$code"
  360. }
  361. saturnin__runsc() {
  362. #
  363. # Run subcommand $SATURNIN_SUBCOMMAND
  364. #
  365. local subcommand="$1"; shift
  366. local binpath # path to subcommand's binary
  367. binpath+="$SATURNIN_LIBEXEC/"
  368. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  369. debug -v binpath
  370. debug "\$*='$*'"
  371. test -x "$binpath" \
  372. || saturnin__usage "invalid sub-command: $subcommand"
  373. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  374. }
  375. saturnin__usage() {
  376. #
  377. # Show usage message and exit
  378. #
  379. #shellcheck disable=SC2046
  380. mkusage "$@" \
  381. "[options] COMMAND [ARG...]" \
  382. -o \
  383. "-D, --full-debug turn on gory debugging" \
  384. "-V, --version show version and exit" \
  385. "-d, --debug turn on debugging" \
  386. "-h, --help show this help message and exit"\
  387. "-v, --verbose turn on verbosity" \
  388. -c \
  389. $(saturnin__lssc)
  390. }
  391. saturnin__version() {
  392. #
  393. # Print version info
  394. #
  395. echo -n "$(basename "$0")"
  396. test -n "$SATURNIN_APP_TAGLINE" \
  397. && echo -n " ($SATURNIN_APP_TAGLINE)"
  398. echo -n " $SATURNIN_APP_VERSION"
  399. test -n "$SATURNIN_APP_CODENAME" \
  400. && echo -n " - $SATURNIN_APP_CODENAME"
  401. echo
  402. echo -n "Powered by Saturnin (__MKIT_PROJ_TAGLINE__)"
  403. echo -n " __MKIT_PROJ_VERSION__"
  404. echo -n " - __MKIT_PROJ_CODENAME__"
  405. echo
  406. return "$EXIT_OK"
  407. }
  408. saturnin__wraphook() {
  409. #
  410. # Wrap command $@ in hooks 'pre' and 'post'
  411. #
  412. # Run pre hook, then command $@, then post hook. Always exit with
  413. # status of the payload command, even if hooks fail. Ignore post-hook
  414. # if payload command failed.
  415. #
  416. local es=0 # exit status of thid function
  417. saturnin__runhook pre
  418. "$@" || return $?
  419. es=$?
  420. saturnin__runhook post
  421. return $es
  422. }
  423. # # that what you see below this line #
  424. # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
  425. # # use in your code to anger the divine #
  426. _saturnin__cat_helpfile() {
  427. #
  428. # Print helpfile $1
  429. #
  430. local helpfile=$1 # path to help file
  431. cat "$helpfile" >&2 && return 0
  432. saturnin__bug "cannot print help file: $helpfile"
  433. return 3
  434. }
  435. _saturnin__conf__merge() {
  436. #
  437. # Take paths and applying merge strategy, load file(s)
  438. #
  439. local path
  440. local found=false
  441. while read -r path;
  442. do
  443. found=true
  444. case $Strategy in
  445. first)
  446. debug "winner: $path"
  447. cat "$path"
  448. cat >/dev/null # throw away rest of paths
  449. ;;
  450. join)
  451. echo "# file: ${path/$HOME/~}"
  452. cat "$path" 2>/dev/null
  453. ;;
  454. esac
  455. done
  456. $found
  457. }
  458. _saturnin__conf__load() {
  459. #
  460. # Print contents of files specified in $@
  461. #
  462. # Each argument means possible file candidate. If candidate contains
  463. # slash, it's treated as file path and is printed directly. If it's
  464. # single dash, standard input is copied.
  465. #
  466. # In all other cases, filename is searched in all elements of variable
  467. # SATURNIN_CONF_PATH; output then depends on chosen $Strategy: with
  468. # 'first' strategy, first existing file is printed, with 'join'
  469. # strategy, all existing files are printed.
  470. #
  471. local arg es
  472. es=0
  473. for arg in "$@";
  474. do
  475. case $arg in
  476. -|*/*) # stdin, or path (with slash)
  477. cat "$arg" || es=3
  478. ;;
  479. *) # name given, find all its incarnations
  480. saturnin__conf_find "$arg" \
  481. | _saturnin__conf__merge; es=$?
  482. ;;
  483. esac
  484. done
  485. return $es
  486. }
  487. _saturnin__conf_usage() {
  488. #
  489. # Show usage message and exit
  490. #
  491. PRETTY_USAGE="self=${0##*/} conf" \
  492. mkusage "$@" \
  493. "[options] [-e] SECTION.KEY [FNAME]" \
  494. "[options] -r SECTION.KEY [FNAME]" \
  495. "[options] -K SECTION [FNAME]" \
  496. "[options] -P FNAME" \
  497. "[options] -S FNAME" \
  498. -- \
  499. "Use inigrep to query config files." \
  500. -o \
  501. "-j join all files before applying query" \
  502. "-1 ensure single line is returned" \
  503. -c \
  504. "-e use normal mode (default)" \
  505. "-r use raw mode (preserves RHS whitespace and some comments)"\
  506. "-K list available keys in SECTION" \
  507. "-S list available sections in FNAME" \
  508. "-P list available keypaths (SECTION.KEY) in FNAME" \
  509. -- \
  510. "FNAME is filename, which is then searched on all paths specified"\
  511. "in SATURNIN_CONF_PATH and depending on -j parameter, first one" \
  512. "wins or all are joined. If FNAME contains slash, this search is"\
  513. "not done and FNAME is taken as path to file that is then" \
  514. "queried." \
  515. "" \
  516. "If FNAME is omitted, it is inferred from SECTION (e.g. 'foo.ini'"\
  517. "if 'foo.bar' was section name; note that section name itself may"\
  518. "contain dot)."
  519. }
  520. _saturnin__nl2colon() {
  521. #
  522. # Convert newline-based list of paths to colon:based:list
  523. #
  524. # Empty paths must not be included in the resulting list, so we need to
  525. # drop them and also get the colons right.
  526. #
  527. local idx=0 # current item index (zero-based)
  528. local path # each path on stdin
  529. while read -r path;
  530. do
  531. test -z "$path" && continue
  532. test $idx -gt 0 && echo -n ':'
  533. echo -n "$path"
  534. ((idx++))
  535. done
  536. }
  537. #shellfu module-version: __MKIT_PROJ_VERSION__