diff --git a/arg-parsing-test.bats b/arg-parsing-test.bats index 0600f49..2957514 100644 --- a/arg-parsing-test.bats +++ b/arg-parsing-test.bats @@ -28,7 +28,7 @@ write_env_file() { } write_conf_file() { - # Write a config-file to override '.env'. + # Write a configuration file to override '.env'. cat <<-EOF > conf GIT_DEPLOY_APPEND_HASH=conf-file EOF @@ -59,7 +59,7 @@ write_conf_file() { write_env_file write_conf_file - parse_args --config-file conf + parse_args --config conf assert that "$append_hash" = "conf-file" } @@ -68,11 +68,17 @@ write_conf_file() { write_env_file write_conf_file - parse_args --config-file conf --no-hash + parse_args --config 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" } +@test ' correctly handles positional args.' { + parse_args pos-dir pos-branch pos-repo + assert that "$deploy_directory" = "pos-dir" + assert that "$deploy_branch" = "pos-branch" + assert that "$repo" = "pos-repo" +} diff --git a/deploy.sh b/deploy.sh index 7a8c5a5..20fb070 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 [-c FILE] [] +Usage: $me [-c ] [] [ [ []]] Deploy generated files to a git branch. Options: @@ -15,7 +15,7 @@ Options: deploy branch. -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 + -c, --config PATH Override default & environment variables' values with those in set in the file at 'PATH'. Must be the first option specified. @@ -28,7 +28,15 @@ Variables: These variables have default values defined in the script. The defaults can be 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." +file specified by the '--config' option. + +Positional Args: + +At the end of the command, you can optionally specify the directory, branch, +and repository as well. Earlier values are required to specify later ones. For +example, in order to specify , you must also specify . Like +the command-line options, these will override values set in configuration files +and the environment." parse_args() { # Set args from a local environment file. @@ -37,7 +45,7 @@ parse_args() { fi # Set args from file specified on the command-line. - if [[ $1 = "-c" || $1 = "--config-file" ]]; then + if [[ $1 = "-c" || $1 = "--config" ]]; then source "$2" shift 2 fi @@ -48,7 +56,7 @@ parse_args() { while : ; do if [[ $1 = "-h" || $1 = "--help" ]]; then echo "$help_message" - return 0 + exit 0 elif [[ $1 = "-v" || $1 = "--verbose" ]]; then verbose=true shift @@ -61,6 +69,16 @@ parse_args() { elif [[ $1 = "-n" || $1 = "--no-hash" ]]; then GIT_DEPLOY_APPEND_HASH=false shift + elif [[ -n ${1} ]]; then + # Set positional args + GIT_DEPLOY_DIR=$1 + if [[ -n $2 ]]; then + GIT_DEPLOY_BRANCH=$2 + fi + if [[ -n $3 ]]; then + GIT_DEPLOY_REPO=$3 + fi + break else break fi diff --git a/readme.md b/readme.md index 8a5f013..926ae64 100644 --- a/readme.md +++ b/readme.md @@ -37,9 +37,12 @@ Do this every time you want to deploy, or have your CI server do it. 5. run `./deploy.sh` ### options + +`deploy.sh [--config ] [] [ [ []]]` + `-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. __This option _must_ come first on the command-line__. +`-c`, `--config`: 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: '. @@ -47,4 +50,6 @@ Do this every time you want to deploy, or have your CI server do it. `-v`, `--verbose`: echo expanded commands as they are executed, using the xtrace option. This can be useful for debugging, as the output will include the values of variables that are being used, such as $commit_title and $deploy_directory. However, the script makes special effort to not output the value of $repo, as it may contain a secret authentication token. -`-e`, `--allow-empty`: allow deployment of an empty directory. By default, the script will abort if `deploy_directory` is empty. \ No newline at end of file +`-e`, `--allow-empty`: allow deployment of an empty directory. By default, the script will abort if `deploy_directory` is empty. + +`[ [ []]]`: set the directory/branch/repository. __These options _must_ come at the end of the command-line__. Also, in order to specify later options, you _must_ specify each earlier one. So in order to specify "repository", you need to also specify "directory" and "branch".