Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ RSpec/SpecFilePathFormat:
- 'spec/bashly/concerns/completions_command_spec.rb'
- 'spec/bashly/concerns/completions_flag_spec.rb'

# Allow longer integration examples as they are more complex by nature
# Allow longer examples in some cases
RSpec/ExampleLength:
Exclude:
- 'spec/bashly/integration/**/*'
- 'spec/bashly/libraries/render*'
- 'spec/bashly/script/command_spec.rb'
55 changes: 41 additions & 14 deletions lib/bashly/libraries/settings/settings.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
# All settings are optional (with their default values provided below), and
# can also be set with an environment variable with the same name, capitalized
# and prefixed by `BASHLY_` - for example: BASHLY_SOURCE_DIR
#-------------------------------------------------------------------------------
# BASHLY SETTINGS
#-------------------------------------------------------------------------------
#
# When setting environment variables, you can use:
# - "0", "false" or "no" to represent false
# - "1", "true" or "yes" to represent true
### Default Values
#
# All settings are optional, with their default values provided below
#
### Environment Variables
#
# Values can also be set using an environment variable with the same name,
# capitalized and prefixed by `BASHLY_` - for example: `BASHLY_SOURCE_DIR`
#
# When setting environment variables, you can use:
# - "0", "false" or "no" to represent false
# - "1", "true" or "yes" to represent true
#
# Environment variables take precedence over values in the config file.
#
### File Location:
#
# Bashly looks for the settings file in these locations.
# - The path defined in the `BASHLY_SETTINGS_PATH` environment variable.
# - A file named `bashly-settings.yml` in the working directory.
# - A file named `settings.yml` in the working directory.
#
### Environment Overrides:
#
# All options (except `env`) may be specified with an environment suffix in
# order to override its value for a given environment.
#
# For example, when defining `formatter_production: shfmt --minify`, then
# this will be the formatter used when generating the script with
# `bashly generate --env production`
#
# Since these values take precedence over the standard values, you can define
# both (i.e. `formatter: shfmt` and `formatter_production: shfmt --minify`).
#
# If you wish to change the path to this file, set the environment variable
# BASHLY_SETTINGS_PATH.


#-------------------------------------------------------------------------------
# PATH OPTIONS
Expand Down Expand Up @@ -52,12 +79,12 @@ strict: false
tab_indent: false

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

Expand Down Expand Up @@ -102,7 +129,7 @@ usage_colors:
#-------------------------------------------------------------------------------

# Set to 'production' or 'development'.
# Determines which features are enabled in the rendered script.
# Determines which features are enabled in the generated script.
# Use the `enable_*` options below to adjust settings for each environment.
# It is recommended to leave this set to 'development' and run
# `bashly generate --env production` when the production version is needed.
Expand Down
27 changes: 19 additions & 8 deletions lib/bashly/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,27 @@ def var_aliases
private

def get(key)
case env_value key
when nil then config[key.to_s]
when '0', 'false', 'no' then false
when '1', 'true', 'yes' then true
else env_value key
end
ENV.has_key?(env_var_name(key)) ? value_from_env(key) : value_from_config(key)
end

def env_var_name(key)
"BASHLY_#{key.upcase}"
end

def env_value(key)
ENV["BASHLY_#{key.upcase}"]
def value_from_config(key)
return config[key.to_s] if key == :env

result = config["#{key}_#{env}"]
result.nil? ? config[key.to_s] : result
end

def value_from_env(key)
result = ENV[env_var_name(key)]
case result&.strip&.downcase
when '0', 'false', 'no' then false
when '1', 'true', 'yes' then true
else result
end
end

def config
Expand Down
21 changes: 21 additions & 0 deletions spec/bashly/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@
expect(subject.tab_indent).to eq ENV['BASHLY_TAB_INDENT']
end
end

context 'when using env suffix overrides' do
before do
reset_tmp_dir
File.write 'spec/tmp/settings.yml', 'formatter_production: shfmt --minify'
subject.formatter = nil
end

it 'returns the default value when it is not the specified environment' do
Dir.chdir 'spec/tmp' do
expect(subject.formatter).to eq 'internal'
end
end

it 'returns the config value when it is the specified environment' do
Dir.chdir 'spec/tmp' do
subject.env = :production
expect(subject.formatter).to eq 'shfmt --minify'
end
end
end
end

describe '::env' do
Expand Down