saturnin.sh.skel 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. .*:*) warn "bad syntax: $*"; _saturnin__conf_usage ;;
  165. *.*:*) ig_mode=-e; ig_query=$1; shift 1; break ;;
  166. --help:*) _saturnin__conf_usage -e 0 ;;
  167. *) warn "bad syntax: $*"; _saturnin__conf_usage ;;
  168. esac done
  169. test -n "$ig_mode" || { warn "could not determine inigrep mode"; _saturnin__conf_usage; }
  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. warn "dunno what to load"
  179. _saturnin__conf_usage
  180. fi
  181. debug -v files
  182. #shellcheck disable=SC2086
  183. _saturnin__conf__load "${files[@]}" | inigrep $ig_limit $ig_mode "$ig_query"
  184. return "${PIPESTATUS[0]}"
  185. }
  186. saturnin__conf_find() {
  187. #
  188. # Find all existing instances of sub-path $1 on $SATURNIN_CONF_PATH
  189. #
  190. # Go through all elements of $SATURNIN_CONF_PATH, looking for file on
  191. # sub-path $1. Print each existing path, ignore rest.
  192. #
  193. # If at least one path was found, return zero. Otherwise, return one,
  194. # or more in case of error.
  195. #
  196. local file=$1 # sub-path to find
  197. local trydir # each item of $SATURNIN_CONF_PATH
  198. local trypath # each combined path
  199. debug -v SATURNIN_CONF_PATH
  200. echos "$SATURNIN_CONF_PATH" \
  201. | tr ':' '\n' \
  202. | while read -r trydir;
  203. do
  204. test -n "$trydir" || continue
  205. trypath="$trydir/$file"
  206. test -e "$trypath" || continue
  207. echos "$trypath"
  208. done \
  209. | grep .
  210. }
  211. saturnin__get() {
  212. #
  213. # Show Saturnin internal info by key $1 and exit
  214. #
  215. # Key $1 can be whole `--saturnin-get-stuff` argument or just the part
  216. # after `--saturnin-get-`.
  217. #
  218. # This is aimed to help debugging and testing the app (or Saturnin
  219. # itself) by showing packaging and deployment related info.
  220. #
  221. local key=${1#--saturnin-get-} # internal info key
  222. case "$key" in
  223. saturnin-conf-path) echo "$SATURNIN_CONF_PATH" ;;
  224. saturnin-version) echo "__MKIT_PROJ_VERSION__" ;;
  225. app-git-hash) echo "$SATURNIN_APP_GIT_HASH" ;;
  226. app-version) echo "$SATURNIN_APP_VERSION" ;;
  227. cache-home) echo "$SATURNIN_CACHE_HOME" ;;
  228. libexec) echo "$SATURNIN_LIBEXEC" ;;
  229. libexec-prefix) echo "$SATURNIN_LIBEXEC_PREFIX" ;;
  230. *) warn "unknown devel key: $key"
  231. exit "$EXIT_USAGE" ;;
  232. esac
  233. exit "$EXIT_OK"
  234. }
  235. saturnin__lssc() {
  236. #
  237. # List subcommands
  238. #
  239. find "$SATURNIN_LIBEXEC" \
  240. -mindepth 1 \
  241. -maxdepth 1 \
  242. -executable \
  243. -name "$SATURNIN_LIBEXEC_PREFIX*" \
  244. | sed -e "s|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
  245. | sort
  246. }
  247. saturnin__main() {
  248. #
  249. # Main meta-command entry function
  250. #
  251. # After setting all mandatory environment variables, call this from your
  252. # main meta-command script.
  253. #
  254. local subcommand # subcommand to execute (first non-option)
  255. test -n "$SATURNIN_CACHE_HOME" || die "SATURNIN_CACHE_HOME is not set"
  256. test -n "$SATURNIN_LIBEXEC" || die "SATURNIN_LIBEXEC is not set"
  257. test -n "$SATURNIN_LIBEXEC_PREFIX" || die "SATURNIN_LIBEXEC_PREFIX is not set"
  258. while true; do case $1 in
  259. -D|--full-debug) export PRETTY_DEBUG=true
  260. export PRETTY_DEBUG_EXCLUDE=""
  261. shift ;;
  262. -d|--debug) export PRETTY_DEBUG=true; shift ;;
  263. -v|--verbose) export PRETTY_VERBOSE=true; shift ;;
  264. -h|--help) saturnin__help; exit ;;
  265. --version) saturnin__version; exit ;;
  266. -V|--version-semver) saturnin__get app-version ;;
  267. --saturnin-get-*) saturnin__get "$1" ;;
  268. -*) saturnin__usage; ;;
  269. --*) saturnin__usage; ;;
  270. --) shift; break ;;
  271. "") saturnin__usage; ;;
  272. *) break; ;;
  273. esac done
  274. subcommand="$1"; shift
  275. debug -v SATURNIN_APP_VERSION SATURNIN_CONF_PATH
  276. case "$subcommand" in
  277. conf) saturnin__conf "$@" ;;
  278. *) saturnin__runsc "$subcommand" "$@" ;;
  279. esac
  280. }
  281. _saturnin__cat_helpfile() {
  282. #
  283. # Print helpfile $1
  284. #
  285. local helpfile=$1 # path to help file
  286. cat "$helpfile" >&2 && return 0
  287. saturnin__bug "cannot print help file: $helpfile"
  288. return 3
  289. }
  290. saturnin__help() {
  291. #
  292. # Print meta-command help text
  293. #
  294. # See $SATURNIN_META_HELP for details.
  295. #
  296. local introline # introduction line
  297. introline=$(basename "$0")
  298. test -n "$SATURNIN_APP_TAGLINE" \
  299. && introline+=" - $SATURNIN_APP_TAGLINE"
  300. case "$SATURNIN_META_HELP" in
  301. "")
  302. echo "$introline"$'\n' >&2
  303. saturnin__usage -E -e 0
  304. ;;
  305. +/*)
  306. echo "$introline"$'\n' >&2
  307. saturnin__usage -E
  308. echo >&2
  309. _saturnin__cat_helpfile "${SATURNIN_META_HELP:1}"
  310. ;;
  311. =/*)
  312. _saturnin__cat_helpfile "${SATURNIN_META_HELP:1}"
  313. ;;
  314. *)
  315. echo "$introline"$'\n' >&2
  316. saturnin__usage -E
  317. saturnin__bug "malformed SATURNIN_META_HELP: $SATURNIN_META_HELP"
  318. return 3
  319. ;;
  320. esac
  321. }
  322. saturnin__conf_mkpath() {
  323. #
  324. # Assemble SATURNIN_CONF_PATH from locations $@
  325. #
  326. # For each location, print colon-delimited list of directories. If
  327. # location ends with "/ini.d", list of subfolders, sorted by C locale is
  328. # printed--this allows for modular configuration. Otherwise the
  329. # location is printed. Non-existent or non-directory locations are
  330. # silently ignored.
  331. #
  332. local location # one location argument
  333. local path # one path listed
  334. for location in "$@";
  335. do
  336. test -d "$location" || continue
  337. case "$location" in
  338. */ini.d) # modular location--sort subfolders
  339. find -L "$location" -mindepth 1 -maxdepth 1 -type d \
  340. | LC_ALL=C sort
  341. ;;
  342. *)
  343. echo "$location"
  344. ;;
  345. esac
  346. done \
  347. | _saturnin__nl2colon
  348. }
  349. saturnin__runhook() {
  350. #
  351. # Run custom hook named $1 from respective configuration section
  352. #
  353. # Will load joined multi-line key "hook.$SATURNIN_SUBCOMMAND.$1" and
  354. # unless syntax check fails, execute it as Bash code (in separate
  355. # process).
  356. #
  357. local name="$1" # hook name
  358. local code # ... code
  359. test -n "$SATURNIN_SUBCOMMAND" || {
  360. warn "unknown subcommand, ignoring hook: $name"
  361. return 0
  362. }
  363. code="$(saturnin__conf -j "hook.$SATURNIN_SUBCOMMAND.$name")"
  364. debug -v SATURNIN_SUBCOMMAND code name
  365. bash -n <<<"$code" || {
  366. warn "syntax errors, ignoring hook: $name"
  367. return 0
  368. }
  369. bash <<<"$code"
  370. }
  371. saturnin__runsc() {
  372. #
  373. # Run subcommand $SATURNIN_SUBCOMMAND
  374. #
  375. local subcommand="$1"; shift
  376. local binpath # path to subcommand's binary
  377. binpath+="$SATURNIN_LIBEXEC/"
  378. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  379. debug -v binpath
  380. debug "\$*='$*'"
  381. test -x "$binpath" || {
  382. warn "invalid sub-command: $subcommand"
  383. saturnin__usage
  384. }
  385. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  386. }
  387. saturnin__usage() {
  388. #
  389. # Show usage message and exit
  390. #
  391. #shellcheck disable=SC2046
  392. mkusage "$@" \
  393. "[options] COMMAND [ARG...]" \
  394. -o \
  395. "-D, --full-debug turn on gory debugging" \
  396. "-V, --version show version and exit" \
  397. "-d, --debug turn on debugging" \
  398. "-h, --help show this help message and exit"\
  399. "-v, --verbose turn on verbosity" \
  400. -c \
  401. $(saturnin__lssc)
  402. }
  403. saturnin__version() {
  404. #
  405. # Print version info
  406. #
  407. echo -n "$(basename "$0")"
  408. test -n "$SATURNIN_APP_TAGLINE" \
  409. && echo -n " ($SATURNIN_APP_TAGLINE)"
  410. echo -n " $SATURNIN_APP_VERSION"
  411. test -n "$SATURNIN_APP_CODENAME" \
  412. && echo -n " - $SATURNIN_APP_CODENAME"
  413. echo
  414. echo -n "Powered by Saturnin (__MKIT_PROJ_TAGLINE__)"
  415. echo -n " __MKIT_PROJ_VERSION__"
  416. echo -n " - __MKIT_PROJ_CODENAME__"
  417. echo
  418. return "$EXIT_OK"
  419. }
  420. saturnin__wraphook() {
  421. #
  422. # Wrap command $@ in hooks 'pre' and 'post'
  423. #
  424. # Run pre hook, then command $@, then post hook. Always exit with
  425. # status of the payload command, even if hooks fail. Ignore post-hook
  426. # if payload command failed.
  427. #
  428. local es=0 # exit status of thid function
  429. saturnin__runhook pre
  430. "$@" || return $?
  431. es=$?
  432. saturnin__runhook post
  433. return $es
  434. }
  435. # # that what you see below this line #
  436. # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
  437. # # use in your code to anger the divine #
  438. _saturnin__conf__merge() {
  439. #
  440. # Take paths and applying merge strategy, load file(s)
  441. #
  442. local path
  443. local found=false
  444. while read -r path;
  445. do
  446. found=true
  447. case $Strategy in
  448. first)
  449. debug "winner: $path"
  450. cat "$path"
  451. cat >/dev/null # throw away rest of paths
  452. ;;
  453. join)
  454. echo "# file: ${path/$HOME/~}"
  455. cat "$path" 2>/dev/null
  456. ;;
  457. esac
  458. done
  459. $found
  460. }
  461. _saturnin__conf__load() {
  462. #
  463. # Print contents of files specified in $@
  464. #
  465. # Each argument means possible file candidate. If candidate contains
  466. # slash, it's treated as file path and is printed directly. If it's
  467. # single dash, standard input is copied.
  468. #
  469. # In all other cases, filename is searched in all elements of variable
  470. # SATURNIN_CONF_PATH; output then depends on chosen $Strategy: with
  471. # 'first' strategy, first existing file is printed, with 'join'
  472. # strategy, all existing files are printed.
  473. #
  474. local arg es
  475. es=0
  476. for arg in "$@";
  477. do
  478. case $arg in
  479. -|*/*) # stdin, or path (with slash)
  480. cat "$arg" || es=3
  481. ;;
  482. *) # name given, find all its incarnations
  483. saturnin__conf_find "$arg" \
  484. | _saturnin__conf__merge; es=$?
  485. ;;
  486. esac
  487. done
  488. return $es
  489. }
  490. _saturnin__conf_usage() {
  491. #
  492. # Show usage message and exit
  493. #
  494. PRETTY_USAGE="self=${0##*/} conf" \
  495. mkusage "$@" \
  496. "[options] [-e] SECTION.KEY [FNAME]" \
  497. "[options] -r SECTION.KEY [FNAME]" \
  498. "[options] -K SECTION [FNAME]" \
  499. "[options] -P FNAME" \
  500. "[options] -S FNAME" \
  501. -- \
  502. "Use inigrep to query config files." \
  503. -o \
  504. "-j join all files before applying query" \
  505. "-1 ensure single line is returned" \
  506. -c \
  507. "-e use normal mode (default)" \
  508. "-r use raw mode (preserves RHS whitespace and some comments)"\
  509. "-K list available keys in SECTION" \
  510. "-S list available sections in FNAME" \
  511. "-P list available keypaths (SECTION.KEY) in FNAME" \
  512. -- \
  513. "FNAME is filename, which is then searched on all paths specified"\
  514. "in SATURNIN_CONF_PATH and depending on -j parameter, first one" \
  515. "wins or all are joined. If FNAME contains slash, this search is"\
  516. "not done and FNAME is taken as path to file that is then" \
  517. "queried." \
  518. "" \
  519. "If FNAME is omitted, it is inferred from SECTION (e.g. 'foo.ini'"\
  520. "if 'foo.bar' was section name; note that section name itself may"\
  521. "contain dot)."
  522. }
  523. _saturnin__nl2colon() {
  524. #
  525. # Convert newline-based list of paths to colon:based:list
  526. #
  527. # Empty paths must not be included in the resulting list, so we need to
  528. # drop them and also get the colons right.
  529. #
  530. local idx=0 # current item index (zero-based)
  531. local path # each path on stdin
  532. while read -r path;
  533. do
  534. test -z "$path" && continue
  535. test $idx -gt 0 && echo -n ':'
  536. echo -n "$path"
  537. ((idx++))
  538. done
  539. }
  540. #shellfu module-version: __MKIT_PROJ_VERSION__