Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add positional args for directory, branch, and repository. #20

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
12 changes: 9 additions & 3 deletions arg-parsing-test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}

Expand All @@ -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"
}

28 changes: 23 additions & 5 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -o errexit #abort if any command fails
me=$(basename "$0")

help_message="\
Usage: $me [-c FILE] [<options>]
Usage: $me [-c <FILE>] [<options>] [<directory> [<branch> [<repository>]]]
Deploy generated files to a git branch.

Options:
Expand All @@ -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.

Expand All @@ -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 <branch>, you must also specify <directory>. 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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ Do this every time you want to deploy, or have your CI server do it.
5. run `./deploy.sh`

### options

`deploy.sh [--config <FILE>] [<options>] [<directory> [<branch> [<repository>]]]`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the flag in the script is --config-file, but actually, how about we change it to just --config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oy, good catch (and my bad). --config works for me.


`-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 <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: '.

`-n`, `--no-hash`: don't append the hash of the source commit to the commit message on `deploy_branch`. By default, the hash will be appended in a new paragraph, regardless of whether a message was specified with `-m`.

`-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.
`-e`, `--allow-empty`: allow deployment of an empty directory. By default, the script will abort if `deploy_directory` is empty.

`[<directory> [<branch> [<repository>]]]`: 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".