bashly 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba0b8832beb94705ec8b978035bdb131203cffd49fe5e17f772804dd89c148d9
4
- data.tar.gz: afbaf3411cbb6430aabe64e91bde45ab5df428b3bff31d45f59ba8adf9e8d0b9
3
+ metadata.gz: 0d80ba9b8ae35371153a3610de0d3d76e64a7a18e3fcc3500c112634b38ec13e
4
+ data.tar.gz: 908bd0775b5568471da0fe0b113fcddfefa1d1063ecafdd12019eacae8392e3d
5
5
  SHA512:
6
- metadata.gz: 9e249dae5a1be6e3110344411a175ba4169c2e7a47f817783316b181b64471a561f701bc896327782378e95647f8f5b76d57afc64709d1589f0557b7d221e4e2
7
- data.tar.gz: 69f9baac8426a5f4bce2c44f5b127e8651d2ad1f6112fb9c0a9202730e9ebd01fce34c9fcf67aa765899342a4cdd77b858b78ebf2f5efa066b8cf99bc83e7309
6
+ metadata.gz: f11a315e7d68230adedb9b73916619b30893871711cb5716f119b53621813eae23ac9c72a76248d13349924343cc3e77834f0b12c3699130309ecd0c0b9b699d
7
+ data.tar.gz: 3a19eada91503c3535b47643e18bb43b4c04d5548f5f18aac0195b8b14f3e18fb548f60a81cccf59d76ed714f8684dcdc9eec3a413b73b1e63d71d8c2d4cf875
@@ -92,7 +92,7 @@ module Bashly
92
92
  assert_string "#{key}.name", value['name']
93
93
  assert_optional_string "#{key}.help", value['help']
94
94
  assert_string_or_array "#{key}.default", value['default']
95
- assert_optional_string "#{key}.validate", value['validate']
95
+ assert_string_or_array "#{key}.validate", value['validate']
96
96
  assert_boolean "#{key}.required", value['required']
97
97
  assert_boolean "#{key}.repeatable", value['repeatable']
98
98
  assert_boolean "#{key}.unique", value['unique']
@@ -123,7 +123,7 @@ module Bashly
123
123
  assert_optional_string "#{key}.help", value['help']
124
124
  assert_optional_string "#{key}.arg", value['arg']
125
125
  assert_string_or_array "#{key}.default", value['default']
126
- assert_optional_string "#{key}.validate", value['validate']
126
+ assert_string_or_array "#{key}.validate", value['validate']
127
127
 
128
128
  assert_boolean "#{key}.private", value['private']
129
129
  assert_boolean "#{key}.repeatable", value['repeatable']
@@ -169,6 +169,7 @@ module Bashly
169
169
  assert_boolean "#{key}.required", value['required']
170
170
  assert_boolean "#{key}.private", value['private']
171
171
  assert_array "#{key}.allowed", value['allowed'], of: :string
172
+ assert_string_or_array "#{key}.validate", value['validate']
172
173
 
173
174
  refute value['required'] && value['default'], "#{key} cannot have both nub`required` and nub`default`"
174
175
  end
@@ -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
+ #-------------------------------------------------------------------------------
4
4
  #
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`).
8
38
  #
9
- # If you wish to change the path to this file, set the environment variable
10
- # BASHLY_SETTINGS_PATH.
11
-
12
39
 
13
40
  #-------------------------------------------------------------------------------
14
41
  # PATH OPTIONS
@@ -52,12 +79,12 @@ strict: false
52
79
  tab_indent: false
53
80
 
54
81
  # Choose a post-processor for the generated script:
55
- # formatter: internal # Use Bashly's internal formatter (compacts newlines)
82
+ # formatter: internal # Use Bashlys built-in formatter (removes extra newlines)
56
83
  # formatter: external # Run the external command `shfmt --case-indent --indent 2`
57
84
  # 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.
61
88
  # # Example: shfmt --minify
62
89
  formatter: internal
63
90
 
@@ -102,10 +129,10 @@ usage_colors:
102
129
  #-------------------------------------------------------------------------------
103
130
 
104
131
  # 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.
106
133
  # Use the `enable_*` options below to adjust settings for each environment.
107
134
  # It is recommended to leave this set to 'development' and run
108
- # `bashly generate --production` when the slimmer production script is needed.
135
+ # `bashly generate --env production` when the production version is needed.
109
136
  env: development
110
137
 
111
138
  # Tweak the script output by enabling or disabling some script output.
@@ -3,6 +3,8 @@ require 'shellwords'
3
3
  module Bashly
4
4
  module Script
5
5
  class Argument < Base
6
+ include Introspection::Validate
7
+
6
8
  class << self
7
9
  def option_keys
8
10
  @option_keys ||= %i[
@@ -21,13 +23,13 @@ module Bashly
21
23
  end
22
24
  end
23
25
 
24
- def usage_string
25
- required ? label : "[#{label}]"
26
- end
27
-
28
26
  def label
29
27
  repeatable ? "#{name.upcase}..." : name.upcase
30
28
  end
29
+
30
+ def usage_string
31
+ required ? label : "[#{label}]"
32
+ end
31
33
  end
32
34
  end
33
35
  end
@@ -158,7 +158,7 @@ module Bashly
158
158
 
159
159
  # Returns a mixed array of Argument and Flag objects that have validations
160
160
  def validatables
161
- @validatables ||= args.select(&:validate) + flags.select(&:validate)
161
+ @validatables ||= args.select(&:validate?) + flags.select(&:validate?)
162
162
  end
163
163
 
164
164
  private
@@ -2,11 +2,12 @@ module Bashly
2
2
  module Script
3
3
  class EnvironmentVariable < Base
4
4
  include Introspection::Visibility
5
+ include Introspection::Validate
5
6
 
6
7
  class << self
7
8
  def option_keys
8
9
  @option_keys ||= %i[
9
- allowed default help name required private validate
10
+ allowed default help name private required validate
10
11
  ]
11
12
  end
12
13
  end
@@ -5,12 +5,13 @@ module Bashly
5
5
  class Flag < Base
6
6
  include Completions::Flag
7
7
  include Introspection::Visibility
8
+ include Introspection::Validate
8
9
 
9
10
  class << self
10
11
  def option_keys
11
12
  @option_keys ||= %i[
12
13
  allowed arg completions conflicts default help long needs
13
- repeatable required short unique validate private
14
+ private repeatable required short unique validate
14
15
  ]
15
16
  end
16
17
  end
@@ -28,7 +28,7 @@ module Bashly
28
28
 
29
29
  # Returns an array of all the environment_variables with a validation
30
30
  def validated_environment_variables
31
- environment_variables.select(&:validate)
31
+ environment_variables.select(&:validate?)
32
32
  end
33
33
 
34
34
  # Returns only public environment variables, or both public and private
@@ -0,0 +1,18 @@
1
+ module Bashly
2
+ module Script
3
+ module Introspection
4
+ module Validate
5
+ # Returns an array of validations
6
+ def validate
7
+ return [] unless options['validate']
8
+
9
+ result = options['validate']
10
+ result.is_a?(Array) ? result : [result]
11
+ end
12
+
13
+ # Returns true if there are any validations defined
14
+ def validate? = validate.any?
15
+ end
16
+ end
17
+ end
18
+ end
@@ -160,20 +160,31 @@ module Bashly
160
160
  private
161
161
 
162
162
  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}"
169
168
  end
170
169
 
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
173
184
  end
174
185
 
175
186
  def config
176
- @config ||= defsult_settings.merge user_settings
187
+ @config ||= default_settings.merge user_settings
177
188
  end
178
189
 
179
190
  def user_settings
@@ -190,8 +201,8 @@ module Bashly
190
201
  end
191
202
  end
192
203
 
193
- def defsult_settings
194
- @defsult_settings ||= Config.new default_settings_path
204
+ def default_settings
205
+ @default_settings ||= Config.new default_settings_path
195
206
  end
196
207
 
197
208
  def default_settings_path
@@ -1,3 +1,3 @@
1
1
  module Bashly
2
- VERSION = '1.3.2'
2
+ VERSION = '1.3.3'
3
3
  end
@@ -1,4 +1,4 @@
1
- if validate
1
+ if validate?
2
2
  = view_marker
3
3
 
4
4
  if repeatable
@@ -6,18 +6,24 @@ if validate
6
6
  > values=''
7
7
  > eval "values=(${args['{{ name }}']})"
8
8
  > for value in "${values[@]}"; do
9
- > validation_output="$(validate_{{ validate }} "$value")"
9
+ validate.each do |funcname|
10
+ > validation_output="$(validate_{{ funcname }} "$value")"
10
11
  > if [[ -n "$validation_output" ]]; then
11
12
  > printf "{{ strings[:validation_error] }}\n" "{{ name.upcase }}" "$validation_output" >&2
12
13
  > exit 1
13
14
  > fi
15
+ end
14
16
  > done
15
17
  > fi
16
18
  else
17
- > validation_output="$(validate_{{ validate }} "${args['{{ name }}']:-}")"
18
- > if [[ -v args['{{ name }}'] && -n "$validation_output" ]]; then
19
- > printf "{{ strings[:validation_error] }}\n" "{{ name.upcase }}" "$validation_output" >&2
20
- > exit 1
19
+ > if [[ -v args['{{ name }}'] ]]; then
20
+ validate.each do |funcname|
21
+ > validation_output="$(validate_{{ funcname }} "${args['{{ name }}']:-}")"
22
+ > if [[ -n "$validation_output" ]]; then
23
+ > printf "{{ strings[:validation_error] }}\n" "{{ name.upcase }}" "$validation_output" >&2
24
+ > exit 1
25
+ > fi
26
+ end
21
27
  > fi
22
28
  >
23
29
  end
@@ -1,9 +1,14 @@
1
- if validate
1
+ if validate?
2
2
  = view_marker
3
3
 
4
- > if [[ -v {{ name.upcase }} && -n $(validate_{{ validate }} "${{ name.upcase }}") ]]; then
5
- > printf "{{ strings[:environment_variable_validation_error] }}\n" "{{ usage_string }}" "$(validate_{{ validate }} "${{ name.upcase }}")" >&2
6
- > exit 1
4
+ > if [[ -v {{ name.upcase }} ]]; then
5
+ validate.each do |funcname|
6
+ > validation_output="$(validate_{{ funcname }} "${{ name.upcase }}")"
7
+ > if [[ -n "${validation_output}" ]]; then
8
+ > printf "{{ strings[:environment_variable_validation_error] }}\n" "{{ usage_string }}" "$validation_output" >&2
9
+ > exit 1
10
+ > fi
11
+ end
7
12
  > fi
8
13
  >
9
14
  end
@@ -1,4 +1,4 @@
1
- if validate
1
+ if validate?
2
2
  = view_marker
3
3
 
4
4
  if repeatable
@@ -6,20 +6,24 @@ if validate
6
6
  > values=''
7
7
  > eval "values=(${args['{{ long }}']})"
8
8
  > for value in "${values[@]}"; do
9
- > validation_output="$(validate_{{ validate }} "$value")"
9
+ validate.each do |funcname|
10
+ > validation_output="$(validate_{{ funcname }} "$value")"
10
11
  > if [[ -n "$validation_output" ]]; then
11
12
  > printf "{{ strings[:validation_error] }}\n" "{{ usage_string }}" "$validation_output" >&2
12
13
  > exit 1
13
14
  > fi
14
15
  > done
16
+ end
15
17
  > fi
16
18
  else
17
19
  > if [[ -v args['{{ long }}'] ]]; then
18
- > validation_output="$(validate_{{ validate }} "${args['{{ long }}']:-}")"
20
+ validate.each do |funcname|
21
+ > validation_output="$(validate_{{ funcname }} "${args['{{ long }}']:-}")"
19
22
  > if [[ -n "${validation_output}" ]]; then
20
23
  > printf "{{ strings[:validation_error] }}\n" "{{ usage_string }}" "$validation_output" >&2
21
24
  > exit 1
22
25
  > fi
26
+ end
23
27
  > fi
24
28
  >
25
29
  end
data/lib/bashly.rb CHANGED
@@ -28,7 +28,7 @@ module Bashly
28
28
  module Introspection
29
29
  autoloads 'bashly/script/introspection', %i[
30
30
  Arguments Commands Dependencies EnvironmentVariables Examples Flags
31
- Variables Visibility
31
+ Validate Variables Visibility
32
32
  ]
33
33
  end
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bashly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
@@ -260,6 +260,7 @@ files:
260
260
  - lib/bashly/script/introspection/environment_variables.rb
261
261
  - lib/bashly/script/introspection/examples.rb
262
262
  - lib/bashly/script/introspection/flags.rb
263
+ - lib/bashly/script/introspection/validate.rb
263
264
  - lib/bashly/script/introspection/variables.rb
264
265
  - lib/bashly/script/introspection/visibility.rb
265
266
  - lib/bashly/script/variable.rb