From a7badff37dd7f4602a69f4d5cd5927409714e1e7 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Thu, 10 Dec 2015 16:27:26 -0800 Subject: [PATCH 01/13] Add positional args for directory, branch, and repository. This implements '[] [ [ [ come before the positional args. --- deploy.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/deploy.sh b/deploy.sh index d52a263..b828730 100755 --- a/deploy.sh +++ b/deploy.sh @@ -11,6 +11,7 @@ main() { append_hash=true # Parse arg flags + # [] [ [ [ Date: Tue, 15 Dec 2015 16:07:32 -0800 Subject: [PATCH 02/13] Move args-parsing to its own file. --- deploy.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/deploy.sh b/deploy.sh index b828730..ff419cd 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -o errexit #abort if any command fails -main() { +parse_args() { # Set args from a local environment file. if [ -e ".env" ]; then source .env @@ -53,7 +53,11 @@ main() { #repository to deploy to. must be readable and writable. repo=${GIT_DEPLOY_REPO:-origin} - +} + +main() { + parse_args "$@" + enable_expanded_output if ! git diff --exit-code --quiet --cached; then From 18284327a935c42f9e1c8b006393edb688d1dc52 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Tue, 15 Dec 2015 16:44:21 -0800 Subject: [PATCH 03/13] Add tests for args. --- tests/args.bats | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/args.bats diff --git a/tests/args.bats b/tests/args.bats new file mode 100644 index 0000000..6e165fb --- /dev/null +++ b/tests/args.bats @@ -0,0 +1,26 @@ +#!/usr/bin/env bats + +source lib/assert.bash +source deploy.sh --source-only + +@test 'option: -v, --verbose' { + assert that `parse_args -v && echo $verbose` = "true" + assert that `parse_args --verbose && echo $verbose` = "true" +} +@test 'option: -e, --allow-empty' { + assert that `parse_args -e && echo $allow_empty` = "true" + assert that `parse_args --allow-empty && echo $allow_empty` = "true" +} +@test 'option: -m, --message' { + skip # Check fails if arg/var value has spaces in it. + assert that `parse_args -m "a message" && echo $commit_message` = "a message" + assert that `parse_args --message "a message" && echo $commit_message` = "a message" +} +@test 'option: -n, --no-hash' { + assert that `parse_args -n && echo $append_hash` = "false" + assert that `parse_args --no-hash && echo $append_hash` = "false" +} +@test 'option: -c, --config-file' { + skip # No var to test yet. +} + From 4f77336bdc0a48a8b9ed88c9bb1cbe96fe1d86ad Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Tue, 15 Dec 2015 16:45:22 -0800 Subject: [PATCH 04/13] Add tests for script's default settings. --- tests/args.bats | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/args.bats b/tests/args.bats index 6e165fb..0ecf982 100644 --- a/tests/args.bats +++ b/tests/args.bats @@ -24,3 +24,21 @@ source deploy.sh --source-only skip # No var to test yet. } +# Script's default settings. +@test 'script default: default_username' { + assert that `parse_args && echo $default_username` = "deploy.sh" +} +@test 'script default: default_email' { + skip # Needs a value to check against. + assert that `parse_args && echo $default_email` = "" +} +@test 'script default: deploy_directory' { + assert that `parse_args && echo $deploy_directory` = "dist" +} +@test 'script default: deploy_branch' { + assert that `parse_args && echo $deploy_branch` = "gh-pages" +} +@test 'script default: repo' { + assert that `parse_args && echo $repo` = "origin" +} + From 0d5cfe7b9ee36c1e820a0a04648cf20ab6b69500 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Tue, 15 Dec 2015 16:47:19 -0800 Subject: [PATCH 05/13] Add failing test for positional args. --- tests/args.bats | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/args.bats b/tests/args.bats index 0ecf982..313954b 100644 --- a/tests/args.bats +++ b/tests/args.bats @@ -42,3 +42,13 @@ source deploy.sh --source-only assert that `parse_args && echo $repo` = "origin" } +# Positional args. +@test 'positional args' { + skip # Test is failing. + assert that $( + args="-n pos-dir pos-branch pos-repo" + parse_args + echo $deploy_directory + ) = "pos-dir" +} + From 66affa9b22c148bcf848ef79a38490cd44a7b6ae Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Wed, 16 Dec 2015 10:50:27 -0800 Subject: [PATCH 06/13] Run tests in 'tests/' on CircleCI. --- circle.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circle.yml b/circle.yml index 1dbd626..793aa41 100644 --- a/circle.yml +++ b/circle.yml @@ -5,4 +5,4 @@ dependencies: test: post: - - bats . lib + - bats . lib tests From ad7b678db46c2e059695ae6a11c9f19744aa2e5b Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Thu, 17 Dec 2015 14:13:55 -0800 Subject: [PATCH 07/13] Remove superfluous tests file. --- circle.yml | 2 +- tests/args.bats | 54 ------------------------------------------------- 2 files changed, 1 insertion(+), 55 deletions(-) delete mode 100644 tests/args.bats diff --git a/circle.yml b/circle.yml index 793aa41..1dbd626 100644 --- a/circle.yml +++ b/circle.yml @@ -5,4 +5,4 @@ dependencies: test: post: - - bats . lib tests + - bats . lib diff --git a/tests/args.bats b/tests/args.bats deleted file mode 100644 index 313954b..0000000 --- a/tests/args.bats +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env bats - -source lib/assert.bash -source deploy.sh --source-only - -@test 'option: -v, --verbose' { - assert that `parse_args -v && echo $verbose` = "true" - assert that `parse_args --verbose && echo $verbose` = "true" -} -@test 'option: -e, --allow-empty' { - assert that `parse_args -e && echo $allow_empty` = "true" - assert that `parse_args --allow-empty && echo $allow_empty` = "true" -} -@test 'option: -m, --message' { - skip # Check fails if arg/var value has spaces in it. - assert that `parse_args -m "a message" && echo $commit_message` = "a message" - assert that `parse_args --message "a message" && echo $commit_message` = "a message" -} -@test 'option: -n, --no-hash' { - assert that `parse_args -n && echo $append_hash` = "false" - assert that `parse_args --no-hash && echo $append_hash` = "false" -} -@test 'option: -c, --config-file' { - skip # No var to test yet. -} - -# Script's default settings. -@test 'script default: default_username' { - assert that `parse_args && echo $default_username` = "deploy.sh" -} -@test 'script default: default_email' { - skip # Needs a value to check against. - assert that `parse_args && echo $default_email` = "" -} -@test 'script default: deploy_directory' { - assert that `parse_args && echo $deploy_directory` = "dist" -} -@test 'script default: deploy_branch' { - assert that `parse_args && echo $deploy_branch` = "gh-pages" -} -@test 'script default: repo' { - assert that `parse_args && echo $repo` = "origin" -} - -# Positional args. -@test 'positional args' { - skip # Test is failing. - assert that $( - args="-n pos-dir pos-branch pos-repo" - parse_args - echo $deploy_directory - ) = "pos-dir" -} - From 19507def4182a8ae710ac0177219db71814edf24 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Thu, 17 Dec 2015 14:20:36 -0800 Subject: [PATCH 08/13] Add test for positional args. --- arg-parsing-test.bats | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arg-parsing-test.bats b/arg-parsing-test.bats index 0600f49..b994790 100644 --- a/arg-parsing-test.bats +++ b/arg-parsing-test.bats @@ -75,4 +75,10 @@ write_conf_file() { 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" +} From f6036b510d74b7d00c094d22ac632dda2b917e62 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Thu, 17 Dec 2015 14:23:09 -0800 Subject: [PATCH 09/13] Make positional args to set env-vars instead of internal vars. --- deploy.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deploy.sh b/deploy.sh index 8801cd6..2fe7778 100755 --- a/deploy.sh +++ b/deploy.sh @@ -63,12 +63,12 @@ parse_args() { shift elif [[ -n ${1} ]]; then # Set positional args - deploy_directory=$1 + GIT_DEPLOY_DIR=$1 if [[ -n $2 ]]; then - deploy_branch=$2 + GIT_DEPLOY_BRANCH=$2 fi if [[ -n $3 ]]; then - repo=$3 + GIT_DEPLOY_REPO=$3 fi break else From 5eb8199f452d68a2064b1863ac5b96de628d859a Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Thu, 17 Dec 2015 16:18:48 -0800 Subject: [PATCH 10/13] Add docs for positional args. --- deploy.sh | 11 +++++++++-- readme.md | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/deploy.sh b/deploy.sh index 2fe7778..cb97773 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: @@ -28,7 +28,14 @@ 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-file' 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" parse_args() { # Set args from a local environment file. diff --git a/readme.md b/readme.md index b3e2427..e2a6c40 100644 --- a/readme.md +++ b/readme.md @@ -37,6 +37,9 @@ 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__. @@ -48,3 +51,5 @@ 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. + +`[ [ []]]`: 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". From 9dd1d10b39a58b575c21694ca967ac8e95086e91 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Fri, 18 Dec 2015 07:33:01 -0800 Subject: [PATCH 11/13] Use '--config' instead of '--config-file'. --- arg-parsing-test.bats | 6 +++--- deploy.sh | 6 +++--- readme.md | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/arg-parsing-test.bats b/arg-parsing-test.bats index b994790..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,7 +68,7 @@ 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.' { diff --git a/deploy.sh b/deploy.sh index cb97773..2a5262d 100755 --- a/deploy.sh +++ b/deploy.sh @@ -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,7 @@ 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: @@ -44,7 +44,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 diff --git a/readme.md b/readme.md index e2a6c40..87d89e3 100644 --- a/readme.md +++ b/readme.md @@ -42,7 +42,7 @@ Do this every time you want to deploy, or have your CI server do it. `-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: '. From b91b54eed77e8bdd7128a262f071c33e36d8256a Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Sun, 20 Dec 2015 13:55:57 -0800 Subject: [PATCH 12/13] Finish sentence explaining positional args. --- deploy.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deploy.sh b/deploy.sh index 2a5262d..fd0af5b 100755 --- a/deploy.sh +++ b/deploy.sh @@ -35,7 +35,8 @@ 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" +the command-line options, these will override values set in configuration files +and the environment." parse_args() { # Set args from a local environment file. From dc3959fcbd9c527a67a57df041e22cf23db53f41 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Sun, 20 Dec 2015 14:14:23 -0800 Subject: [PATCH 13/13] Make '-h' exit immediately after printing help info. Without this, the script continues to execute, and we see this error: ``` ./deploy.sh: line 124: [: =: unary operator expected Deploy directory '' does not exist. Aborting. ``` --- deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy.sh b/deploy.sh index fd0af5b..20fb070 100755 --- a/deploy.sh +++ b/deploy.sh @@ -56,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