1
1
Fork 0
mirror of https://github.com/tonydamage/nux-env.git synced 2025-12-11 13:24:28 +01:00

nux.cfg: Introduced configuration engine.

Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
This commit is contained in:
Tony Tkáčik 2017-06-20 18:25:04 +02:00
parent 89010fd209
commit 613ef65f67
4 changed files with 194 additions and 33 deletions

View file

@ -38,11 +38,10 @@
readonly NUX_RUNNER_BIN_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
source $NUX_RUNNER_BIN_DIR/../inc/nux-base.inc.sh
readonly NUX_RUNNER=$NUX_RUNNER_BIN_DIR/nux-runner;
nux.include nux-runner
nux.use nux-runner
nux.use nux.cfg
##
## Additional commands provided by *nux-runner*:
@ -90,7 +89,15 @@ task.help() {
### local, global, dist
###
task.config() {
nux.notimplemented task.config
nux.log trace "global is" $(nux.cfg.dir.global)
nux.log trace "dist is" $(nux.cfg.dir.dist)
nux.log trace "local is" $(nux.cfg.dir.local)
if [ "$#" -ge 3 ] ; then
nux.cfg.write "$@"
else
nux.cfg.read "$@";
fi
}
task.() {

View file

@ -134,9 +134,9 @@ function task.init {
nux.log debug "User specified template is " $TEMPLATE
if [ $(echo $TEMPLATE | grep -c "$SUFFIX\$") -eq 0 ]; then
nux.log debug "Template is common template."
TEMPLATE=$TEMPLATE_DIR/$TEMPLATE$SUFFIX
if [ ! -e $TEMPLATE ]; then
echo -n "Template '$1' does not exists. "
TEMPLATE=$(nux.cfg.get.path --first templates/${TEMPLATE}${SUFFIX})
if [ -z $TEMPLATE ]; then
echo "Template '$1' does not exists. "
nuxfs.template.list
return;
fi;
@ -150,8 +150,8 @@ function task.init {
local template_relative=$(realpath -Lms $TEMPLATE --relative-to $(pwd))
nux.log debug "Creating link $SUFFIX: target is '$template_relative'";
ln -s "$template_relative" "$SUFFIX"
nux.log debug "Invoking create task."
task.create
nux.log debug "Invoking apply task."
nuxr.run apply
}
## template - List available templates
@ -237,15 +237,11 @@ function task.capture {
}
function nuxfs.template.list {
echo "Available templates:";
pushd $TEMPLATE_DIR > /dev/null;
for i in $TEMPLATE_FILTER; do
local TEMPLATE_ID=$(basename $i $SUFFIX)
echo -n " $TEMPLATE_ID - " ;
grep -m1 "^name " $i | sed -e "s/name *//"
echo
done;
popd > /dev/null;
nux.log trace "Templates folders are: $(nux.cfg.get.path templates)"
for _dir in $(nux.cfg.get.path templates) ; do
find $_dir -maxdepth 1 -iname "*.nuxfs"
done | xargs -n1 basename --suffix $SUFFIX | sort | uniq
}

View file

@ -29,7 +29,7 @@ function nuxr.task.help {
else
nuxr.help.task.comment "$NUX_SCRIPT" "$command" \
|| nuxr.help.task.comment "$NUX_RUNNER" "$command" \
|| echo "Help topic $1 not found. Run $0 help to see topics."
|| echo "Help topic $1 not found. Run '$NUX_APPNAME help' to see topics."
fi
}

View file

@ -1,26 +1,184 @@
## #nux.cfg - NUX Script configuration management library
## #nux.cfg - NUX Script Configuration management library
##
## *nux.cfg* provides basic configuration management using yaml as configuration
## store and merging configuration from *local*, *global* and *dist* configuration.
##
## The configuration management library tries to have *sane defaults* (which
## are overrideable):
##
## - configuration file is allways *config.yaml*
## - configuration locations are (overrideable):
## - *local* - by default same as global
## - *global* - ~/.config/{app-name}/
## - *dist* - {app-path}/config
## - value is tried to be read first from *local*, then *global*, then *dist* configuration.
##
## #Usage
##
## Basic usage of nux.cfg is really simple:
## - use *nux.cfg.read* function to read configuration value
## - use *nux.cfg.write* to write configuration value.
##
## If you are using *nux-runner* for your scripts, it automaticly add *config*
## task which can be used by user to read / modify configuration values.
##
## For more advanced use see *Public functions* section describing each function.
##
##
nux_cfg_config_file=config.yaml
## #Public functions:
##
## nux.cfg.include [<app>] <config>
## Includes app specific configuration file.
## nux.cfg.read:: [<store>] <path>
## Reads configuration value stored at *path* from specified *store*.
## If no store is specified returns first found value in all stores
## in following order *local, global, dist*.
##
function nux.cfg.include {
:
function nux.cfg.read {
nux.log trace "Reading configuration $@"
maybe_store="$1";
local read_from="local global dist"
case "$maybe_store" in
global ) ;&
local ) ;&
dist )
shift;
read_from=$maybe_store
;;
* ) ;;
esac
nux.log trace "Reading from $read_from"
for store in $read_from ; do
value=$(nux.cfg.read.direct "$(nux.cfg.file.$store)" "$@")
if [ -n "$value" ] ; then
echo $value;
break;
fi
done
}
## nux.cfg.write:: <global | local> <path> <value>
## Writes specified value to *global* or *local* store.
## If configuration file does not exists, it creates it.
##
## nux.cfg.global.path
## Returns global path., otherwise provides specified.
##
function nux.cfg.global.dir {
:
function nux.cfg.write {
store="$1";
nux.log trace "Store is: $1";
case $store in
global ) ;;
local ) ;;
dist )
nux.fatal "Write to dist store is disabled."
;;
* )
nux.fatal "Unknown config store $1".
;;
esac
shift;
nux.cfg.write.direct "$(nux.cfg.file.$store)" "$@"
}
function nux.cfg.dist.dir
:
## nux.cfg.dir.global::
## Returns path of *global* config directory. May be overriden
## by library for customization of path format.
##
function nux.cfg.dir.global {
echo "$HOME/.config/$NUX_APPNAME"
}
## nux.cfg.dir.dist::
## Returns path of *dist* config directory. SHOULD be overriden
## by library for customization of path format.
function nux.cfg.dir.dist {
echo "$NUX_ENV_DIR/config/$NUX_APPNAME"
}
## nux.cfg.dir.local::
## Returns path of *local* config directory. SHOULD be overriden
## by library for customization of path format.
##
function nux.cfg.dir.local {
nux.cfg.dir.global
}
function nux.cfg.list {
:
## nux.cfg.file.global::
## nux.cfg.file.local::
## nux.cfg.file.dist::
## Returns path of *global* config file. Default implementation appends
## *config.yaml* to the respective path. May be overriden if main config
## file is determined in different way.
##
function nux.cfg.file.global {
echo $(nux.cfg.dir.global)/$nux_cfg_config_file
}
function nux.cfg.file.dist {
echo $(nux.cfg.dir.dist)/$nux_cfg_config_file
}
function nux.cfg.file.local {
echo $(nux.cfg.dir.local)/$nux_cfg_config_file
}
function nux.cfg.read.direct {
local file="$1";shift
nux.log trace "Direct read from $file";
local path="$@";
if nux.check.file.exists "$file"; then
if [ -n "$path" ]; then
value=$(yaml r "$file" "$path")
if [ "$value" != null ]; then
echo "$value"
fi
else
cat "$file";
fi
fi
}
function nux.cfg.write.direct {
file="$1";
if ! nux.check.file.exists "$1" ; then
mkdir -p "$(dirname "$file")";
touch "$file";
fi
shift;
yaml w "$file" "$@" -i
}
function nux.cfg.get.path {
nux.log trace "Reading configuration $@"
local only_first=""
if [ "$1" = "--first" ]; then
only_first="$1"; shift;
fi
maybe_store="$1";
local read_from="local global dist"
case "$maybe_store" in
global ) ;&
local ) ;&
dist )
shift;
read_from=$maybe_store
;;
* ) ;;
esac
nux.log trace "Reading from $read_from"
for store in $read_from ; do
path="$(nux.cfg.dir.$store)/$@"
nux.log trace "Testing $path"
if [ -e "$path" ] ; then
echo $path;
if [ -n "$only_first" ]; then
break;
fi
fi
done
}