瀏覽代碼

feat(run_policy): Set flags and options for "run_policy"

IN order to be able to use flags and options, I used 'getoptions'
 (ref: https://github.com/ko1nksm/getoptions), so to generate script,
just run:
~/.local/bin/gengetoptions embed run_policy.sh.template > run_policy.sh

Signed-off-by: Jeremy MAURO <jeremy.mauro@gmail.com>
Jeremy MAURO 2 年之前
父節點
當前提交
6273549638
共有 2 個文件被更改,包括 141 次插入4 次删除
  1. 101 4
      scripts/run_policy.sh
  2. 40 0
      scripts/run_policy.sh.template

+ 101 - 4
scripts/run_policy.sh

@@ -1,14 +1,111 @@
 #!/usr/bin/env bash
 
+# @getoptions
+parser_definition() {
+	setup	REST	help:usage	--			"Usage: run_policy [options]... [arguments]"	''
+	msg	--	'Options:'
+	flag	FLAG_D	-d		--debug	   init:=0	--	"displays debug information"
+	flag	DRY_RUN	-n		--dry-run  init:=0	--	"run a dry run"
+	param	POLICY	-p		--policy base:"default"	--	"Policy to run (default: 'base')"
+	disp	:usage	-h		--help
+}
+
+# @gengetoptions parser -i parser_definition parse
+# Generated by getoptions (BEGIN)
+# URL: https://github.com/ko1nksm/getoptions (v3.3.0)
+FLAG_D='0'
+DRY_RUN='0'
+POLICY=''
+REST=''
+parse() {
+  OPTIND=$(($#+1))
+  while OPTARG= && [ $# -gt 0 ]; do
+    case $1 in
+      --?*=*) OPTARG=$1; shift
+        eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'}
+        ;;
+      --no-*|--without-*) unset OPTARG ;;
+      -[p]?*) OPTARG=$1; shift
+        eval 'set -- "${OPTARG%"${OPTARG#??}"}" "${OPTARG#??}"' ${1+'"$@"'}
+        ;;
+      -[dnh]?*) OPTARG=$1; shift
+        eval 'set -- "${OPTARG%"${OPTARG#??}"}" -"${OPTARG#??}"' ${1+'"$@"'}
+        OPTARG= ;;
+    esac
+    case $1 in
+      '-d'|'--debug')
+        [ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break
+        eval '[ ${OPTARG+x} ] &&:' && OPTARG='1' || OPTARG=''
+        FLAG_D="$OPTARG"
+        ;;
+      '-n'|'--dry-run')
+        [ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break
+        eval '[ ${OPTARG+x} ] &&:' && OPTARG='1' || OPTARG=''
+        DRY_RUN="$OPTARG"
+        ;;
+      '-p'|'--policy')
+        [ $# -le 1 ] && set "required" "$1" && break
+        OPTARG=$2
+        POLICY="$OPTARG"
+        shift ;;
+      '-h'|'--help')
+        usage
+        exit 0 ;;
+      --)
+        shift
+        while [ $# -gt 0 ]; do
+          REST="${REST} \"\${$(($OPTIND-$#))}\""
+          shift
+        done
+        break ;;
+      [-]?*) set "unknown" "$1"; break ;;
+      *)
+        REST="${REST} \"\${$(($OPTIND-$#))}\""
+    esac
+    shift
+  done
+  [ $# -eq 0 ] && { OPTIND=1; unset OPTARG; return 0; }
+  case $1 in
+    unknown) set "Unrecognized option: $2" "$@" ;;
+    noarg) set "Does not allow an argument: $2" "$@" ;;
+    required) set "Requires an argument: $2" "$@" ;;
+    pattern:*) set "Does not match the pattern (${1#*:}): $2" "$@" ;;
+    notcmd) set "Not a command: $2" "$@" ;;
+    *) set "Validation error ($1): $2" "$@"
+  esac
+  echo "$1" >&2
+  exit 1
+}
+usage() {
+cat<<'GETOPTIONSHERE'
+Usage: run_policy [options]... [arguments]
+
+Options:
+  -d, --debug                 displays debug information
+  -n, --dry-run               run a dry run
+  -p, --policy POLICY         Policy to run (default: 'base')
+  -h, --help                  
+GETOPTIONSHERE
+}
+# Generated by getoptions (END)
+# @end
+
+parse "$@"
+
+eval "set -- ${REST}"
+
 # Ref:
 # 	https://medium.com/@emachnic/using-policyfiles-with-chef-client-local-mode-4f47477b24db
-./scripts/export_policy.sh
+./scripts/export_policy.sh ${POLICY}
 pushd policy-export > /dev/null
-if [ "$1" = "dry" ]; then
+if [ ${DRY_RUN} -eq 1 ]; then
 	OPT="--why-run"
-elif [ "$1" = "debug" ]; then
-	OPT="--log_level debug"
 fi
+
+if [ ${FLAG_D} -eq 1 ]; then
+	OPT="${OPT} --log_level debug"
+fi
+
 bundle exec chef-client --chef-license 'accept' -z ${OPT}
 popd > /dev/null
 ./scripts/end_run.sh

+ 40 - 0
scripts/run_policy.sh.template

@@ -0,0 +1,40 @@
+#!/usr/bin/env bash
+
+# To generate script:
+#	~/.local/bin/gengetoptions embed run_policy.sh.template > run_policy.sh
+
+# @getoptions
+parser_definition() {
+	setup	REST	help:usage	--			"Usage: run_policy [options]... [arguments]"	''
+	msg	--	'Options:'
+	flag	FLAG_D	-d		--debug	   init:=0	--	"displays debug information"
+	flag	DRY_RUN	-n		--dry-run  init:=0	--	"run a dry run"
+	param	POLICY	-p		--policy base:"default"	--	"Policy to run (default: 'base')"
+	disp	:usage	-h		--help
+}
+
+# @gengetoptions parser -i parser_definition parse
+#
+#     INSERTED HERE
+#
+# @end
+
+parse "$@"
+
+eval "set -- ${REST}"
+
+# Ref:
+# 	https://medium.com/@emachnic/using-policyfiles-with-chef-client-local-mode-4f47477b24db
+./scripts/export_policy.sh ${POLICY}
+pushd policy-export > /dev/null
+if [ ${DRY_RUN} -eq 1 ]; then
+	OPT="--why-run"
+fi
+
+if [ ${FLAG_D} -eq 1 ]; then
+	OPT="${OPT} --log_level debug"
+fi
+
+bundle exec chef-client --chef-license 'accept' -z ${OPT}
+popd > /dev/null
+./scripts/end_run.sh