saturnin.sh 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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: $*"; return 2 ;;
  94. *.*:*) ig_mode=-e; ig_query=$1; shift 1; break ;;
  95. *) warn "bad syntax: $*"; return 2 ;;
  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__help() {
  140. saturnin__usage --wanted
  141. }
  142. saturnin__lssc() {
  143. #
  144. # List subcommands
  145. #
  146. echo conf
  147. find "$SATURNIN_LIBEXEC" \
  148. -mindepth 1 \
  149. -maxdepth 1 \
  150. -executable \
  151. | sed -e "s|^.*/||; s|^$SATURNIN_LIBEXEC_PREFIX||" \
  152. | sort
  153. }
  154. saturnin__main() {
  155. local subcommand
  156. while true; do case $1 in
  157. -d|--debug) export PRETTY_DEBUG=true; shift ;;
  158. -v|--verbose) export PRETTY_VERBOSE=true; shift ;;
  159. -h|--help) saturnin__help; exit ;;
  160. --version) saturnin__version; exit ;;
  161. --version-semver) saturnin__get app-version ;;
  162. --saturnin-get-*) saturnin__get "$1" ;;
  163. -*) saturnin__usage; ;;
  164. --*) saturnin__usage; ;;
  165. --) shift; break ;;
  166. "") saturnin__usage; ;;
  167. *) break; ;;
  168. esac done
  169. subcommand="$1"; shift
  170. debug -v SHELLFU_PATH SATURNIN_LIBEXEC SATURNIN_CONF_PATH
  171. case "$subcommand" in
  172. conf) saturnin__conf "$@" ;;
  173. _ls_sc) saturnin__lssc ;;
  174. _lsfun) shellfu-get lsfun ;;
  175. _lsmod) shellfu-get lsmod ;;
  176. *) saturnin__runsc "$subcommand" "$@" ;;
  177. esac
  178. }
  179. saturnin__conf_mkpath() {
  180. #
  181. # Assemble SATURNIN_CONF_PATH from locations $@
  182. #
  183. # For each location, print colon-delimited list of
  184. # directories. If location ends with "/ini.d", list of
  185. # subfolders, sorted by C locale is printed--this allows
  186. # for modular configuration. Otherwise the location
  187. # is printed. Non-existent or non-directory locations
  188. # are silently ignored.
  189. #
  190. local location # one location argument
  191. local path # one path listed
  192. for location in "$@";
  193. do
  194. test -d "$location" || continue
  195. case "$location" in
  196. */ini.d) # modular location--sort subfolders
  197. find -L "$location" -mindepth 1 -maxdepth 1 -type d \
  198. | LC_ALL=C sort
  199. ;;
  200. *)
  201. echo "$location"
  202. ;;
  203. esac
  204. done \
  205. | _saturnin__nl2colon
  206. }
  207. saturnin__runhook() {
  208. #
  209. # Run custom hook
  210. #
  211. local hname="$1"
  212. local hook_code
  213. test -n "$SATURNIN_SUBCOMMAND" || {
  214. warn "unknown subcommand, ignoring hook: $hname"
  215. return 0
  216. }
  217. hook_code="$(saturnin__conf -j "hook.$SATURNIN_SUBCOMMAND.$hname")"
  218. debug -v SATURNIN_SUBCOMMAND hook_code hname
  219. bash -n <<<"$hook_code" || {
  220. warn "syntax errors, ignoring hook: $hname"
  221. return 0
  222. }
  223. eval "$hook_code"
  224. }
  225. saturnin__runsc() {
  226. #
  227. # Run subcommand $SATURNIN_SUBCOMMAND
  228. #
  229. local subcommand="$1"; shift
  230. local binpath # path to subcommand's binary
  231. binpath+="$SATURNIN_LIBEXEC/"
  232. binpath+="$SATURNIN_LIBEXEC_PREFIX$subcommand"
  233. debug -v binpath
  234. debug "\$*='$*'"
  235. test -x "$binpath" || {
  236. warn "invalid sub-command: $subcommand"
  237. saturnin__usage
  238. }
  239. SATURNIN_SUBCOMMAND="$subcommand" "$binpath" "$@"
  240. }
  241. saturnin__usage() {
  242. local es_param # exit-status related arguments
  243. test "$1" == "--wanted" && es_param="-e 0"
  244. #shellcheck disable=SC2086 disable=SC2046
  245. mkusage $es_param \
  246. "[options] COMMAND [ARG...]" \
  247. -o \
  248. "-d, --debug turn on debugging" \
  249. "-h, --help show this help message and exit"\
  250. "-v, --verbose turn on verbosity" \
  251. "--version show version and exit" \
  252. -c \
  253. $(saturnin__lssc)
  254. }
  255. saturnin__version() {
  256. #
  257. # Print version info
  258. #
  259. local tagline=${SATURNIN_APP_TAGLINE:-Some app with default tagline}
  260. local maybe_codename=""
  261. test -n "$SATURNIN_APP_CODENAME" && maybe_codename=" - $SATURNIN_APP_CODENAME"
  262. echo "$(basename "$0") ($tagline) $SATURNIN_APP_VERSION$maybe_codename"
  263. return "$EXIT_OK"
  264. }
  265. saturnin__wraphook() {
  266. #
  267. # Wrap command "$@" in hooks
  268. #
  269. # Run pre hook, then "$@", then post hook. Always exit
  270. # with status of "$@", even if hooks fail. Ignore
  271. # post-hook if "$@" failed.
  272. #
  273. local es=0
  274. saturnin__runhook pre
  275. "$@" || return $?
  276. es=$?
  277. saturnin__runhook post
  278. return $es
  279. }
  280. # # that what you see below this line #
  281. # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
  282. # # use in your code to anger the divine #
  283. _saturnin__conf__merge() {
  284. #
  285. # Take paths and applying merge strategy, load file(s)
  286. #
  287. local path
  288. local found=false
  289. while read -r path;
  290. do
  291. test -f "$path" || continue
  292. found=true
  293. case $Strategy in
  294. first)
  295. debug "winner: $path"
  296. cat "$path"
  297. cat >/dev/null # throw away rest of paths
  298. ;;
  299. join)
  300. echo "# file: ${path/$HOME/~}"
  301. cat "$path" 2>/dev/null
  302. ;;
  303. esac
  304. done
  305. $found
  306. }
  307. _saturnin__conf__load() {
  308. #
  309. # Print contents of files specified in $@
  310. #
  311. # Each argument means possible file candidate. If candidate
  312. # contains slash, it's treated as file path and is printed
  313. # directly. If it's single dash, standard input is copied.
  314. #
  315. # In all other cases, filename is searched in all elements
  316. # of SATURNIN_CONF_PATH; output then depends on chosen $Strategy:
  317. # with 'first' strategy, first existing file is printed, with
  318. # 'join' strategy. all existing files are printed.
  319. #
  320. local arg trydir trypath es
  321. es=0
  322. for arg in "$@";
  323. do
  324. case $arg in
  325. -|*/*) # stdin, or path (with slash)
  326. cat "$arg" || es=3
  327. ;;
  328. *) # name given, find all its incarnations
  329. debug -v SATURNIN_CONF_PATH
  330. echos "$SATURNIN_CONF_PATH" \
  331. | tr ':' '\n' \
  332. | while read -r trydir;
  333. do
  334. test -n "$trydir" || continue
  335. trypath="$trydir/$arg"
  336. echos "$trypath"
  337. done \
  338. | _saturnin__conf__merge; es=$?
  339. ;;
  340. esac
  341. done
  342. return $es
  343. }
  344. _saturnin__conf_usage() {
  345. #
  346. #
  347. #
  348. mkusage "[options] [-e] SECTION.KEY [FILE...]" \
  349. "[options] -r SECTION.KEY [FILE...]" \
  350. "[options] -K SECTION [FILE...]" \
  351. "[options] -P [FILE...]" \
  352. "[options] -S [FILE...]" \
  353. -o \
  354. "-j join all files before applying query" \
  355. "-1 ensure single line is returned" \
  356. -c \
  357. "-e use normal mode (default)" \
  358. "-r use raw mode (preserves RHS whitespace and some comments)" \
  359. "-K list available keys in SECTION" \
  360. "-S list available sections" \
  361. "-P list available keypaths (SECTION.KEY)" \
  362. -- \
  363. "Uses inigrep to query config files. SECTION is section name (may"\
  364. "contain dots itself, KEY is key name. Unless FILE is provided," \
  365. "filename is inferred from SECTION (eg. 'foo.ini' if 'foo.bar' was"\
  366. "sectionname) and searched in folders given in SATURNIN_CONF_PATH."\
  367. "If FILE contains slash, no search is done and just single FILE is"\
  368. "used instead."
  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. }