Last active
September 3, 2019 20:43
-
-
Save Deifinger/b209da379a933c3c3e53c9f10c197129 to your computer and use it in GitHub Desktop.
Bash script is used for running nested named scripts from "scripts.json"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": { | |
"dc": "ls @l", | |
"l": "-l", | |
"pwd": "pwd", | |
"echo": "echo Monkey", | |
"another": "sudo @dc", | |
"docker": "docker ps" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#author :Ruslan Kostikov <[email protected]> | |
#description :This script is used for running nested named scripts from "scripts.json" | |
# Written with best practices: | |
# - https://www.davidpashley.com/articles/writing-robust-shell-scripts/ | |
# - https://github.com/progrium/bashstyle | |
[[ "$TRACE" ]] && set -o xtrace # For debugging. Print command traces before executing command. | |
set -o errexit # Tells bash that it should exit the script if any statement returns a non-true return value | |
set -o nounset # Exit script if you try to use an uninitialised variable | |
set -o pipefail # false | true will be considered to have fail | |
set -o noclobber # IO redirection | |
alias errcho=">&2 echo" | |
scripts=$(jq '.scripts' scripts.json) | |
keys=$(echo "${scripts}" | jq 'keys[]') | |
main() { | |
local cmd="${1-}" | |
local options="${2-}" | |
local prepared_script | |
prepared_script="$(generate_script "${cmd}")" | |
eval "${prepared_script}" "${options}" | |
} | |
generate_script() | |
{ | |
local command="${1//@}" | |
local raw_script; | |
if [[ ${keys} =~ ${command} ]]; then | |
raw_script="$(echo "${scripts}" | jq -r ."${command}")" | |
proceed_raw_script "${raw_script}" | |
else | |
echo "${command}" | |
fi | |
} | |
proceed_nested_script() | |
{ | |
local nested_script="$1" | |
local prepared_script | |
for nested_sign in ${nested_script}; do | |
signature="$(generate_script "${nested_sign}")" | |
prepared_script+=("${signature}"); | |
done | |
echo "${prepared_script[@]}" | |
} | |
proceed_signature() | |
{ | |
local signature="$1" | |
local prepared_script | |
# printf "@ Signature: %s" "${signature}" | |
if [[ ${signature} =~ @ ]]; then | |
# printf "@ Signature: ${signature}" | |
nested_script="$(echo "${scripts}" | jq -r ."${signature//@}")" | |
prepared_script+=("$(proceed_nested_script "${nested_script}")") | |
else | |
prepared_script+=("${signature}"); | |
fi | |
echo "${prepared_script[@]}" | |
} | |
proceed_raw_script() | |
{ | |
local raw_script="$1" | |
local prepared_script | |
for signature in ${raw_script}; do | |
prepared_script+=("$(proceed_signature "${signature}")") | |
done | |
echo "${prepared_script[@]}" | |
} | |
if [[ $# != 0 && ${keys} =~ $1 ]]; then | |
main "$1" "${*:2}" | |
else | |
echo 'Command not found. Available commands: ' | |
echo "${keys}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment