diff options
| author | Teddy Wing | 2018-04-18 23:25:23 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2018-04-18 23:25:23 +0200 | 
| commit | 56397ffdadf6a74c78dff249e28063f0622e7387 (patch) | |
| tree | 0b96743e2752aed86206edfa8626804a2644225d | |
| parent | 1873a4c0d0683591269502def35948ead3c99b5d (diff) | |
| download | redprine-56397ffdadf6a74c78dff249e28063f0622e7387.tar.bz2 | |
Parse arguments
Get our API tokens etc. from command line arguments. Add a function to
parse arguments, along with a few helper functions error handling.
Much of this is based on my Harvester code:
https://github.com/teddywing/harvester/blob/ae9528d34a2b9df1b8c6ff47f5985e4a747833f8/harvester
| -rwxr-xr-x | redprine | 114 | 
1 files changed, 110 insertions, 4 deletions
| @@ -174,8 +174,114 @@ function update_cache_with_new_pulls () { -pr_json=$(new_pull_requests) -issue_prs=$(issue_numbers_and_pull_request_urls "$pr_json") +# pr_json=$(new_pull_requests) +# issue_prs=$(issue_numbers_and_pull_request_urls "$pr_json") +# +# update_redmine_statuses "$issue_prs" +# update_cache_with_new_pulls "$pr_json" + + +function echo_error () { +	local message="$1" + +	echo "redprine: error: $message" 1>&2 +} + +function exit_with_error () { +	local message="$1" + +	echo_error "$message" + +	exit 1 +} + +function argument_error () { +	local flag="$1" + +	echo_error "Required argument '$flag' not provided, try --help" +} + +function check_required_argument () { +	local flag="$1" +	local variable="$2" + +	if [ -z "$variable" ]; then +		argument_error "$flag" +	fi +} + +function parse_arguments () { +	while [ $# -gt 0 ]; do +		case "$1" in +		--github-token) +			GITHUB_TOKEN="$2" + +			shift 2 +			continue +			;; +		--github-token=*) +			GITHUB_TOKEN="${1##--github-token=}" + +			shift +			continue +			;; +		--github-username) +			GITHUB_USERNAME="$2" + +			shift 2 +			continue +			;; +		--github-username=*) +			GITHUB_USERNAME="${1##--github-username=}" + +			shift +			continue +			;; +		--redmine-base-url) +			REDMINE_BASE_URL="$2" + +			shift 2 +			continue +			;; +		--redmine-base-url=*) +			REDMINE_BASE_URL="${1##--redmine-base-url=}" + +			shift +			continue +			;; +		--redmine-token) +			REDMINE_TOKEN="$2" + +			shift 2 +			continue +			;; +		--redmine-token=*) +			REDMINE_TOKEN="${1##--redmine-token=}" + +			shift +			continue +			;; +		-h | --help) +			print_usage + +			exit 0 +			;; +		-v | --version) +			echo 'TODO' + +			exit 0 +			;; +		*) +			exit_with_error "Unrecognised argument '$1', try --help" +			;; +		esac +	done + +	check_required_argument '--github-token' "$GITHUB_TOKEN" +	check_required_argument '--github-username' "$GITHUB_USERNAME" +	check_required_argument '--redmine-base-url' "$REDMINE_BASE_URL" +	check_required_argument '--redmine-token' "$REDMINE_TOKEN" +} + -update_redmine_statuses "$issue_prs" -update_cache_with_new_pulls "$pr_json" +parse_arguments "$@" | 
