saturnin.sh 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #!/bin/bash
  2. shellfu import exit
  3. shellfu import inigrep
  4. shellfu import pretty
  5. #
  6. # Path where saturnin__conf should look for files
  7. #
  8. # If filename does not contain slash, it is looked up in each
  9. # (or all, based on strategy--see saturnin__conf() doc) path in this
  10. # list. The list is colon-separated and non-dirs as well as
  11. # empty strings are silently ignored.
  12. #
  13. SATURNIN_CONF_PATH="${SATURNIN_CONF_PATH:-}"
  14. #
  15. # Expected config filename extension (for guessing from path head)
  16. #
  17. # If no filename to read is given, saturnin__conf() will guess
  18. # filename as the path head plus this suffix (e.g. `foo.ini` for
  19. # `saturnin__conf foo.bar.baz`)
  20. #
  21. SATURNIN_CONF_SUFFIX="${SATURNIN_CONF_SUFFIX:-.ini}"
  22. saturnin__conf() {
  23. #
  24. # inigrep smart loader
  25. #
  26. # Usage:
  27. # saturnin__conf [-j] [inigrep-query] [-- [file]..]
  28. #
  29. #
  30. # File arguments
  31. # ==============
  32. #
  33. # If omitted, *file* argument is inferred by taking part of
  34. # kpath name before first dot and appending value of
  35. # `$SATURNIN_CONF_SUFFIX`, (".ini" by default).
  36. #
  37. # Each *file* argument is then processed as follows:
  38. #
  39. # * `-` (single dash) is interpreted as reading from
  40. # STDIN.
  41. #
  42. # * If argument contains slash, it is expanded as a regular
  43. # path (relative or absolute).
  44. #
  45. # * Otherwise, it is taken as filename and searched for
  46. # in directories given in `$SATURNIN_CONF_PATH`. (This can
  47. # yield more than one path, which is equivalent as if
  48. # all paths were provided.)
  49. #
  50. # Not all files expanded based on `$SATURNIN_CONF_PATH`
  51. # are read by default; reading is governed by "merge
  52. # strategy": the default strategy "first" reads only
  53. # the first existing file.
  54. #
  55. # "join" strategy on the other hand, means that any
  56. # files are simply concatenated and prefixed with
  57. # comment (visible only in raw mode) containing path
  58. # to the file.
  59. #
  60. # This means that if a section is queried that is
  61. # present in both files, it is effectively concatenated
  62. # as well.
  63. #
  64. # Following calls are equivalent
  65. #
  66. # saturnin__conf foo.bar.baz
  67. # saturnin__conf foo.bar.baz foo.ini
  68. #
  69. # and both result in reading of key *baz* from section *foo.bar*
  70. # in file *foo.ini*, which is selected from *SATURNIN_CONF_PATH*.
  71. # Should there be more foo.ini's, the first is selected.
  72. # Using `-j` switch
  73. #
  74. # saturnin__conf -j foo.bar.baz
  75. #
  76. # would cause all foo.ini's on *SATURNIN_CONF_PATH* be
  77. # concatenated instead.
  78. #
  79. local ig_mode # retrieval mode
  80. local ig_query # keypath or section name (when listing keys)
  81. local ig_limit # line limit
  82. local files=() # file specification
  83. local Strategy=first # merge strategy
  84. while true; do case $1:$2 in
  85. "":*) break ;;
  86. -j:*) Strategy=join; shift 1 ;;
  87. -1:*) ig_limit=$1; shift 1 ;;
  88. -e:*.*) ig_mode=$1; ig_query=$2; shift 2; break ;;
  89. -r:*.*) ig_mode=$1; ig_query=$2; shift 2; break ;;
  90. -K:*) ig_mode=$1; ig_query=$2; shift 2; break ;;
  91. -S:*) ig_mode=$1; ig_query=""; shift 1; break ;;
  92. -P:*) ig_mode=$1; ig_query=""; shift 1; break ;;
  93. .*:*) warn "bad syntax: $*"; _saturnin__conf_usage ;;
  94. *.*:*) ig_mode=-e; ig_query=$1; shift 1; break ;;
  95. *) warn "bad syntax: $*"; _saturnin__conf_usage ;;
  96. esac done
  97. test -n "$ig_mode" || { warn "could not determine inigrep mode"; _saturnin__conf_usage; }
  98. debug -v ig_limit ig_query ig_mode Strategy
  99. if test -n "$*";
  100. then
  101. files=("$@")
  102. elif test -n "$ig_query";
  103. then
  104. files=("${ig_query%%.*}$SATURNIN_CONF_SUFFIX")
  105. else
  106. warn "dunno what to load"
  107. _saturnin__conf_usage
  108. fi
  109. debug -v files
  110. #shellcheck disable=SC2086
  111. _saturnin__conf__load "${files[@]}" | inigrep $ig_limit $ig_mode "$ig_query"
  112. return "${PIPESTATUS[0]}"
  113. }
  114. saturnin__get() {
  115. #
  116. # Show Saturnin Internal info by key $1 and exit
  117. #
  118. # Key $1 can be whole `--saturnin-get-stuff` argument or just
  119. # the part after `--saturnin-get-`.
  120. #
  121. # This is aimed to help debugging and testing the app (or
  122. # Saturnin itself) by showing packaging and deployment related
  123. # info.
  124. #
  125. local key=${1#--saturnin-get-}
  126. case "$key" in
  127. shellfu-path) echo "$SHELLFU_PATH" ;;
  128. saturnin-conf-path) echo "$SATURNIN_CONF_PATH" ;;
  129. app-git-hash) echo "$SATURNIN_APP_GIT_HASH" ;;
  130. app-version) echo "$SATURNIN_APP_VERSION" ;;
  131. cache-home) echo "$SATURNIN_CACHE_HOME" ;;
  132. libexec) echo "$SATURNIN_LIBEXEC" ;;
  133. libexec-prefix) echo "$SATURNIN_LIBEXEC_PREFIX" ;;
  134. *) warn "unknown devel key: $key"
  135. exit "$EXIT_USAGE" ;;
  136. esac
  137. exit "$EXIT_OK"
  138. }
  139. saturnin__lssc() {
  140. #
  141. # List subcommands
  142. #
  143. echo conf
  144. find "$SATURNIN_LIBEXEC" \
  145. -mindepth 1 \
  146. -maxdepth 1 \
  147. -executable \
  148. | sed -e "s|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
  149. | sort
  150. }
  151. saturnin__main() {
  152. local subcommand
  153. while true; do case $1 in
  154. -d|--debug) export PRETTY_DEBUG=true; shift ;;
  155. -v|--verbose) export PRETTY_VERBOSE=true; shift ;;
  156. -h|--help) saturnin__usage -e 0; exit ;;
  157. --version) saturnin__version; exit ;;
  158. --version-semver) saturnin__get app-version ;;
  159. --saturnin-get-*) saturnin__get "$1" ;;
  160. -*) saturnin__usage; ;;
  161. --*) saturnin__usage; ;;
  162. --) shift; break ;;
  163. "") saturnin__usage; ;;
  164. *) break; ;;
  165. esac done
  166. subcommand="$1"; shift
  167. debug -v SHELLFU_PATH SATURNIN_LIBEXEC SATURNIN_CONF_PATH
  168. case "$subcommand" in
  169. conf) saturnin__conf "$@" ;;
  170. _ls_sc) saturnin__lssc ;;
  171. _lsfun) shellfu-get lsfun ;;
  172. _lsmod) shellfu-get lsmod ;;
  173. *) saturnin__runsc "$subcommand" "$@" ;;
  174. esac
  175. }
  176. saturnin__conf_mkpath() {
  177. #
  178. # Assemble SATURNIN_CONF_PATH from locations $@
  179. #
  180. # For each location, print colon-delimited list of
  181. # directories. If location ends with "/ini.d", list of
  182. # subfolders, sorted by C locale is printed--this allows
  183. # for modular configuration. Otherwise the location
  184. # is printed. Non-existent or non-directory locations
  185. # are silently ignored.
  186. #
  187. local location # one location argument
  188. local path # one path listed
  189. for location in "$@";
  190. do
  191. test -d "$location" || continue
  192. case "$location" in
  193. */ini.d) # modular location--sort subfolders
  194. find -L "$location" -mindepth 1 -maxdepth 1 -type d \
  195. | LC_ALL=C sort
  196. ;;
  197. *)
  198. echo "$location"
  199. ;;
  200. esac
  201. done \
  202. | _saturnin__nl2colon
  203. }
  204. saturnin__runhook() {
  205. #
  206. # Run custom hook
  207. #
  208. local hname="$1"
  209. local hook_code
  210. test -n "$SATURNIN_SUBCOMMAND" || {
  211. warn "unknown subcommand, ignoring hook: $hname"
  212. return 0
  213. }
  214. hook_code="$(saturnin__conf -j "hook.$SATURNIN_SUBCOMMAND.$hname")"
  215. debug -v SATURNIN_SUBCOMMAND hook_code hname
  216. bash -n <<<"$hook_code" || {
  217. warn "syntax errors, ignoring hook: $hname"
  218. return 0
  219. }
  220. eval "$hook_code"
  221. }
  222. saturnin__runsc() {
  223. #
  224. # Run subcommand $SATURNIN_SUBCOMMAND
  225. #
  226. local subcommand="$1"; shift
  227. local binpath # path to subcommand's binary
  228. binpath+="$SATURNIN_LIBEXEC/"
  229. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  230. debug -v binpath
  231. debug "\$*='$*'"
  232. test -x "$binpath" || {
  233. warn "invalid sub-command: $subcommand"
  234. saturnin__usage
  235. }
  236. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  237. }
  238. saturnin__usage() {
  239. #
  240. # Show usage message and exit
  241. #
  242. #shellcheck disable=SC2046
  243. mkusage "$@" \
  244. "[options] COMMAND [ARG...]" \
  245. -o \
  246. "-d, --debug turn on debugging" \
  247. "-h, --help show this help message and exit"\
  248. "-v, --verbose turn on verbosity" \
  249. "--version show version and exit" \
  250. -c \
  251. $(saturnin__lssc)
  252. }
  253. saturnin__version() {
  254. #
  255. # Print version info
  256. #
  257. local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
  258. local maybe_codename=""
  259. test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
  260. echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
  261. return "$EXIT_OK"
  262. }
  263. saturnin__wraphook() {
  264. #
  265. # Wrap command "$@" in hooks
  266. #
  267. # Run pre hook, then "$@", then post hook. Always exit
  268. # with status of "$@", even if hooks fail. Ignore
  269. # post-hook if "$@" failed.
  270. #
  271. local es=0
  272. saturnin__runhook pre
  273. "$@" || return $?
  274. es=$?
  275. saturnin__runhook post
  276. return $es
  277. }
  278. # # that what you see below this line #
  279. # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
  280. # # use in your code to anger the divine #
  281. _saturnin__conf__merge() {
  282. #
  283. # Take paths and applying merge strategy, load file(s)
  284. #
  285. local path
  286. local found=false
  287. while read -r path;
  288. do
  289. test -f "$path" || continue
  290. found=true
  291. case $Strategy in
  292. first)
  293. debug "winner: $path"
  294. cat "$path"
  295. cat >/dev/null # throw away rest of paths
  296. ;;
  297. join)
  298. echo "# file: ${path/$HOME/~}"
  299. cat "$path" 2>/dev/null
  300. ;;
  301. esac
  302. done
  303. $found
  304. }
  305. _saturnin__conf__load() {
  306. #
  307. # Print contents of files specified in $@
  308. #
  309. # Each argument means possible file candidate. If candidate
  310. # contains slash, it's treated as file path and is printed
  311. # directly. If it's single dash, standard input is copied.
  312. #
  313. # In all other cases, filename is searched in all elements
  314. # of SATURNIN_CONF_PATH; output then depends on chosen $Strategy:
  315. # with 'first' strategy, first existing file is printed, with
  316. # 'join' strategy. all existing files are printed.
  317. #
  318. local arg trydir trypath es
  319. es=0
  320. for arg in "$@";
  321. do
  322. case $arg in
  323. -|*/*) # stdin, or path (with slash)
  324. cat "$arg" || es=3
  325. ;;
  326. *) # name given, find all its incarnations
  327. debug -v SATURNIN_CONF_PATH
  328. echos "$SATURNIN_CONF_PATH" \
  329. | tr ':' '\n' \
  330. | while read -r trydir;
  331. do
  332. test -n "$trydir" || continue
  333. trypath="$trydir/$arg"
  334. echos "$trypath"
  335. done \
  336. | _saturnin__conf__merge; es=$?
  337. ;;
  338. esac
  339. done
  340. return $es
  341. }
  342. _saturnin__conf_usage() {
  343. #
  344. # Show usage message and exit
  345. #
  346. PRETTY_USAGE="self=${0##*/} conf" \
  347. mkusage "[options] [-e] SECTION.KEY [FNAME]" \
  348. "[options] -r SECTION.KEY [FNAME]" \
  349. "[options] -K SECTION [FNAME]" \
  350. "[options] -P FNAME" \
  351. "[options] -S FNAME" \
  352. -- \
  353. "Use inigrep to query config files." \
  354. -o \
  355. "-j join all files before applying query" \
  356. "-1 ensure single line is returned" \
  357. -c \
  358. "-e use normal mode (default)" \
  359. "-r use raw mode (preserves RHS whitespace and some comments)" \
  360. "-K list available keys in SECTION" \
  361. "-S list available sections in FNAME" \
  362. "-P list available keypaths (SECTION.KEY) in FNAME" \
  363. -- \
  364. "FNAME is filename, which is then searched on all paths specified" \
  365. "in SATURNIN_CONF_PATH and depending on -j parameter, first one" \
  366. "wins or all are joined. If FNAME contains slash, this search is" \
  367. "not done and FNAME is taken as path to file that is then queried."\
  368. "" \
  369. "If FNAME is omitted, it is inferred from SECTION (e.g. .'foo.ini'"\
  370. "if 'foo.bar' was section name; note that section name may contain"\
  371. "dot)."
  372. }
  373. _saturnin__nl2colon() {
  374. #
  375. # Convert newline-based list of paths to colon:based:list
  376. #
  377. # Empty paths must not be included in the resulting list,
  378. # so we need to drop them and also get the colons right.
  379. #
  380. local idx=0 # current item index (zero-based)
  381. local path
  382. while read -r path;
  383. do
  384. test -z "$path" && continue
  385. test $idx -gt 0 && echo -n ':'
  386. echo -n "$path"
  387. ((idx++))
  388. done
  389. }