Skip to content

Commit d37a214

Browse files
committed
- Make default environment variables values available in initialize()
1 parent 4c29723 commit d37a214

File tree

7 files changed

+68
-6
lines changed

7 files changed

+68
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if default_environment_variables.any?
2+
= 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
9+
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cli
2+
src/*.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: default_target_location
26+
default: not-available-in-initialize
27+
help: Set the default location to download to
28+
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)