Skip to content

Commit b1b21a4

Browse files
authored
Merge pull request #268 from DannyBen/fix/default-env-vars-init
Make default environment variable values available in `initialize()`
2 parents 4c29723 + 9d5e97a commit b1b21a4

File tree

9 files changed

+113
-6
lines changed

9 files changed

+113
-6
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if default_environment_variables.any?
2+
= view_marker
3+
4+
default_environment_variables.each do |env_var|
5+
> export {{ env_var.name.upcase }}="${<%= env_var.name.upcase %>:-<%= env_var.default %>}"
6+
end
7+
end

lib/bashly/views/command/environment_variables_filter.gtx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
if default_environment_variables.any? or required_environment_variables.any?
22
= view_marker
3-
4-
if default_environment_variables.any?
5-
default_environment_variables.each do |env_var|
6-
> export {{ env_var.name.upcase }}="${<%= env_var.name.upcase %>:-<%= env_var.default %>}"
7-
end
8-
end
3+
= render(:environment_variables_default)
94

105
if required_environment_variables.any?
116
required_environment_variables.each do |env_var|

lib/bashly/views/command/initialize.gtx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
> {{ Settings.strict ? "set -euo pipefail" : "set -e" }}
77
>
88

9+
if root_command?
10+
= render(:environment_variables_default).indent 2
11+
end
12+
913
= load_user_file("initialize.sh", placeholder: false).indent 2
1014

1115
> }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
creating user files in src
2+
skipped src/initialize.sh (exists)
3+
created src/download_command.sh
4+
created ./cli
5+
run ./cli --help to test your bash script
6+
+ ./cli
7+
PASS: API_KEY=must-be-available-in-initialize
8+
PASS: NESTED_VAR is empty
9+
missing required environment variable: CONFIG_PATH
10+
+ export CONFIG_PATH=somepath
11+
+ CONFIG_PATH=somepath
12+
+ ./cli download source
13+
PASS: API_KEY=must-be-available-in-initialize
14+
PASS: NESTED_VAR is empty
15+
# this file is located in 'src/download_command.sh'
16+
# code for 'cli download' goes here
17+
# you can edit it freely and regenerate (it will not be overwritten)
18+
args:
19+
- ${args[source]} = source
20+
+ export API_KEY=override-value
21+
+ API_KEY=override-value
22+
+ ./cli
23+
PASS: API_KEY=override-value
24+
PASS: NESTED_VAR is empty
25+
cli - Sample application
26+
27+
Usage:
28+
cli [command]
29+
cli [command] --help | -h
30+
cli --version | -v
31+
32+
Commands:
33+
download Download a file
34+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cli
2+
src/download_command.sh
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This fixture tests that default values for environment variables exist in
2+
the initialize function, and that required environment variables are NOT
3+
blocking --help.
4+
5+
Reference issue: https://github.com/DannyBen/bashly/issues/265
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
environment_variables:
6+
- name: api_key
7+
default: must-be-available-in-initialize
8+
help: Set your API key
9+
- name: config_path
10+
required: true
11+
help: Set the config path
12+
13+
commands:
14+
- name: download
15+
alias: d
16+
help: Download a file
17+
18+
args:
19+
- name: source
20+
help: URL to download from
21+
- name: target
22+
help: "Target filename (default: same as source)"
23+
24+
environment_variables:
25+
- name: nested_var
26+
default: not-available-in-initialize
27+
help: Set the default location to download to
28+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
if [[ -n "$API_KEY" ]]; then
2+
echo "PASS: API_KEY=$API_KEY"
3+
else
4+
echo "FAIL: API_KEY is empty"
5+
fi
6+
7+
if [[ -z "$NESTED_VAR" ]]; then
8+
echo "PASS: NESTED_VAR is empty"
9+
else
10+
echo "FAIL: NESTED_VAR=$NESTED_VAR"
11+
fi
12+
13+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
# This fixture tests that default values for environment variables exist in
4+
# the initialize function, and that required environment variables are NOT
5+
# blocking --help.
6+
# Reference issue: https://github.com/DannyBen/bashly/issues/265
7+
8+
rm -f ./src/download_command.sh
9+
rm -f ./cli
10+
11+
bundle exec bashly generate
12+
13+
set -x
14+
15+
./cli
16+
export CONFIG_PATH=somepath
17+
./cli download source
18+
export API_KEY=override-value
19+
./cli

0 commit comments

Comments
 (0)