Skip to content

Commit

Permalink
Merge pull request #34 from joao-paulo-parity/opt-args
Browse files Browse the repository at this point in the history
Receive CLI arguments as options for check_dependent_project.sh
  • Loading branch information
TriplEight committed Apr 26, 2022
2 parents 00e758e + b453289 commit 51ace43
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
28 changes: 15 additions & 13 deletions check_dependent_project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,29 @@ changes.
set -eu -o pipefail
shopt -s inherit_errexit

die() {
if [ "${1:-}" ]; then
>&2 echo "$1"
fi
exit 1
}
. "$(dirname "${BASH_SOURCE[0]}")/utils.sh"

get_arg required --org "$@"
org="$out"

get_arg required --dependent-repo "$@"
dependent_repo="$out"

get_arg required --github-api-token "$@"
github_api_token="$out"

org="$1"
this_repo="$2"
this_repo_diener_arg="$3"
dependent_repo="$4"
github_api_token="$5"
update_crates_on_default_branch="$6"
extra_dependencies="${7:-}"
get_arg optional --extra-dependencies "$@"
extra_dependencies="${out:-}"

set -x
this_repo_dir="$PWD"
this_repo="$(basename "$this_repo_dir")"
companions_dir="$this_repo_dir/companions"
extra_dependencies_dir="$this_repo_dir/extra_dependencies"
github_api="https://api.github.com"
org_github_prefix="https://github.com/$org"
org_crates_prefix="git+$org_github_prefix"
set +x

our_crates=()
discover_our_crates() {
Expand Down
68 changes: 68 additions & 0 deletions utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
die() {
if [ "${1:-}" ]; then
>&2 echo "$1"
fi
exit 1
}

get_arg() {
local arg_type="$1"
shift

local is_required
case "$arg_type" in
required|required-many)
is_required=true
;;
optional|optional-many) ;;
*)
die "Invalid is_required argument \"$2\" in get_arg"
;;
esac

local has_many_values
if [ "${arg_type: -6}" == "-many" ]; then
has_many_values=true
fi

local option_arg="$1"
shift

unset out
out=()

local get_next_arg
for arg in "$@"; do
if [ "${get_next_arg:-}" ]; then
out+=("$arg")
unset get_next_arg
if [ ! "${has_many_values:-}" ]; then
break
fi
# --foo=bar (get the value after '=')
elif [ "${arg:0:$(( ${#option_arg} + 1 ))}" == "$option_arg=" ]; then
out+=("${arg:$(( ${#option_arg} + 1 ))}")
if [ ! "${has_many_values:-}" ]; then
break
fi
# --foo bar (get the next argument)
elif [ "$arg" == "$option_arg" ]; then
get_next_arg=true
fi
done

# arg list ended with --something but no argument was provided next
if [ "${get_next_arg:-}" ]; then
die "Expected argument after \"${args[-1]}"\"
fi

if [ "${out[0]:-}" ]; then
if [ ! "${has_many_values:-}" ]; then
out="${out[0]}"
fi
elif [ "${is_required:-}" ]; then
die "Argument $option_arg is required, but was not found"
else
unset out
fi
}

0 comments on commit 51ace43

Please sign in to comment.