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:
parent
89010fd209
commit
613ef65f67
4 changed files with 194 additions and 33 deletions
|
|
@ -38,11 +38,10 @@
|
||||||
|
|
||||||
readonly NUX_RUNNER_BIN_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
|
readonly NUX_RUNNER_BIN_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
|
||||||
source $NUX_RUNNER_BIN_DIR/../inc/nux-base.inc.sh
|
source $NUX_RUNNER_BIN_DIR/../inc/nux-base.inc.sh
|
||||||
|
|
||||||
|
|
||||||
readonly NUX_RUNNER=$NUX_RUNNER_BIN_DIR/nux-runner;
|
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*:
|
## Additional commands provided by *nux-runner*:
|
||||||
|
|
@ -90,7 +89,15 @@ task.help() {
|
||||||
### local, global, dist
|
### local, global, dist
|
||||||
###
|
###
|
||||||
task.config() {
|
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.() {
|
task.() {
|
||||||
|
|
|
||||||
24
bin/nuxfs
24
bin/nuxfs
|
|
@ -134,9 +134,9 @@ function task.init {
|
||||||
nux.log debug "User specified template is " $TEMPLATE
|
nux.log debug "User specified template is " $TEMPLATE
|
||||||
if [ $(echo $TEMPLATE | grep -c "$SUFFIX\$") -eq 0 ]; then
|
if [ $(echo $TEMPLATE | grep -c "$SUFFIX\$") -eq 0 ]; then
|
||||||
nux.log debug "Template is common template."
|
nux.log debug "Template is common template."
|
||||||
TEMPLATE=$TEMPLATE_DIR/$TEMPLATE$SUFFIX
|
TEMPLATE=$(nux.cfg.get.path --first templates/${TEMPLATE}${SUFFIX})
|
||||||
if [ ! -e $TEMPLATE ]; then
|
if [ -z $TEMPLATE ]; then
|
||||||
echo -n "Template '$1' does not exists. "
|
echo "Template '$1' does not exists. "
|
||||||
nuxfs.template.list
|
nuxfs.template.list
|
||||||
return;
|
return;
|
||||||
fi;
|
fi;
|
||||||
|
|
@ -150,8 +150,8 @@ function task.init {
|
||||||
local template_relative=$(realpath -Lms $TEMPLATE --relative-to $(pwd))
|
local template_relative=$(realpath -Lms $TEMPLATE --relative-to $(pwd))
|
||||||
nux.log debug "Creating link $SUFFIX: target is '$template_relative'";
|
nux.log debug "Creating link $SUFFIX: target is '$template_relative'";
|
||||||
ln -s "$template_relative" "$SUFFIX"
|
ln -s "$template_relative" "$SUFFIX"
|
||||||
nux.log debug "Invoking create task."
|
nux.log debug "Invoking apply task."
|
||||||
task.create
|
nuxr.run apply
|
||||||
}
|
}
|
||||||
|
|
||||||
## template - List available templates
|
## template - List available templates
|
||||||
|
|
@ -237,15 +237,11 @@ function task.capture {
|
||||||
}
|
}
|
||||||
|
|
||||||
function nuxfs.template.list {
|
function nuxfs.template.list {
|
||||||
echo "Available templates:";
|
nux.log trace "Templates folders are: $(nux.cfg.get.path templates)"
|
||||||
pushd $TEMPLATE_DIR > /dev/null;
|
for _dir in $(nux.cfg.get.path templates) ; do
|
||||||
for i in $TEMPLATE_FILTER; do
|
find $_dir -maxdepth 1 -iname "*.nuxfs"
|
||||||
local TEMPLATE_ID=$(basename $i $SUFFIX)
|
done | xargs -n1 basename --suffix $SUFFIX | sort | uniq
|
||||||
echo -n " $TEMPLATE_ID - " ;
|
|
||||||
grep -m1 "^name " $i | sed -e "s/name *//"
|
|
||||||
echo
|
|
||||||
done;
|
|
||||||
popd > /dev/null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ function nuxr.task.help {
|
||||||
else
|
else
|
||||||
nuxr.help.task.comment "$NUX_SCRIPT" "$command" \
|
nuxr.help.task.comment "$NUX_SCRIPT" "$command" \
|
||||||
|| nuxr.help.task.comment "$NUX_RUNNER" "$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
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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:
|
## #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
|
function nux.cfg.write {
|
||||||
## Returns global path., otherwise provides specified.
|
store="$1";
|
||||||
##
|
nux.log trace "Store is: $1";
|
||||||
function nux.cfg.global.dir {
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue