All of lore.kernel.org
 help / color / mirror / Atom feed
From: George Dunlap <george.dunlap@eu.citrix.com>
To: xen-devel@lists.xen.org
Cc: George Dunlap <george.dunlap@eu.citrix.com>,
	Stefano Stabellini <stefano.stabellini@citrix.com>
Subject: [PATCH 1/8] Add core.sh and wrapper function
Date: Thu, 9 Apr 2015 20:29:45 +0100	[thread overview]
Message-ID: <1428607792-13418-2-git-send-email-george.dunlap@eu.citrix.com> (raw)
In-Reply-To: <1428607792-13418-1-git-send-email-george.dunlap@eu.citrix.com>

Add core functionality and an executable to run it

Signed-off-by: George Dunlap <george.dunlap@eu.citrix.com>
---
CC: Stefano Stabellini <stefano.stabellini@citrix.com>
---
 lib/core.sh | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 raise       |  16 +++++++
 2 files changed, 163 insertions(+)
 create mode 100644 lib/core.sh
 create mode 100755 raise

diff --git a/lib/core.sh b/lib/core.sh
new file mode 100644
index 0000000..46b02e0
--- /dev/null
+++ b/lib/core.sh
@@ -0,0 +1,147 @@
+#!/usr/bin/env bash
+
+RAISIN_HELP=()
+
+# Core calling convention functionality
+# 
+# $arg_parse
+#
+#   Parse $@.  Begin with every element that a '=' in it being treated
+#   as a variable assignment: declare the LHS a local variable and set
+#   it to the RHS.  Once you reach an element without a '=' in it, or
+#   a '--', put all further elements into the array args.
+# 
+# $requireargs VARNAME1 [VARNAME2, ...]
+#
+#   Check to see that VARNAME1 is defined, and throw an error if not.
+#
+# default VARNAME "VALUE" ; $default_post
+#
+#   Check to see is VARNAME is defined; if not, declare it a local
+#   variable and set it to VALUE.
+#
+# Example usage:
+#
+#   function log()
+#   {
+#       local i;
+#
+#       $arg_parse
+#
+#       default tmpdir "/tmp"; $default_post
+#
+#       $requireargs filename
+#
+#       for $i in ${args[@]} ; do
+#           echo "$i" > $tmpdir/$filename
+#       done
+#
+#       [[ -n "$flower" ] && echo $flower
+#   }
+
+arg_parse_cmd=\
+"local -a args;
+local _a;
+local _vn;
+local _m;
+
+_m=true;
+
+for _a in \"\$@\" ; do
+    false && echo \"Evaluating \${_a} [[ \"\${_a/=}\" = \"\${_a}\" ]]\";
+    if \$_m && [[ \"\${_a/=}\" != \"\${_a}\" ]] ; then
+        false && echo Parameter;
+        _vn=\${_a%%=*};
+        eval \"local \$_vn\";
+        eval \"\$_a\";
+    elif \$_m && [[ \"\${_a}\" == \"--\" ]] ; then
+        false && echo Separator;
+        _m=false;
+    else
+        false && echo Argument;
+        _m=false;
+        args+=(\"\$_a\");
+    fi;
+done"
+
+arg_parse="eval $arg_parse_cmd"
+
+# Pass in either the current function name, or the name of the script
+requireargs="eval _func=\"\$FUNCNAME\" ; eval [[ -n \\\"\$_func\\\" ]] || _func=\$0 ; eval _require-args \$_func"
+
+function _require-args()
+{
+    local _arg
+    local _args
+
+    _args=($@)
+
+    for _arg in ${_args[@]:1} ; do
+	eval "[[ -n \"\${$_arg}\" ]] || fail \"${_args[0]}: Missing $_arg\""
+    done
+}
+
+function default()
+{
+    # l0: eval i="5"
+    # l1: default_post="eval $1=\"$2\""
+    # l3: eval "if [[ -z \"\$$1\" ]] ; then default_post=\"eval \$1=\\\"$2\\\"\" ; fi"
+    eval "if [[ -z \"\$$1\" ]] ; then default_post=\"eval local \$1=\\\"$2\\\"\" ; else unset default_post ; fi"
+}
+
+function fail()
+{
+   echo FATAL "$@" 1>&2
+   [[ -n "$fail_cleanup" ]] && $fail_cleanup
+   exit 1
+}
+
+function info()
+{
+   echo INFO "$@" 1>&2
+}
+
+function error()
+{
+   echo ERROR "$@" 1>&2
+}
+
+RAISIN_HELP+=("help            List available commands")
+function help()
+{
+    echo "Usage: $0 command [arguments...]"
+    echo "  Run '$0 help' for a list of commands, or '$0 command-help' for"
+    echo "  help on a particular command."
+    echo
+
+    for i in "${RAISIN_HELP[@]}" ; do
+	echo "$i"
+    done
+
+    echo
+
+    exit 0
+}
+
+function cmdline()
+{
+    local cmd;
+
+    [[ "$#" -eq "0" ]] && help
+
+    $arg_parse
+
+    # If the command is not defined, then print help and exit
+    if ! type "${args[0]}" 2>/dev/null | grep -q 'function' ; then
+	echo "Unknown function: ${args[0]}"
+	echo
+	help
+    fi
+
+    info Running "${args[0]}"
+    "${args[0]}" "${args[@]:1}" || exit 1
+
+    if ! [[ -z "$RET" ]] ; then
+	echo $RET
+    fi
+}
diff --git a/raise b/raise
new file mode 100755
index 0000000..7f3faae
--- /dev/null
+++ b/raise
@@ -0,0 +1,16 @@
+#!/usr/bin/env bash
+
+# Include your defaults
+if [[ -e "./config" ]] ; then
+    . ./config
+fi
+
+# To use this as a library, set RAISIN_PATH appropriately
+[[ -z "$RAISIN_PATH" ]] && RAISIN_PATH="$PWD/lib"
+
+# Then as many as the sub-libraries as you need
+. ${RAISIN_PATH}/core.sh
+
+# And do your own thing rather than running commands
+# I suggest defining a "main" function of your own and running it like this.
+cmdline "$@"
-- 
1.9.1

  reply	other threads:[~2015-04-09 19:29 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-09 19:29 raisin: Refactor to be more like a library George Dunlap
2015-04-09 19:29 ` George Dunlap [this message]
2015-04-09 19:40   ` [PATCH 1/8] Add core.sh and wrapper function George Dunlap
2015-04-10 14:30     ` Stefano Stabellini
2015-04-10 14:29   ` Stefano Stabellini
2015-04-13 10:23     ` George Dunlap
2015-04-13 10:41     ` George Dunlap
2015-04-13 16:13       ` Stefano Stabellini
2015-04-14 13:55         ` Ian Campbell
2015-04-14 14:44           ` George Dunlap
2015-04-15  8:29             ` Ian Campbell
2015-04-09 19:29 ` [PATCH 2/8] Remove redundant "source" from component definitions George Dunlap
2015-04-10 11:05   ` Stefano Stabellini
2015-04-09 19:29 ` [PATCH 3/8] Move common-functions.sh and git-checkout.sh into lib George Dunlap
2015-04-10 11:05   ` Stefano Stabellini
2015-04-09 19:29 ` [PATCH 4/8] Import raise.sh and unraise.sh into library George Dunlap
2015-04-10 14:29   ` Stefano Stabellini
2015-04-09 19:29 ` [PATCH 5/8] Allow the user's config to live outside of git George Dunlap
2015-04-10 14:30   ` Stefano Stabellini
2015-04-09 19:29 ` [PATCH 6/8] xen: Replace iasl with acpica-tools George Dunlap
2015-04-10 11:05   ` Stefano Stabellini
2015-04-09 19:29 ` [PATCH 7/8] xen: Require wget George Dunlap
2015-04-10 11:06   ` Stefano Stabellini
2015-04-09 19:29 ` [PATCH 8/8] Refactor package dependency checking and installation George Dunlap
2015-04-10 14:30   ` Stefano Stabellini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1428607792-13418-2-git-send-email-george.dunlap@eu.citrix.com \
    --to=george.dunlap@eu.citrix.com \
    --cc=stefano.stabellini@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.