Skip to content

Commit c72dfed

Browse files
author
Matt Pearson
committed
Merge branch 'master' into feature/positional-args
2 parents 66affa9 + 7afe859 commit c72dfed

File tree

3 files changed

+129
-10
lines changed

3 files changed

+129
-10
lines changed

arg-parsing-test.bats

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env bats
2+
3+
source lib/assert.bash
4+
source deploy.sh --source-only
5+
6+
setup() {
7+
run mktemp -dt deploy_test.XXXX
8+
assert_success
9+
tmp=$output
10+
pushd "$tmp" >/dev/null
11+
}
12+
13+
teardown() {
14+
popd >/dev/null
15+
rm -rf "$tmp"
16+
}
17+
18+
set_env_vars() {
19+
# Set environment variables.
20+
export GIT_DEPLOY_APPEND_HASH=env-var
21+
}
22+
23+
write_env_file() {
24+
# Write a '.env' file to override environment variables.
25+
cat <<-EOF > .env
26+
GIT_DEPLOY_APPEND_HASH=env-file
27+
EOF
28+
}
29+
30+
write_conf_file() {
31+
# Write a config-file to override '.env'.
32+
cat <<-EOF > conf
33+
GIT_DEPLOY_APPEND_HASH=conf-file
34+
EOF
35+
}
36+
37+
@test 'Arg-parsing defaults to in-script values.' {
38+
parse_args
39+
assert that "$append_hash" = "true"
40+
}
41+
42+
@test ' overrides script defaults with environment variables.' {
43+
set_env_vars
44+
45+
parse_args
46+
assert that "$append_hash" = "env-var"
47+
}
48+
49+
@test ' overrides environment variables with .env file.' {
50+
set_env_vars
51+
write_env_file
52+
53+
parse_args
54+
assert that "$append_hash" = "env-file"
55+
}
56+
57+
@test ' overrides .env with a file specified on the command-line.' {
58+
set_env_vars
59+
write_env_file
60+
write_conf_file
61+
62+
parse_args --config-file conf
63+
assert that "$append_hash" = "conf-file"
64+
}
65+
66+
@test ' overrides everything with a command-line option.' {
67+
set_env_vars
68+
write_env_file
69+
write_conf_file
70+
71+
parse_args --config-file conf --no-hash
72+
assert that "$append_hash" = "false"
73+
}
74+
@test ' sets a commit message with spaces in it.' {
75+
parse_args --message "a message"
76+
assert that "$commit_message" = "a message"
77+
}
78+

deploy.sh

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,55 @@
11
#!/usr/bin/env bash
22
set -o errexit #abort if any command fails
3+
me=$(basename "$0")
4+
5+
help_message="\
6+
Usage: $me [-c FILE] [<options>]
7+
Deploy generated files to a git branch.
8+
9+
Options:
10+
11+
-h, --help Show this help information.
12+
-v, --verbose Increase verbosity. Useful for debugging.
13+
-e, --allow-empty Allow deployment of an empty directory.
14+
-m, --message MESSAGE Specify the message used when committing on the
15+
deploy branch.
16+
-n, --no-hash Don't append the source commit's hash to the deploy
17+
commit's message.
18+
-c, --config-file PATH Override default & environment variables' values
19+
with those in set in the file at 'PATH'. Must be the
20+
first option specified.
21+
22+
Variables:
23+
24+
GIT_DEPLOY_DIR Folder path containing the files to deploy.
25+
GIT_DEPLOY_BRANCH Commit deployable files to this branch.
26+
GIT_DEPLOY_REPO Push the deploy branch to this repository.
27+
28+
These variables have default values defined in the script. The defaults can be
29+
overridden by environment variables. Any environment variables are overridden
30+
by values set in a '.env' file (if it exists), and in turn by those set in a
31+
file specified by the '--config-file' option."
332

433
parse_args() {
534
# Set args from a local environment file.
635
if [ -e ".env" ]; then
736
source .env
837
fi
938

10-
#append commit hash to the end of message by default
11-
append_hash=true
39+
# Set args from file specified on the command-line.
40+
if [[ $1 = "-c" || $1 = "--config-file" ]]; then
41+
source "$2"
42+
shift 2
43+
fi
1244

1345
# Parse arg flags
14-
# [<options>] [<directory> [<branch> [<repository]]]
46+
# If something is exposed as an environment variable, set/overwrite it
47+
# here. Otherwise, set/overwrite the internal variable instead.
1548
while : ; do
16-
if [[ $1 = "-v" || $1 = "--verbose" ]]; then
49+
if [[ $1 = "-h" || $1 = "--help" ]]; then
50+
echo "$help_message"
51+
return 0
52+
elif [[ $1 = "-v" || $1 = "--verbose" ]]; then
1753
verbose=true
1854
shift
1955
elif [[ $1 = "-e" || $1 = "--allow-empty" ]]; then
@@ -23,11 +59,8 @@ parse_args() {
2359
commit_message=$2
2460
shift 2
2561
elif [[ $1 = "-n" || $1 = "--no-hash" ]]; then
26-
append_hash=false
62+
GIT_DEPLOY_APPEND_HASH=false
2763
shift
28-
elif [[ $1 = "-c" || $1 = "--config-file" ]]; then
29-
source "$2"
30-
shift 2
3164
elif [[ -n ${1} ]]; then
3265
# Set positional args
3366
deploy_directory=$1
@@ -43,7 +76,10 @@ parse_args() {
4376
fi
4477
done
4578

46-
# Set default options
79+
# Set internal option vars from the environment and arg flags. All internal
80+
# vars should be declared here, with sane defaults if applicable.
81+
82+
# Source directory & target branch.
4783
deploy_directory=${GIT_DEPLOY_DIR:-dist}
4884
deploy_branch=${GIT_DEPLOY_BRANCH:-gh-pages}
4985

@@ -53,6 +89,9 @@ parse_args() {
5389

5490
#repository to deploy to. must be readable and writable.
5591
repo=${GIT_DEPLOY_REPO:-origin}
92+
93+
#append commit hash to the end of message by default
94+
append_hash=${GIT_DEPLOY_APPEND_HASH:-true}
5695
}
5796

5897
main() {

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ Do this every time you want to deploy, or have your CI server do it.
3737
5. run `./deploy.sh`
3838

3939
### options
40-
`-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.
40+
`-h`, `--help`: show the program's help info.
41+
42+
`-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__.
4143

4244
`-m`, `--message <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: '.
4345

0 commit comments

Comments
 (0)