# HG changeset patch # User Tero Marttila # Date 1325766511 -7200 # Node ID ab661ceed4dca254804f106eb6b953b3a9c0ed5a # Parent 8ed4a750d1338b7a33c9a4665a6685cd32231f83 pkvlm-create: logging and command-line options diff -r 8ed4a750d133 -r ab661ceed4dc pkvlm-create --- a/pkvlm-create Wed Dec 28 11:04:02 2011 +0200 +++ b/pkvlm-create Thu Jan 05 14:28:31 2012 +0200 @@ -1,39 +1,148 @@ #!/bin/bash -### Parameters -## Options -_MOCK=${_MOCK:0} -_ACK=${_ACK:0} +### Initialize +set -u +set -e +TESTING= + +DO_VIRTINSTALL= + +scripts=$(dirname $0)/scripts +. $scripts/lib.sh + +### Command-line input +## Command-line options +function _help () { + cat <&2 +} + +function log_info () { + [ $LOG_INFO ] && echo "--- $@" >&2 +} + +function log_cmd () { + [ $LOG_CMD ] && echo ">>> $@" >&2 +} + +function log_warn () { + [ $LOG_WARN ] && echo "XXX $@" >&2 +} + +function log_error () { + [ $LOG_ERROR ] && echo "!!! $@" >&2 +} + function die () { - echo "!!! $@" >&2 + log_error "$@" exit 1 } # Execute command verbosely, and exit on failure +CMD_MOCK= +CMD_PROMPT=y + function cmd () { - echo ">>> $@" + log_cmd "$@" - [ $_MOCK ] && return 0 + [ $CMD_MOCK ] && return 0 eval "$@" # return $? } +function cmd_confirm () { + [ $CMD_PROMPT ] && read -p "Confirm: $1" + + cmd "$@" +} + function expand_MB () { local size=${1^} @@ -67,6 +102,22 @@ ) } +function expand_line () { + local line=$1 + + # evaluate {...} expressions + # a slight hack, but it works \o/ + # http://stackoverflow.com/questions/415677/how-to-replace-placeholders-in-a-text-file/7633579#7633579 + line="${line//\\/\\\\}" + line="${line//\"/\\\"}" + line="${line//\`/\\\`}" + line="${line//\$/\\\$}" + line="${line//{/\${}" # This is just for vim: } " + + # log_debug "($line)" >&2 + eval "echo \"$line\"" +} + function expand_template () { local tpl=$1 local out=$2 @@ -82,17 +133,7 @@ echo "$line" else - # evaluate {...} expressions - # a slight hack, but it works \o/ - # http://stackoverflow.com/questions/415677/how-to-replace-placeholders-in-a-text-file/7633579#7633579 - line="${line//\\/\\\\}" - line="${line//\"/\\\"}" - line="${line//\`/\\\`}" - line="${line//\$/\\\$}" - line="${line//{/\${}" # This is just for vim: } " - - # echo "($line)" >&2 - (eval "echo \"$line\"") || die "Error at $tpl:$linecount: $line" + expand_line "$line" || die "Error at $tpl:$linecount: $line" fi done < $tpl > $out @@ -110,15 +151,15 @@ local target=$dst/$name if [ -d $path ]; then - echo "expand_tree: $path -> $target" - echo "expand_tree $path $target $filter" + log_debug "expand_tree: $path -> $target" + expand_tree $path $target $filter elif [ -f $path ]; then - echo "expand_file: $path -> $target" - echo "expand_file $path $target" + log_debug "expand_file: $path -> $target" + expand_file $path $target else - echo "XXX: ignore weird file: $path" + log_warn "ignore weird file: $path" fi done