diff --git a/arg-parsing-test.bats b/arg-parsing-test.bats new file mode 100644 index 0000000..0600f49 --- /dev/null +++ b/arg-parsing-test.bats @@ -0,0 +1,78 @@ +#!/usr/bin/env bats + +source lib/assert.bash +source deploy.sh --source-only + +setup() { + run mktemp -dt deploy_test.XXXX + assert_success + tmp=$output + pushd "$tmp" >/dev/null +} + +teardown() { + popd >/dev/null + rm -rf "$tmp" +} + +set_env_vars() { + # Set environment variables. + export GIT_DEPLOY_APPEND_HASH=env-var +} + +write_env_file() { + # Write a '.env' file to override environment variables. + cat <<-EOF > .env + GIT_DEPLOY_APPEND_HASH=env-file + EOF +} + +write_conf_file() { + # Write a config-file to override '.env'. + cat <<-EOF > conf + GIT_DEPLOY_APPEND_HASH=conf-file + EOF +} + +@test 'Arg-parsing defaults to in-script values.' { + parse_args + assert that "$append_hash" = "true" +} + +@test ' overrides script defaults with environment variables.' { + set_env_vars + + parse_args + assert that "$append_hash" = "env-var" +} + +@test ' overrides environment variables with .env file.' { + set_env_vars + write_env_file + + parse_args + assert that "$append_hash" = "env-file" +} + +@test ' overrides .env with a file specified on the command-line.' { + set_env_vars + write_env_file + write_conf_file + + parse_args --config-file conf + assert that "$append_hash" = "conf-file" +} + +@test ' overrides everything with a command-line option.' { + set_env_vars + write_env_file + write_conf_file + + parse_args --config-file conf --no-hash + assert that "$append_hash" = "false" +} +@test ' sets a commit message with spaces in it.' { + parse_args --message "a message" + assert that "$commit_message" = "a message" +} + diff --git a/deploy.sh b/deploy.sh index a543596..b20b952 100755 --- a/deploy.sh +++ b/deploy.sh @@ -3,7 +3,7 @@ set -o errexit #abort if any command fails me=$(basename "$0") help_message="\ -Usage: $me [] +Usage: $me [-c FILE] [] Deploy generated files to a git branch. Options: @@ -16,7 +16,8 @@ Options: -n, --no-hash Don't append the source commit's hash to the deploy commit's message. -c, --config-file PATH Override default & environment variables' values - with those in set in the file at 'PATH'. + with those in set in the file at 'PATH'. Must be the + first option specified. Variables: @@ -29,16 +30,21 @@ overridden by environment variables. Any environment variables are overridden by values set in a '.env' file (if it exists), and in turn by those set in a file specified by the '--config-file' option." -main() { +parse_args() { # Set args from a local environment file. if [ -e ".env" ]; then source .env fi - #append commit hash to the end of message by default - append_hash=true + # Set args from file specified on the command-line. + if [[ $1 = "-c" || $1 = "--config-file" ]]; then + source "$2" + shift 2 + fi # Parse arg flags + # If something is exposed as an environment variable, set/overwrite it + # here. Otherwise, set/overwrite the internal variable instead. while : ; do if [[ $1 = "-h" || $1 = "--help" ]]; then echo "$help_message" @@ -53,17 +59,17 @@ main() { commit_message=$2 shift 2 elif [[ $1 = "-n" || $1 = "--no-hash" ]]; then - append_hash=false + GIT_DEPLOY_APPEND_HASH=false shift - elif [[ $1 = "-c" || $1 = "--config-file" ]]; then - source "$2" - shift 2 else break fi done - # Set default options + # Set internal option vars from the environment and arg flags. All internal + # vars should be declared here, with sane defaults if applicable. + + # Source directory & target branch. deploy_directory=${GIT_DEPLOY_DIR:-dist} deploy_branch=${GIT_DEPLOY_BRANCH:-gh-pages} @@ -73,7 +79,14 @@ main() { #repository to deploy to. must be readable and writable. repo=${GIT_DEPLOY_REPO:-origin} - + + #append commit hash to the end of message by default + append_hash=${GIT_DEPLOY_APPEND_HASH:-true} +} + +main() { + parse_args "$@" + enable_expanded_output if ! git diff --exit-code --quiet --cached; then diff --git a/readme.md b/readme.md index 7b449ed..b3e2427 100644 --- a/readme.md +++ b/readme.md @@ -39,7 +39,7 @@ Do this every time you want to deploy, or have your CI server do it. ### options `-h`, `--help`: show the program's help info. -`-c`, `--config-file`: specify a file that overrides the script's default configuration, or those values set in `.env`. The syntax for this file should be normal `var=value` declarations. +`-c`, `--config-file`: specify a file that overrides the script's default configuration, or those values set in `.env`. The syntax for this file should be normal `var=value` declarations. __This option _must_ come first on the command-line__. `-m`, `--message `: specify message to be used for the commit on `deploy_branch`. By default, the message is the title of the source commit, prepended with 'publish: '.