Created
May 22, 2023 17:33
-
-
Save bardic/a66ac6a5b6cbe9464096da3dd971855d to your computer and use it in GitHub Desktop.
Example of how I use jq to parse some json for script commands and details
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
| #!/bin/sh | |
| ############ | |
| # | |
| # Functions | |
| # | |
| ########### | |
| function exit_with_help { | |
| echo \ | |
| " | |
| +-------+------+--------------+ | |
| | name | flag | help | | |
| +-------+------+--------------+ | |
| | clean | c | docker prune | | |
| | test | t | test flag | | |
| | help | h: | get help | | |
| +-------+------+--------------+ | |
| " | |
| echo "Usage: $(basename $0) [-a] [-t] [-h argument]" | |
| exit 1 | |
| } | |
| function start { | |
| docker-compose up | |
| } | |
| function up_no_cache { | |
| docker-compose up --build --force-recreate | |
| } | |
| ############ | |
| # | |
| # Iput / Startup | |
| # | |
| ########### | |
| export DOCKER_BUILDKIT=1 | |
| export COMPOSE_DOCKER_CLI_BUILD=1 | |
| CPWD=$_ | |
| OPTIONS='{"root": [ | |
| {"name":"clean", "flag":"c", "help":"docker prune"}, | |
| {"name":"test", "flag":"t", "help":"test flag"}, | |
| {"name":"help", "flag":"h:", "help":"get help"} | |
| ]}' | |
| JOP=$(echo $OPTIONS | jq -c -r '.root[].flag' | tr -d "\"" | tr -d "\n" ) | |
| DARGS="" | |
| while getopts $JOP OPTION; do | |
| HASOPS=true | |
| echo "no ops" | |
| case "$OPTION" in | |
| c) | |
| echo $? | |
| ;; | |
| h) | |
| helpArg="$OPTARG" | |
| echo $OPTIONS | jq --arg helpArg "$helpArg" '.root[] | select (.flag == $helpArg).help' | |
| ;; | |
| t) | |
| ;; | |
| ?) | |
| echo "helo" | |
| exit_with_help | |
| ;; | |
| esac | |
| done | |
| if [[ -z $HASOPS ]] | |
| then | |
| start | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment