saturnin.sh.skel 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 named $1 from respective configuration section
  296. #
  297. # Will load joined multi-line key "hook.$SATURNIN_SUBCOMMAND.$1",
  298. # and execute it as Bash code.
  299. #
  300. local hname="$1"
  301. local hook_code
  302. test -n "$SATURNIN_SUBCOMMAND" || {
  303. warn "unknown subcommand, ignoring hook: $hname"
  304. return 0
  305. }
  306. hook_code="$(saturnin__conf -j "hook.$SATURNIN_SUBCOMMAND.$hname")"
  307. debug -v SATURNIN_SUBCOMMAND hook_code hname
  308. bash -n <<<"$hook_code" || {
  309. warn "syntax errors, ignoring hook: $hname"
  310. return 0
  311. }
  312. eval "$hook_code"
  313. }
  314. saturnin__runsc() {
  315. #
  316. # Run subcommand $SATURNIN_SUBCOMMAND
  317. #
  318. local subcommand="$1"; shift
  319. local binpath # path to subcommand's binary
  320. binpath+="$SATURNIN_LIBEXEC/"
  321. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  322. debug -v binpath
  323. debug "\$*='$*'"
  324. test -x "$binpath" || {
  325. warn "invalid sub-command: $subcommand"
  326. saturnin__usage
  327. }
  328. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  329. }
  330. saturnin__usage() {
  331. #
  332. # Show usage message and exit
  333. #
  334. #shellcheck disable=SC2046
  335. mkusage "$@" \
  336. "[options] COMMAND [ARG...]" \
  337. -o \
  338. "-d, --debug turn on debugging" \
  339. "-h, --help show this help message and exit"\
  340. "-v, --verbose turn on verbosity" \
  341. "--version show version and exit" \
  342. -c \
  343. $(saturnin__lssc)
  344. }
  345. saturnin__version() {
  346. #
  347. # Print version info
  348. #
  349. local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
  350. local maybe_codename=""
  351. test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
  352. echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
  353. return "$EXIT_OK"
  354. }
  355. saturnin__wraphook() {
  356. #
  357. # Wrap command "$@" in hooks
  358. #
  359. # Run pre hook, then "$@", then post hook. Always exit
  360. # with status of "$@", even if hooks fail. Ignore
  361. # post-hook if "$@" failed.
  362. #
  363. local es=0
  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
  401. # contains slash, it's treated as file path and is printed
  402. # directly. If it's single dash, standard input is copied.
  403. #
  404. # In all other cases, filename is searched in all elements
  405. # of SATURNIN_CONF_PATH; output then depends on chosen $Strategy:
  406. # with 'first' strategy, first existing file is printed, with
  407. # 'join' 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 queried."\
  452. "" \
  453. "If FNAME is omitted, it is inferred from SECTION (e.g. .'foo.ini'"\
  454. "if 'foo.bar' was section name; note that section name may contain"\
  455. "dot)."
  456. }
  457. _saturnin__nl2colon() {
  458. #
  459. # Convert newline-based list of paths to colon:based:list
  460. #
  461. # Empty paths must not be included in the resulting list,
  462. # so we need to drop them and also get the colons right.
  463. #
  464. local idx=0 # current item index (zero-based)
  465. local path
  466. while read -r path;
  467. do
  468. test -z "$path" && continue
  469. test $idx -gt 0 && echo -n ':'
  470. echo -n "$path"
  471. ((idx++))
  472. done
  473. }
  474. #shellfu module-version: __MKIT_PROJ_VERSION__