]> git.droids-corp.org - protos/libecoli.git/commitdiff
move shell common functions in a lib
authorOlivier Matz <zer0@droids-corp.org>
Thu, 7 Mar 2019 18:16:42 +0000 (19:16 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Thu, 7 Mar 2019 18:16:42 +0000 (19:16 +0100)
examples/parse-yaml/parse-yaml.sh
libshell/libecoli.sh [new file with mode: 0644]

index 1b7d02938836cd96a9cc87a717dec3eb790cd635..9d5a680cfe19bd4338b99fccfb77160013aaf4ac 100644 (file)
@@ -2,86 +2,53 @@
 
 set -e
 
-# use a safer version of echo (no option)
-echo()
-{
-       printf "%s\n" "$*"
-}
+parse_yaml=ecoli-parse-yaml
+libecoli=/usr/share/libecoli/ecoli.sh
 
-debug()
+usage()
 {
-       echo "$@" >&2
-}
+        cat <<- END_OF_HELP
+        This example demonstrates how to build an interactive command
+        line in a shell script, using a description in yaml format.
 
-# $1: node sequence number (ex: ec_node4)
-ec_parse_get_first_child()
-{
-       local first_child=${1}_first_child
-       echo $(eval 'echo ${'$first_child'}')
-}
-
-# $1: node sequence number (ex: ec_node4)
-ec_parse_get_next()
-{
-       local next=${1}_next
-       echo $(eval 'echo ${'$next'}')
+        Usage: $0 [options...] [tests...]
+            -h
+               show this help
+            -p <path>
+               path to ecoli-parse-yaml binary (default is to search
+               in PATH).
+            -l <path>
+               path to libecoli.sh library (default is $libecoli)
+END_OF_HELP
 }
 
-# $1: node sequence number (ex: ec_node4)
-ec_parse_iter_next()
-{
-       local seq=${1#ec_node}
-       seq=$((seq+1))
-       local next=ec_node${seq}
-       if [ "$(ec_parse_get_id $next)" != "" ]; then
-               echo $next
-       fi
-}
-
-# $1: node sequence number (ex: ec_node4)
-ec_parse_get_id()
-{
-       local id=${1}_id
-       echo $(eval 'echo ${'$id'}')
-}
+ARG=0
+while getopts "hp:l:" opt; do
+        case "$opt" in
+        "h")
+                usage
+                exit 0
+                ;;
+        "p")
+                parse_yaml="$OPTARG"
+                ;;
+        "l")
+                libecoli="$OPTARG"
+                ;;
+       esac
+done
 
-# $1: node sequence number (ex: ec_node4)
-ec_parse_get_strvec_len()
-{
-       local strvec_len=${1}_strvec_len
-       echo $(eval 'echo ${'$strvec_len'}')
-}
-
-# $1: node sequence number (ex: ec_node4)
-# $2: index in strvec
-ec_parse_get_str()
-{
-       if [ $# -ne 2 ]; then
-               return
-       fi
-       local str=${1}_str${2}
-       echo $(eval 'echo ${'$str'}')
-}
-
-# $1: node sequence number (ex: ec_node4)
-# $2: node id (string)
-ec_parse_find_first()
-{
-       if [ $# -ne 2 ]; then
-               return
-       fi
-       local node_seq=$1
-       while [ "$node_seq" != "" ]; do
-               local id=$(ec_parse_get_id $node_seq)
-               if [ "$id" = "$2" ]; then
-                       echo $node_seq
-                       return 0
-               fi
-               node_seq=$(ec_parse_iter_next $node_seq)
-       done
-}
+if ! command -v $parse_yaml > /dev/null 2> /dev/null && \
+               ! readlink -e $parse_yaml > /dev/null 2> /dev/null; then
+       echo "Cannot find ecoli-parse-yaml ($parse_yaml)"
+       exit 1
+fi
+if ! readlink -e $libecoli > /dev/null 2> /dev/null; then
+       echo "Cannot find libecoli.sh ($libecoli)"
+       exit 1
+fi
 
-path=$(dirname $0)
+. $libecoli
 
 yaml=$(mktemp)
 cat << EOF > $yaml
@@ -121,7 +88,7 @@ EOF
 
 output=$(mktemp)
 match=1
-$path/build/parse-yaml -i $yaml -o $output || match=0
+$parse_yaml -i $yaml -o $output || match=0
 if [ "$match" = "1" ]; then
        cat $output
        . $output
diff --git a/libshell/libecoli.sh b/libshell/libecoli.sh
new file mode 100644 (file)
index 0000000..fdb642d
--- /dev/null
@@ -0,0 +1,83 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright 2018, Olivier MATZ <zer0@droids-corp.org>
+
+# Source this file from a shell script to add libecoli helpers.
+
+# use a safer version of echo (no option)
+ec_echo()
+{
+       printf "%s\n" "$*"
+}
+
+ec_debug()
+{
+       ec_echo "$@" >&2
+}
+
+# $1: node sequence number (ex: ec_node4)
+ec_parse_get_first_child()
+{
+       local first_child=${1}_first_child
+       ec_echo $(eval 'ec_echo ${'$first_child'}')
+}
+
+# $1: node sequence number (ex: ec_node4)
+ec_parse_get_next()
+{
+       local next=${1}_next
+       ec_echo $(eval 'ec_echo ${'$next'}')
+}
+
+# $1: node sequence number (ex: ec_node4)
+ec_parse_iter_next()
+{
+       local seq=${1#ec_node}
+       seq=$((seq+1))
+       local next=ec_node${seq}
+       if [ "$(ec_parse_get_id $next)" != "" ]; then
+               ec_echo $next
+       fi
+}
+
+# $1: node sequence number (ex: ec_node4)
+ec_parse_get_id()
+{
+       local id=${1}_id
+       ec_echo $(eval 'ec_echo ${'$id'}')
+}
+
+# $1: node sequence number (ex: ec_node4)
+ec_parse_get_strvec_len()
+{
+       local strvec_len=${1}_strvec_len
+       ec_echo $(eval 'ec_echo ${'$strvec_len'}')
+}
+
+# $1: node sequence number (ex: ec_node4)
+# $2: index in strvec
+ec_parse_get_str()
+{
+       if [ $# -ne 2 ]; then
+               return
+       fi
+       local str=${1}_str${2}
+       ec_echo $(eval 'ec_echo ${'$str'}')
+}
+
+# $1: node sequence number (ex: ec_node4)
+# $2: node id (string)
+ec_parse_find_first()
+{
+       if [ $# -ne 2 ]; then
+               return
+       fi
+       local node_seq=$1
+       while [ "$node_seq" != "" ]; do
+               local id=$(ec_parse_get_id $node_seq)
+               if [ "$id" = "$2" ]; then
+                       ec_echo $node_seq
+                       return 0
+               fi
+               node_seq=$(ec_parse_iter_next $node_seq)
+       done
+}