# This file provides:# - a default control flow# * initializes the environment# * able to mock "git push" in your script and in all sub scripts# * call a function in your script based on the arguments# - named argument parsing and automatic generation of the "usage" for your script# - intercepting "git push" in your script and all sub scripts# - utility functions## Usage:# - define the variable ARGS_DEF (see below) with the arguments for your script# - include this file using `source utils.inc` at the end of your script.## Default control flow:# 0. Set the current directory to the directory of the script. By this# the script can be called from anywhere.# 1. Parse the named arguments# 2. If the parameter "git_push_dryrun" is set, all calls to `git push` in this script# or in child scripts will be intercepted so that the `--dry-run` and `--porcelain` is added# to show what the push would do but not actually do it.# 3. If the parameter "verbose" is set, the `-x` flag will be set in bash.# 4. The function "init" will be called if it exists# 5. If the parameter "action" is set, it will call the function with the name of that parameter.# Otherwise the function "run" will be called.## Named Argument Parsing:# - The variable ARGS_DEF defines the valid command arguments# * Required args syntax: --paramName=paramRegex# * Optional args syntax: [--paramName=paramRegex]# * e.g. ARG_DEFS=("--required_param=(.+)" "[--optional_param=(.+)]")# - Checks that:# * all arguments match to an entry in ARGS_DEF# * all required arguments are present# * all arguments match their regex# - Afterwards, every parameter value will be stored in a variable# with the name of the parameter in upper case (with dash converted to underscore).## Special arguments that are always available:# - "--action=.*": This parameter will be used to execute a function with that name when the# script is started# - "--git_push_dryrun=true": This will intercept all calls to `git push` in this script# or in child scripts so that the `--dry-run` and `--porcelain` is added# to show what the push would do but not actually do it.# - "--verbose=true": This will set the `-x` flag in bash so that all calls will be logged## Utility functions:# - readJsonProp# - replaceJsonProp# - resolveDir# - getVar# - serVar# - isFunction# always stop on errorsset -efunction usage {echo "Usage: ${0} ${ARG_DEFS[@]}"exit 1}function parseArgs {local REQUIRED_ARG_NAMES=()# -- helper functionsfunction varName {# everything to upper case and dash to underscoreecho ${1//-/_} | tr '[:lower:]' '[:upper:]'}function readArgDefs {local ARG_DEFlocal AD_OPTIONALlocal AD_NAMElocal AD_RE# -- helper functionsfunction parseArgDef {local ARG_DEF_REGEX="(\[?)--([^=]+)=(.*)"if [[ ! $1 =~ $ARG_DEF_REGEX ]]; thenecho "Internal error: arg def has wrong format: $ARG_DEF"exit 1fiAD_OPTIONAL="${BASH_REMATCH[1]}"AD_NAME="${BASH_REMATCH[2]}"AD_RE="${BASH_REMATCH[3]}"if [[ $AD_OPTIONAL ]]; then# Remove last bracket for optional args.# Can't put this into the ARG_DEF_REGEX somehow...AD_RE=${AD_RE%?}fi}# -- runfor ARG_DEF in "${ARG_DEFS[@]}"doparseArgDef $ARG_DEFlocal AD_NAME_UPPER=$(varName $AD_NAME)setVar "${AD_NAME_UPPER}_OPTIONAL" "$AD_OPTIONAL"setVar "${AD_NAME_UPPER}_RE" "$AD_RE"if [[ ! $AD_OPTIONAL ]]; thenREQUIRED_ARG_NAMES+=($AD_NAME)fidone}function readAndValidateArgs {local ARG_NAMElocal ARG_VALUElocal ARG_NAME_UPPER# -- helper functionsfunction parseArg {local ARG_REGEX="--([^=]+)=?(.*)"if [[ ! $1 =~ $ARG_REGEX ]]; thenecho "Can't parse argument $i"usagefiARG_NAME="${BASH_REMATCH[1]}"ARG_VALUE="${BASH_REMATCH[2]}"ARG_NAME_UPPER=$(varName $ARG_NAME)}function validateArg {local AD_RE=$(getVar ${ARG_NAME_UPPER}_RE)if [[ ! $AD_RE ]]; thenecho "Unknown option: $ARG_NAME"usagefiif [[ ! $ARG_VALUE =~ ^${AD_RE}$ ]]; thenecho "Wrong format: $ARG_NAME"usage;fi# validate that the "action" option points to a valid functionif [[ $ARG_NAME == "action" ]] && ! isFunction $ARG_VALUE; thenecho "No action $ARG_VALUE defined in this script"usage;fi}# -- runfor i in "$@"doparseArg $ivalidateArgsetVar "${ARG_NAME_UPPER}" "$ARG_VALUE"done}function checkMissingArgs {local ARG_NAMEfor ARG_NAME in "${REQUIRED_ARG_NAMES[@]}"doARG_VALUE=$(getVar $(varName $ARG_NAME))if [[ ! $ARG_VALUE ]]; thenecho "Missing: $ARG_NAME"usage;fidone}# -- runreadArgDefsreadAndValidateArgs "$@"checkMissingArgs}# getVar(varName)function getVar {echo ${!1}}# setVar(varName, varValue)function setVar {eval "1ドル=\"2ドル\""}# isFunction(name)# - to be used in an if, so return 0 if successful and 1 if not!function isFunction {if [[ $(type -t $1) == "function" ]]; thenreturn 0elsereturn 1fi}# readJsonProp(jsonFile, property)# - restriction: property needs to be on a single line!function readJsonProp {echo $(sed -En 's/.*"'$2'"[ ]*:[ ]*"(.*)".*/1円/p' $1)}# replaceJsonProp(jsonFile, propertyRegex, valueRegex, replacePattern)# - note: propertyRegex will be automatically placed into a# capturing group! -> all other groups start at index 2!function replaceJsonProp {replaceInFile $1 '"('$2')"[ ]*:[ ]*"'$3'"' '"1円": "'$4'"'}# replaceInFile(file, findPattern, replacePattern)function replaceInFile {sed -i .tmp -E "s/2ドル/3ドル/" $1rm $1.tmp}# resolveDir(relativeDir)# - resolves a directory relative to the current scriptfunction resolveDir {echo $(cd $SCRIPT_DIR; cd $1; pwd)}function git_push_dryrun_proxy {echo "## git push dryrun proxy enabled!"export ORIGIN_GIT=$(which git)function git {local ARGS=("$@")local RCif [[ $1 == "push" ]]; thenARGS+=("--dry-run" "--porcelain")echo "####### START GIT PUSH DRYRUN #######"echo "${ARGS[@]}"fiif [[ $1 == "commit" ]]; thenecho "${ARGS[@]}"fi$ORIGIN_GIT "${ARGS[@]}"RC=$?if [[ $1 == "push" ]]; thenecho "####### END GIT PUSH DRYRUN #######"fireturn $RC}export -f git}function main {# normalize the working dir to the directory of the scriptcd $(dirname $0);SCRIPT_DIR=$(pwd)ARG_DEFS+=("[--git-push-dryrun=(true|false)]" "[--verbose=(true|false)]")parseArgs "$@"# --git_push_dryrun argumentif [[ $GIT_PUSH_DRYRUN == "true" ]]; thengit_push_dryrun_proxyfi# --verbose argumentif [[ $VERBOSE == "true" ]]; thenset -xfiif isFunction init; theninit "$@"fi# jump to the function denoted by the --action argument,# otherwise call the "run" functionif [[ $ACTION ]]; then$ACTION "$@"elserun "$@"fi}main "$@"
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。