Skip to content

Commit a29a2fc

Browse files
authored
Merge pull request #668 from bashly-framework/add/config-env-suffix
Add support for per-environment settings
2 parents 6c8c697 + 110450d commit a29a2fc

File tree

4 files changed

+83
-23
lines changed

4 files changed

+83
-23
lines changed

.rubocop.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ RSpec/SpecFilePathFormat:
3737
- 'spec/bashly/concerns/completions_command_spec.rb'
3838
- 'spec/bashly/concerns/completions_flag_spec.rb'
3939

40-
# Allow longer integration examples as they are more complex by nature
40+
# Allow longer examples in some cases
4141
RSpec/ExampleLength:
4242
Exclude:
4343
- 'spec/bashly/integration/**/*'
4444
- 'spec/bashly/libraries/render*'
45+
- 'spec/bashly/script/command_spec.rb'

lib/bashly/libraries/settings/settings.yml

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
1-
# All settings are optional (with their default values provided below), and
2-
# can also be set with an environment variable with the same name, capitalized
3-
# and prefixed by `BASHLY_` - for example: BASHLY_SOURCE_DIR
1+
#-------------------------------------------------------------------------------
2+
# BASHLY SETTINGS
3+
#-------------------------------------------------------------------------------
44
#
5-
# When setting environment variables, you can use:
6-
# - "0", "false" or "no" to represent false
7-
# - "1", "true" or "yes" to represent true
5+
### Default Values
6+
#
7+
# All settings are optional, with their default values provided below
8+
#
9+
### Environment Variables
10+
#
11+
# Values can also be set using an environment variable with the same name,
12+
# capitalized and prefixed by `BASHLY_` - for example: `BASHLY_SOURCE_DIR`
13+
#
14+
# When setting environment variables, you can use:
15+
# - "0", "false" or "no" to represent false
16+
# - "1", "true" or "yes" to represent true
17+
#
18+
# Environment variables take precedence over values in the config file.
19+
#
20+
### File Location:
21+
#
22+
# Bashly looks for the settings file in these locations.
23+
# - The path defined in the `BASHLY_SETTINGS_PATH` environment variable.
24+
# - A file named `bashly-settings.yml` in the working directory.
25+
# - A file named `settings.yml` in the working directory.
26+
#
27+
### Environment Overrides:
28+
#
29+
# All options (except `env`) may be specified with an environment suffix in
30+
# order to override its value for a given environment.
31+
#
32+
# For example, when defining `formatter_production: shfmt --minify`, then
33+
# this will be the formatter used when generating the script with
34+
# `bashly generate --env production`
35+
#
36+
# Since these values take precedence over the standard values, you can define
37+
# both (i.e. `formatter: shfmt` and `formatter_production: shfmt --minify`).
838
#
9-
# If you wish to change the path to this file, set the environment variable
10-
# BASHLY_SETTINGS_PATH.
11-
1239

1340
#-------------------------------------------------------------------------------
1441
# PATH OPTIONS
@@ -52,12 +79,12 @@ strict: false
5279
tab_indent: false
5380

5481
# Choose a post-processor for the generated script:
55-
# formatter: internal # Use Bashly's internal formatter (compacts newlines)
82+
# formatter: internal # Use Bashly’s built-in formatter (removes extra newlines)
5683
# formatter: external # Run the external command `shfmt --case-indent --indent 2`
5784
# formatter: none # Disable formatting entirely
58-
# formatter: <string> # Use a custom shell command to format the script.
59-
# # The command will receive the script via stdin and
60-
# # must output the result to stdout.
85+
# formatter: <string> # Provide a custom shell command to format the script.
86+
# # The command receives the script via stdin and must
87+
# # write the result to stdout.
6188
# # Example: shfmt --minify
6289
formatter: internal
6390

@@ -102,7 +129,7 @@ usage_colors:
102129
#-------------------------------------------------------------------------------
103130

104131
# Set to 'production' or 'development'.
105-
# Determines which features are enabled in the rendered script.
132+
# Determines which features are enabled in the generated script.
106133
# Use the `enable_*` options below to adjust settings for each environment.
107134
# It is recommended to leave this set to 'development' and run
108135
# `bashly generate --env production` when the production version is needed.

lib/bashly/settings.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,27 @@ def var_aliases
160160
private
161161

162162
def get(key)
163-
case env_value key
164-
when nil then config[key.to_s]
165-
when '0', 'false', 'no' then false
166-
when '1', 'true', 'yes' then true
167-
else env_value key
168-
end
163+
ENV.has_key?(env_var_name(key)) ? value_from_env(key) : value_from_config(key)
164+
end
165+
166+
def env_var_name(key)
167+
"BASHLY_#{key.upcase}"
169168
end
170169

171-
def env_value(key)
172-
ENV["BASHLY_#{key.upcase}"]
170+
def value_from_config(key)
171+
return config[key.to_s] if key == :env
172+
173+
result = config["#{key}_#{env}"]
174+
result.nil? ? config[key.to_s] : result
175+
end
176+
177+
def value_from_env(key)
178+
result = ENV[env_var_name(key)]
179+
case result&.strip&.downcase
180+
when '0', 'false', 'no' then false
181+
when '1', 'true', 'yes' then true
182+
else result
183+
end
173184
end
174185

175186
def config

spec/bashly/settings_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@
7474
expect(subject.tab_indent).to eq ENV['BASHLY_TAB_INDENT']
7575
end
7676
end
77+
78+
context 'when using env suffix overrides' do
79+
before do
80+
reset_tmp_dir
81+
File.write 'spec/tmp/settings.yml', 'formatter_production: shfmt --minify'
82+
subject.formatter = nil
83+
end
84+
85+
it 'returns the default value when it is not the specified environment' do
86+
Dir.chdir 'spec/tmp' do
87+
expect(subject.formatter).to eq 'internal'
88+
end
89+
end
90+
91+
it 'returns the config value when it is the specified environment' do
92+
Dir.chdir 'spec/tmp' do
93+
subject.env = :production
94+
expect(subject.formatter).to eq 'shfmt --minify'
95+
end
96+
end
97+
end
7798
end
7899

79100
describe '::env' do

0 commit comments

Comments
 (0)