1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Bashベースの良い感じのサブコマンド付きコマンドをサクッと作る

Last updated at Posted at 2020-09-19

管理系コマンドを作る時に本気で作る時はGo言語なりで書くのだけど、とりあえず動くだけで良い時は内部的にはcurl投げてるだけとかgcloudなど別の管理コマンドの結果をjqで加工して組合わせてるだけで済ましたい時は良くある。

とはいえ、設定ファイルらしきものを読み込みたいし、環境変数で上書きもしたい。あとサブコマンドも欲しい。getoptも良いけど、あれはサブコマンドに対応してないのが難点。
という分けで以下のように作ったのだけどたぶん忘れるので投稿しとく。めんどくさいからrubyで書いたワンライナーは後でperlかawkに直しておか無いとなぁ。

# !/bin/bash

#
# Usage
#
function usage {
  cat <<EOM
Usage: $(basename "$0") [optional flags] [command] args
GLOBAL FLAGS
    -h|--help                              Display help
    -v|--verbose                           Make the operation more talkative        
COMMANDS
    deploy {FLOW_FILE}                     Deploy flow
       --help                              Sub Flag
    flows                                  Show flows
    triggers {FLOW_NAME} {ENDPOINT_NAME}   Show triggers
EOM

  exit 2
}

#
# Load config
#
[[ -z "${API_URL}" ]] && API_URL=$(cat ${HOME}/.kuda_config|grep URL:|awk '{print $2}')

#
# Define variable
#
CURL_OPTIONS=""

#
# Parse options
#
OPTIONS=($(echo $@ | ruby -ne 'puts $_.split(" ").reduce([[],[]]){|r, s| (s[0] == "-" && r[1].size == 0 ) ? [r[0] + [s], r[1]] : [r[0], r[1] + [s]] }[0].join(" ")'))
ARGS=($(echo $@    | ruby -ne 'puts $_.split(" ").reduce([[],[]]){|r, s| (s[0] == "-" && r[1].size == 0 ) ? [r[0] + [s], r[1]] : [r[0], r[1] + [s]] }[1].join(" ")'))
SUBCMD=${ARGS[0]}
SUBOPT=($(echo ${ARGS[@]:1} | ruby -ne 'puts $_.split(" ").reduce([[],[]]){|r, s| (s[0] == "-" && r[1].size == 0 ) ? [r[0] + [s], r[1]] : [r[0], r[1] + [s]] }[0].join(" ")'))
SUBARGS=($(echo ${ARGS[@]:1}| ruby -ne 'puts $_.split(" ").reduce([[],[]]){|r, s| (s[0] == "-" && r[1].size == 0 ) ? [r[0] + [s], r[1]] : [r[0], r[1] + [s]] }[1].join(" ")'))

#
# If no arguments
#
if [ "$1" = "" ];then
    usage
fi

#
# Options
#
for opt in ${OPTIONS[@]}; do
    case "$opt" in
        '-v'|'--verbose' ) CURL_OPTIONS=" -v "$CURL_OPTIONS ;;
        '-h'|'--help' ) usage ;;
    esac
done

#
# Sub Commands
#
case "${SUBCMD}" in
    "deploy" ) 
    "run" ) 
        for opt in ${SUBOPT[@]}; do
            case "$opt" in
                '--help' ) 
                    echo "This is a sub-flag"
                ;;
            esac
        done
        FLOW_FILE=${SUBARGS[0]}
        gsutil cp $FLOW_FILE gs://afeafgeae-kuda-flow
        curl ${CURL_OPTIONS} -X POST ${API_URL}/management/load
    ;;
    "flows" ) 
        curl ${CURL_OPTIONS} -X GET ${API_URL}/management/flows
    ;;
    "triggers" ) 
        FLOW_NAME=${SUBARGS[0]}
        ENDPOINT_NAME=${SUBARGS[1]}
        curl ${CURL_OPTIONS} -X GET ${API_URL}/management/triggers/${FLOW_NAME}/${ENDPOINT_NAME}
    ;;
    "help" ) 
        usage
    ;;
esac
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?