Skip to content

Commit

Permalink
Dash parameter (#121)
Browse files Browse the repository at this point in the history
* Adding empty DEBUG checks

* Adding empty DEBUG check and implemented KEYS_LIST="$(build_keys_list $ROOT_KEY $ROOT_KEY_SCHEMA)"

* Adding shyaml get-values for selection list

* Selection-list converter added

* Dash-Param schema option added

* Double dash as dash parm default

* This was driving me nuts.

* Implemented parameter list, updated schema to reflect last to changes, updated README for localscript dev

* changed to use ascending numeric order

* final clean up

* example config files

* Removed testing / proofs in helm schema, added dash_type, parameter-list to terraform schema

* Fresh eyes - Adding /scripts to appropriate exports

* foo / bar example

* Adding adding in comments for old function and double dash default mention

* Changing echo outputs to use DEEP_DEBUG

* Updating Terraform configuration doc

* Adding example to yaml

* Cleaned up standalone script run

* I meet your resolution request and raise you optional args.

* EOD clean up

* Adding the root_schema_key to function call, and removing ENV set

* removing comments

* removed NOTES.md
  • Loading branch information
PhillypHenning authored Sep 8, 2021
1 parent a7565d4 commit 0ce4623
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 45 deletions.
11 changes: 11 additions & 0 deletions docs/example-config-files/helm.bitops.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
helm:
cli:
namespace: bitops
debug: false
atomic: true
options:
release-name: bitops-eks
kubeconfig:
fetch:
enabled: true
cluster-name: bitops-eks
9 changes: 9 additions & 0 deletions docs/example-config-files/terraform.bitops.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform:
cli:
backend-config:
- KEY1=foo
- KEY2=bar
options:
command: apply
version: "0.13.2"
fetch-kubeconfig: false
10 changes: 10 additions & 0 deletions docs/tool-configuration/configuration-terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ terraform:
cli:
var-file: my-vars.tfvars
target: terraform.module.resource
backend-config:
- KEY1=foo
- KEY2=bar
options:
command: apply
version: "0.13.2"
Expand All @@ -29,6 +32,13 @@ terraform:
* **Environment Variable:** `TF_TARGET`
* **default:** `""`

-------------------
### backend-config
* **BitOps Property:** `backend-config`
* **CLI Argument:** `--KEY1=foo --KEY2=bar`
* **Environment Variable:** ``
* **default:** `""`

-------------------

## Options Configuration
Expand Down
11 changes: 11 additions & 0 deletions scripts/bitops-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ nested:
foo: true
```

### Sample standalone scripts
```
export ENV_FILE=.env; \
export DEBUG=true; \
export DEEP_DEBUG=; \
export BITOPS_DIR=$(pwd); \
export SCRIPTS_DIR=$BITOPS_DIR/scripts; export SCHEMA_FILE=$SCRIPTS_DIR/terraform/bitops.schema.yaml; \
export BITOPS_CONFIG_FILE=$BITOPS_DIR/docs/example-config-files/terraform.bitops.config.yaml; \
$SCRIPTS_DIR/bitops-config/convert-schema.sh $SCHEMA_FILE $BITOPS_CONFIG_FILE
```

### Sample convert schema
```
docker run --rm --name bitops-local \
Expand Down
110 changes: 87 additions & 23 deletions scripts/bitops-config/convert-schema.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
#!/usr/bin/env bash
# convert-schema.sh --> get-convert --> get.sh --> shyaml
# | --> convert.sh --> converters/ (converter is choosen by the schema.type, defaults to string)

set -e

export BITOPS_DIR="/opt/bitops"
export SCRIPTS_DIR="$BITOPS_DIR/scripts"
if [ -z "$BITOPS_DIR" ];then
echo "Using default BitOps Directory"
export BITOPS_DIR="/opt/bitops"
fi

if [ -z "$SCRIPTS_DIR" ];then
echo "Using default BitOps Script Directory"
export SCRIPTS_DIR="/opt/bitops/scripts"
fi


POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"

case $key in
-r|--root-key)
ROOT_KEY=$2
shift # Pass over optional arguement
shift # Pass over optional arguement value
;;
-rs|--root-key-schema)
ROOT_KEY_SCHEMA=$2
shift # Pass over optional arguement
shift # Pass over optional arguement value
;;
*)
POSITIONAL+=("$1")
shift # Add Normal positionals back into place
;;
esac
done


set -- "${POSITIONAL[@]}" # Restore positional arguements

# -- #
SCHEMA_FILE="$1"
BITOPS_CONFIG_FILE="$2"
ROOT_KEY="$3"
ROOT_KEY_SCHEMA="$4"
# -- #
if [ -z "$ROOT_KEY" ];then
export ROOT_KEY="$3"
fi
# -- #
if [ -z "$ROOT_KEY_SCHEMA" ];then
export ROOT_KEY_SCHEMA="$4"
fi
# -- #


if [ -n "$DEEP_DEBUG" ]; then
echo "BITOPS SET TO: [$BITOPS_DIR]"
echo "SCRIPTS SET TO: [$SCRIPTS_DIR]"
echo "SCHEMA_FILESET TO: [$SCHEMA_FILE]"
echo "BITOPS_CONFIG_FILE SET TO: [$BITOPS_CONFIG_FILE]"
echo "ROOT_KEY SET TO: [$ROOT_KEY]"
echo "ROOT_KEY_SCHEMA SET TO: [$ROOT_KEY_SCHEMA]"
fi

KEYS_LIST=""

function get_schema_keys(){
Expand All @@ -22,36 +77,34 @@ function build_keys_list(){
local keys=""

keys="$(get_schema_keys ${rootkey_schema})"

while IFS= read -r value; do
# echo "RECEIVED: '$value'"

while IFS= read -r value; do
full_value_path="${value}"
full_value_path_schema="${value}"

if [ -n "$rootkey" ]; then
full_value_path="${rootkey}.${value}"
full_value_path_schema="${rootkey_schema}.${value}"
fi

type="$($SCRIPTS_DIR/bitops-config/get.sh $SCHEMA_FILE "${full_value_path_schema}.type")"

# if type is object, recurse
# else, add key path to final list
if [ "$type" == "object" ]; then
build_keys_list "${full_value_path}" "${full_value_path_schema}.properties"
else
KEYS_LIST="$KEYS_LIST
${full_value_path},${full_value_path_schema}"
echo "$full_value_path,$full_value_path_schema"
fi
done <<< "$keys"
}

# TODO
# KEYS_LIST="$(build_keys_list)"
build_keys_list "$ROOT_KEY" "$ROOT_KEY_SCHEMA"


# OLD USAGE
# build_keys_list "$ROOT_KEY" "$ROOT_KEY_SCHEMA"
# This has been changed to the line below
KEYS_LIST="$(build_keys_list $ROOT_KEY $ROOT_KEY_SCHEMA)"
if [ -n "$DEEP_DEBUG" ]; then
echo "Keys List: [$KEYS_LIST]"
fi

script_options=""
while IFS= read -r value; do
Expand All @@ -63,33 +116,44 @@ while IFS= read -r value; do
full_value_path="${array[0]}"
full_value_path_schema="${array[1]}"

if [ -n "$DEEP_DEBUG" ]; then
echo "full_value_path=$full_value_path"
echo "full_value_path_schema=$full_value_path_schema"
fi

type="$($SCRIPTS_DIR/bitops-config/get.sh $SCHEMA_FILE "${full_value_path_schema}.type")"
parameter="$($SCRIPTS_DIR/bitops-config/get.sh $SCHEMA_FILE "${full_value_path_schema}.parameter")"
terminal="$($SCRIPTS_DIR/bitops-config/get.sh $SCHEMA_FILE "${full_value_path_schema}.terminal")"
required="$($SCRIPTS_DIR/bitops-config/get.sh $SCHEMA_FILE "${full_value_path_schema}.required")"
export_env="$($SCRIPTS_DIR/bitops-config/get.sh $SCHEMA_FILE "${full_value_path_schema}.export_env")"
default="$($SCRIPTS_DIR/bitops-config/get.sh $SCHEMA_FILE "${full_value_path_schema}.default")"
dash_type="$($SCRIPTS_DIR/bitops-config/get.sh $SCHEMA_FILE "${full_value_path_schema}.dash_type")"

# Default to double dash
if [ -z "$dash_type" ]; then
dash_type="--"
fi

script_option="$($SCRIPTS_DIR/bitops-config/get-convert.sh $BITOPS_CONFIG_FILE "$full_value_path" "$type" "$parameter" "$terminal" "$required" "$export_env" "$default")"

if [ -n "$DEBUG" ]; then
if [ -n "$DEEP_DEBUG" ]; then
echo "$full_value_path"
echo " type: $type"
echo " parameter: $parameter"
echo " terminal: $terminal"
echo " required: $required"
echo " export_env: $export_env"
echo " default: $default"
echo " script_option: $script_option"
echo " dash_type: $dash_type"
fi

script_options="$script_options $script_option"
done <<< "$KEYS_LIST"
script_option="$($SCRIPTS_DIR/bitops-config/get-convert.sh $BITOPS_CONFIG_FILE "$full_value_path" "$type" "$parameter" "$terminal" "$required" "$export_env" "$default" "$dash_type" )"

if [ -n "$script_option" ]; then
script_options="$script_options $script_option"
fi
done <<< "$KEYS_LIST"

if [ -n "$DEBUG" ]; then
echo "script_options:"
echo "script_options: [$script_options]"
fi

echo "$script_options"
Expand Down
21 changes: 16 additions & 5 deletions scripts/bitops-config/convert.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
#!/usr/bin/env bash
set -e

export BITOPS_DIR="/opt/bitops"
export SCRIPTS_DIR="$BITOPS_DIR/scripts"
if [ -z "$BITOPS_DIR" ];then
echo "Using default BitOps Directory"
export BITOPS_DIR="/opt/bitops"
fi

if [ -z "$SCRIPTS_DIR" ];then
echo "Using default BitOps Script Directory"
export SCRIPTS_DIR="/opt/bitops/scripts"
fi

value="$1"
key_type="$2"
cli_flag="$3"
terminal="$4"
dash_type="$5"

if [ -n "$DEBUG" ]; then
if [ -n "$DEEP_DEBUG" ]; then
echo "convert.sh"
echo " value: $value"
echo " key_type: $key_type"
echo " cli_flag: $cli_flag"
echo " terminal: $terminal"
echo " dash_type: $dash_type"
fi

converter_script="$SCRIPTS_DIR/bitops-config/converters/${key_type}.sh"
Expand All @@ -25,9 +34,11 @@ if [ -z "$cli_flag" ] || [ -z "$value" ]; then
fi

if [ -f "$converter_script" ]; then
OUTPUT="$(bash "$converter_script" "$value" "$cli_flag" "$terminal" || exit)"

OUTPUT="$(bash "$converter_script" "$value" "$cli_flag" "$terminal" "$dash_type" || exit)"

else
OUTPUT="$(bash "$SCRIPTS_DIR/bitops-config/converters/string.sh" "$value" "$cli_flag" "$terminal" || exit)"
OUTPUT="$(bash "$SCRIPTS_DIR/bitops-config/converters/string.sh" "$value" "$cli_flag" "$terminal" "$dash_type" || exit)"
fi

echo "$OUTPUT"
Expand Down
9 changes: 5 additions & 4 deletions scripts/bitops-config/converters/boolean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ set -e
value="$1"
cli_flag="$2"
terminal="$3"
dash_type="$4"

if [ -n "$DEBUG" ]; then
if [ -n "$DEEP_DEBUG" ]; then
echo "converters/boolean.sh"
echo " value: $value"
echo " cli_flag: $cli_flag"
Expand All @@ -16,7 +17,7 @@ OUTPUT=""
if [ -z "$value" ] || [ "$value" == "" ] || [ "$value" == "False" ]; then
OUTPUT=""
elif [ "$value" == "True" ]; then
OUTPUT="--${cli_flag}"
OUTPUT="${cli_flag}"
if [ "$terminal" == "True" ] || [ "$terminal" == "true" ]; then
if [ -n "$DEBUG" ]; then
echo "boolean terminal: true - exit 1"
Expand All @@ -25,8 +26,8 @@ elif [ "$value" == "True" ]; then
exit 1
fi
else
OUTPUT="--${cli_flag} $value"
OUTPUT="${cli_flag} $value"
fi

echo "$OUTPUT"
echo "$dash_type$OUTPUT"

7 changes: 4 additions & 3 deletions scripts/bitops-config/converters/exists.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ set -e
value="$1"
cli_flag="$2"
terminal="$3"
dash_type="$4"

if [ -n "$DEBUG" ]; then
if [ -n "$DEEP_DEBUG" ]; then
echo "converters/exists.sh"
echo " value: $value"
echo " cli_flag: $cli_flag"
Expand All @@ -15,8 +16,8 @@ fi
if [ -z "$value" ] || [ "$value" == "" ]; then
OUTPUT=""
else
OUTPUT="--${cli_flag}"
OUTPUT="${cli_flag}"
fi

echo "$OUTPUT"
echo "$dash_type$OUTPUT"

5 changes: 3 additions & 2 deletions scripts/bitops-config/converters/list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e
value="$1"
cli_flag="$2"
terminal="$3"
dash_type="$4"
space=" "

value=${value%$'\n'}
Expand All @@ -12,11 +13,11 @@ do
if [[ $stringval == "" ]] || [[ $stringval == " " ]]; then
setval=""
else
setval=$"--${cli_flag}$stringval"
setval=$"${cli_flag}$stringval"
setval=${setval%$'\n'}
OUTPUT=$OUTPUT$setval$space
fi
done

echo "$OUTPUT"
echo "$dash_type$OUTPUT"

34 changes: 34 additions & 0 deletions scripts/bitops-config/converters/parameter-list.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

# Convert incoming values (from config file) into normalized CLI options
set -e

value="$1"
cli_flag="$2"
terminal="$3"
dash_type="$4"
space=" "

if [ -n "$DEEP_DEBUG" ]; then
echo "converters/parameter-list.sh"
echo " value: $value"
echo " cli_flag: $cli_flag"
echo " terminal: $terminal"
echo " dash_type: $dash_type"
fi

values=$(echo $value | tr "\n" " " | tr "\- " " " | xargs)

IFS=" " read -r -a values_array <<< $values


OUTPUT=
for i in "${values_array[@]}"
do
# <dashes><schema_parameter><config value>
OUTPUT="$OUTPUT $(echo "$dash_type$cli_flag=\"${i}\"")"
done
# OUTPUT NEEDS TO BE: backend-config="KEY1=VALUE1" backend-config="KEY2=VALUE2"

echo "$OUTPUT"

Loading

0 comments on commit 0ce4623

Please sign in to comment.