saturnin.sh.skel 17KB

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