From e4ae60fdb0a72b8fc45855c64b98acf9a88e539c Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Thu, 24 Nov 2016 13:34:01 +0100 Subject: [PATCH] nuxfs: added init and templates support. Signed-off-by: Tony Tkacik --- bin/nuxfs | 59 ++++++++++++++++++++++++++++++++++++++++++-- inc/nuxfs.dsl.inc.sh | 12 ++++++--- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/bin/nuxfs b/bin/nuxfs index c144ef5..ac455ea 100755 --- a/bin/nuxfs +++ b/bin/nuxfs @@ -2,7 +2,10 @@ ### Filesystem Layout manager ### -STARTDIR=$(pwd) +local STARTDIR=$(pwd) +local TEMPLATE_DIR=$NUX_ENV_DIR/templates +local SUFFIX=".nuxfs" +local TEMPLATE_FILTER="*$SUFFIX" nux.include nuxfs nux.include nuxfs.dsl @@ -65,5 +68,57 @@ function task.create { popd > /dev/null; } - nuxfs.dsl.execute .nuxfs + nuxfs.dsl.execute $SUFFIX +} + +## init - Initializes a directory using template +function task.init { + if [ $# -eq 0 ]; then + nux.log debug "No template specified. Creating empty $SUFFIX definition." + touch .nuxfs + return; + fi + local TEMPLATE=$1 + 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. " + nuxfs.template.list + return; + fi; + + fi; + nux.log debug "Using '$TEMPLATE' to initialize." + if [ ! -e $TEMPLATE ]; then + echo "nuxfs template $1 does not exists." + return; + fi; + 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 +} + +## +## template - List available templates +# +function task.template { + nux.log debug $NUX_ENV_DIR + + nuxfs.template.list +} + +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; } diff --git a/inc/nuxfs.dsl.inc.sh b/inc/nuxfs.dsl.inc.sh index 33324f3..ca047ed 100644 --- a/inc/nuxfs.dsl.inc.sh +++ b/inc/nuxfs.dsl.inc.sh @@ -6,12 +6,12 @@ function exec.if.function { shift; shift; if nux.check.function $FUNC_NAME; then - nux.log debug Executing: $FUNC_NAME "$@"; + nux.log trace Executing: $FUNC_NAME "$@"; $FUNC_NAME "$@"; return; fi if nux.check.function $DEFAULT_NAME; then - nux.log debug Executing: $FUNC_NAME "$@"; + nux.log trace Executing: $FUNC_NAME "$@"; $DEFAULT_NAME "$@"; return; fi @@ -43,6 +43,9 @@ function nuxfs.dsl.keywords { origin() { : } + name() { + : + } dir() { nuxfs.dsl.command directory "$@"; } @@ -71,6 +74,7 @@ function nuxfs.dsl.keywords { nux.log debug "Directory exists '$1'" nux.log trace "Trying to nest into directory" if test -e "$1/.nuxfs"; then + nux.log debug "Invoking nested nuxfs definition." source "$1/.nuxfs"; fi; else @@ -89,9 +93,9 @@ function nuxfs.dsl.execute { nuxfs.dsl.keywords declare -a DIR_ARRAY DIR_ARRAY[0]=. - if test -f "$1"; then + if test -e "$1"; then source $1; else - error "$1": Definition file does not exists. + nuxfs.error "$1" Definition file does not exists. fi }