saturnin.sh.skel 16KB

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