saturnin.sh 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. #shellcheck disable=SC2046
  240. mkusage "$@" \
  241. "[options] COMMAND [ARG...]" \
  242. -o \
  243. "-d, --debug turn on debugging" \
  244. "-h, --help show this help message and exit"\
  245. "-v, --verbose turn on verbosity" \
  246. "--version show version and exit" \
  247. -c \
  248. $(saturnin__lssc)
  249. }
  250. saturnin__version() {
  251. #
  252. # Print version info
  253. #
  254. local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
  255. local maybe_codename=""
  256. test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
  257. echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
  258. return "$EXIT_OK"
  259. }
  260. saturnin__wraphook() {
  261. #
  262. # Wrap command "$@" in hooks
  263. #
  264. # Run pre hook, then "$@", then post hook. Always exit
  265. # with status of "$@", even if hooks fail. Ignore
  266. # post-hook if "$@" failed.
  267. #
  268. local es=0
  269. saturnin__runhook pre
  270. "$@" || return $?
  271. es=$?
  272. saturnin__runhook post
  273. return $es
  274. }
  275. # # that what you see below this line #
  276. # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
  277. # # use in your code to anger the divine #
  278. _saturnin__conf__merge() {
  279. #
  280. # Take paths and applying merge strategy, load file(s)
  281. #
  282. local path
  283. local found=false
  284. while read -r path;
  285. do
  286. test -f "$path" || continue
  287. found=true
  288. case $Strategy in
  289. first)
  290. debug "winner: $path"
  291. cat "$path"
  292. cat >/dev/null # throw away rest of paths
  293. ;;
  294. join)
  295. echo "# file: ${path/$HOME/~}"
  296. cat "$path" 2>/dev/null
  297. ;;
  298. esac
  299. done
  300. $found
  301. }
  302. _saturnin__conf__load() {
  303. #
  304. # Print contents of files specified in $@
  305. #
  306. # Each argument means possible file candidate. If candidate
  307. # contains slash, it's treated as file path and is printed
  308. # directly. If it's single dash, standard input is copied.
  309. #
  310. # In all other cases, filename is searched in all elements
  311. # of SATURNIN_CONF_PATH; output then depends on chosen $Strategy:
  312. # with 'first' strategy, first existing file is printed, with
  313. # 'join' strategy. all existing files are printed.
  314. #
  315. local arg trydir trypath es
  316. es=0
  317. for arg in "$@";
  318. do
  319. case $arg in
  320. -|*/*) # stdin, or path (with slash)
  321. cat "$arg" || es=3
  322. ;;
  323. *) # name given, find all its incarnations
  324. debug -v SATURNIN_CONF_PATH
  325. echos "$SATURNIN_CONF_PATH" \
  326. | tr ':' '\n' \
  327. | while read -r trydir;
  328. do
  329. test -n "$trydir" || continue
  330. trypath="$trydir/$arg"
  331. echos "$trypath"
  332. done \
  333. | _saturnin__conf__merge; es=$?
  334. ;;
  335. esac
  336. done
  337. return $es
  338. }
  339. _saturnin__conf_usage() {
  340. #
  341. #
  342. #
  343. PRETTY_USAGE="self=${0##*/} conf" \
  344. mkusage "[options] [-e] SECTION.KEY [FNAME]" \
  345. "[options] -r SECTION.KEY [FNAME]" \
  346. "[options] -K SECTION [FNAME]" \
  347. "[options] -P FNAME" \
  348. "[options] -S FNAME" \
  349. -- \
  350. "Use inigrep to query config files." \
  351. -o \
  352. "-j join all files before applying query" \
  353. "-1 ensure single line is returned" \
  354. -c \
  355. "-e use normal mode (default)" \
  356. "-r use raw mode (preserves RHS whitespace and some comments)" \
  357. "-K list available keys in SECTION" \
  358. "-S list available sections in FNAME" \
  359. "-P list available keypaths (SECTION.KEY) in FNAME" \
  360. -- \
  361. "FNAME is filename, which is then searched on all paths specified" \
  362. "in SATURNIN_CONF_PATH and depending on -j parameter, first one" \
  363. "wins or all are joined. If FNAME contains slash, this search is" \
  364. "not done and FNAME is taken as path to file that is then queried."\
  365. "" \
  366. "If FNAME is omitted, it is inferred from SECTION (e.g. .'foo.ini'"\
  367. "if 'foo.bar' was section name; note that section name may contain"\
  368. "dot)."
  369. }
  370. _saturnin__nl2colon() {
  371. #
  372. # Convert newline-based list of paths to colon:based:list
  373. #
  374. # Empty paths must not be included in the resulting list,
  375. # so we need to drop them and also get the colons right.
  376. #
  377. local idx=0 # current item index (zero-based)
  378. local path
  379. while read -r path;
  380. do
  381. test -z "$path" && continue
  382. test $idx -gt 0 && echo -n ':'
  383. echo -n "$path"
  384. ((idx++))
  385. done
  386. }