saturnin.sh.skel 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. _ls_sc) saturnin__lssc ;;
  279. _lsfun) shellfu-get lsfun ;;
  280. _lsmod) shellfu-get lsmod ;;
  281. *) saturnin__runsc "$subcommand" "$@" ;;
  282. esac
  283. }
  284. _saturnin__cat_helpfile() {
  285. #
  286. # Print helpfile $1
  287. #
  288. local helpfile=$1 # path to help file
  289. cat "$helpfile" >&2 && return 0
  290. saturnin__bug "cannot print help file: $helpfile"
  291. return 3
  292. }
  293. saturnin__help() {
  294. #
  295. # Print meta-command help text
  296. #
  297. # See $SATURNIN_META_HELP for details.
  298. #
  299. local introline # introduction line
  300. introline=$(basename "$0")
  301. test -n "$SATURNIN_APP_TAGLINE" \
  302. && introline+=" - $SATURNIN_APP_TAGLINE"
  303. case "$SATURNIN_META_HELP" in
  304. "")
  305. echo "$introline"$'\n' >&2
  306. saturnin__usage -E -e 0
  307. ;;
  308. +/*)
  309. echo "$introline"$'\n' >&2
  310. saturnin__usage -E
  311. echo >&2
  312. _saturnin__cat_helpfile "${SATURNIN_META_HELP:1}"
  313. ;;
  314. =/*)
  315. _saturnin__cat_helpfile "${SATURNIN_META_HELP:1}"
  316. ;;
  317. *)
  318. echo "$introline"$'\n' >&2
  319. saturnin__usage -E
  320. saturnin__bug "malformed SATURNIN_META_HELP: $SATURNIN_META_HELP"
  321. return 3
  322. ;;
  323. esac
  324. }
  325. saturnin__conf_mkpath() {
  326. #
  327. # Assemble SATURNIN_CONF_PATH from locations $@
  328. #
  329. # For each location, print colon-delimited list of directories. If
  330. # location ends with "/ini.d", list of subfolders, sorted by C locale is
  331. # printed--this allows for modular configuration. Otherwise the
  332. # location is printed. Non-existent or non-directory locations are
  333. # silently ignored.
  334. #
  335. local location # one location argument
  336. local path # one path listed
  337. for location in "$@";
  338. do
  339. test -d "$location" || continue
  340. case "$location" in
  341. */ini.d) # modular location--sort subfolders
  342. find -L "$location" -mindepth 1 -maxdepth 1 -type d \
  343. | LC_ALL=C sort
  344. ;;
  345. *)
  346. echo "$location"
  347. ;;
  348. esac
  349. done \
  350. | _saturnin__nl2colon
  351. }
  352. saturnin__runhook() {
  353. #
  354. # Run custom hook named $1 from respective configuration section
  355. #
  356. # Will load joined multi-line key "hook.$SATURNIN_SUBCOMMAND.$1" and
  357. # unless syntax check fails, execute it as Bash code (in separate
  358. # process).
  359. #
  360. local name="$1" # hook name
  361. local code # ... code
  362. test -n "$SATURNIN_SUBCOMMAND" || {
  363. warn "unknown subcommand, ignoring hook: $name"
  364. return 0
  365. }
  366. code="$(saturnin__conf -j "hook.$SATURNIN_SUBCOMMAND.$name")"
  367. debug -v SATURNIN_SUBCOMMAND code name
  368. bash -n <<<"$code" || {
  369. warn "syntax errors, ignoring hook: $name"
  370. return 0
  371. }
  372. bash <<<"$code"
  373. }
  374. saturnin__runsc() {
  375. #
  376. # Run subcommand $SATURNIN_SUBCOMMAND
  377. #
  378. local subcommand="$1"; shift
  379. local binpath # path to subcommand's binary
  380. binpath+="$SATURNIN_LIBEXEC/"
  381. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  382. debug -v binpath
  383. debug "\$*='$*'"
  384. test -x "$binpath" || {
  385. warn "invalid sub-command: $subcommand"
  386. saturnin__usage
  387. }
  388. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  389. }
  390. saturnin__usage() {
  391. #
  392. # Show usage message and exit
  393. #
  394. #shellcheck disable=SC2046
  395. mkusage "$@" \
  396. "[options] COMMAND [ARG...]" \
  397. -o \
  398. "-D, --full-debug turn on gory debugging" \
  399. "-V, --version show version and exit" \
  400. "-d, --debug turn on debugging" \
  401. "-h, --help show this help message and exit"\
  402. "-v, --verbose turn on verbosity" \
  403. -c \
  404. $(saturnin__lssc)
  405. }
  406. saturnin__version() {
  407. #
  408. # Print version info
  409. #
  410. echo -n "$(basename "$0")"
  411. test -n "$SATURNIN_APP_TAGLINE" \
  412. && echo -n " ($SATURNIN_APP_TAGLINE)"
  413. echo -n " $SATURNIN_APP_VERSION"
  414. test -n "$SATURNIN_APP_CODENAME" \
  415. && echo -n " - $SATURNIN_APP_CODENAME"
  416. echo
  417. echo -n "Powered by Saturnin (__MKIT_PROJ_TAGLINE__)"
  418. echo -n " __MKIT_PROJ_VERSION__"
  419. echo -n " - __MKIT_PROJ_CODENAME__"
  420. echo
  421. return "$EXIT_OK"
  422. }
  423. saturnin__wraphook() {
  424. #
  425. # Wrap command $@ in hooks 'pre' and 'post'
  426. #
  427. # Run pre hook, then command $@, then post hook. Always exit with
  428. # status of the payload command, even if hooks fail. Ignore post-hook
  429. # if payload command failed.
  430. #
  431. local es=0 # exit status of thid function
  432. saturnin__runhook pre
  433. "$@" || return $?
  434. es=$?
  435. saturnin__runhook post
  436. return $es
  437. }
  438. # # that what you see below this line #
  439. # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
  440. # # use in your code to anger the divine #
  441. _saturnin__conf__merge() {
  442. #
  443. # Take paths and applying merge strategy, load file(s)
  444. #
  445. local path
  446. local found=false
  447. while read -r path;
  448. do
  449. found=true
  450. case $Strategy in
  451. first)
  452. debug "winner: $path"
  453. cat "$path"
  454. cat >/dev/null # throw away rest of paths
  455. ;;
  456. join)
  457. echo "# file: ${path/$HOME/~}"
  458. cat "$path" 2>/dev/null
  459. ;;
  460. esac
  461. done
  462. $found
  463. }
  464. _saturnin__conf__load() {
  465. #
  466. # Print contents of files specified in $@
  467. #
  468. # Each argument means possible file candidate. If candidate contains
  469. # slash, it's treated as file path and is printed directly. If it's
  470. # single dash, standard input is copied.
  471. #
  472. # In all other cases, filename is searched in all elements of variable
  473. # SATURNIN_CONF_PATH; output then depends on chosen $Strategy: with
  474. # 'first' strategy, first existing file is printed, with 'join'
  475. # strategy, all existing files are printed.
  476. #
  477. local arg es
  478. es=0
  479. for arg in "$@";
  480. do
  481. case $arg in
  482. -|*/*) # stdin, or path (with slash)
  483. cat "$arg" || es=3
  484. ;;
  485. *) # name given, find all its incarnations
  486. saturnin__conf_find "$arg" \
  487. | _saturnin__conf__merge; es=$?
  488. ;;
  489. esac
  490. done
  491. return $es
  492. }
  493. _saturnin__conf_usage() {
  494. #
  495. # Show usage message and exit
  496. #
  497. PRETTY_USAGE="self=${0##*/} conf" \
  498. mkusage "$@" \
  499. "[options] [-e] SECTION.KEY [FNAME]" \
  500. "[options] -r SECTION.KEY [FNAME]" \
  501. "[options] -K SECTION [FNAME]" \
  502. "[options] -P FNAME" \
  503. "[options] -S FNAME" \
  504. -- \
  505. "Use inigrep to query config files." \
  506. -o \
  507. "-j join all files before applying query" \
  508. "-1 ensure single line is returned" \
  509. -c \
  510. "-e use normal mode (default)" \
  511. "-r use raw mode (preserves RHS whitespace and some comments)"\
  512. "-K list available keys in SECTION" \
  513. "-S list available sections in FNAME" \
  514. "-P list available keypaths (SECTION.KEY) in FNAME" \
  515. -- \
  516. "FNAME is filename, which is then searched on all paths specified"\
  517. "in SATURNIN_CONF_PATH and depending on -j parameter, first one" \
  518. "wins or all are joined. If FNAME contains slash, this search is"\
  519. "not done and FNAME is taken as path to file that is then" \
  520. "queried." \
  521. "" \
  522. "If FNAME is omitted, it is inferred from SECTION (e.g. 'foo.ini'"\
  523. "if 'foo.bar' was section name; note that section name itself may"\
  524. "contain dot)."
  525. }
  526. _saturnin__nl2colon() {
  527. #
  528. # Convert newline-based list of paths to colon:based:list
  529. #
  530. # Empty paths must not be included in the resulting list, so we need to
  531. # drop them and also get the colons right.
  532. #
  533. local idx=0 # current item index (zero-based)
  534. local path # each path on stdin
  535. while read -r path;
  536. do
  537. test -z "$path" && continue
  538. test $idx -gt 0 && echo -n ':'
  539. echo -n "$path"
  540. ((idx++))
  541. done
  542. }
  543. #shellfu module-version: __MKIT_PROJ_VERSION__