bashly 0.6.7 → 0.7.1

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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/lib/bashly/cli.rb +1 -0
  4. data/lib/bashly/commands/add.rb +35 -82
  5. data/lib/bashly/commands/generate.rb +55 -9
  6. data/lib/bashly/commands/init.rb +1 -1
  7. data/lib/bashly/commands/preview.rb +2 -2
  8. data/lib/bashly/commands/validate.rb +19 -0
  9. data/lib/bashly/concerns/asset_helper.rb +4 -0
  10. data/lib/bashly/concerns/completions.rb +5 -1
  11. data/lib/bashly/config_validator.rb +135 -0
  12. data/lib/bashly/extensions/file.rb +13 -0
  13. data/lib/bashly/extensions/string.rb +5 -1
  14. data/lib/bashly/libraries/base.rb +19 -0
  15. data/lib/bashly/libraries/completions.rb +14 -0
  16. data/lib/bashly/libraries/completions_function.rb +38 -0
  17. data/lib/bashly/libraries/completions_script.rb +29 -0
  18. data/lib/bashly/libraries/completions_yaml.rb +27 -0
  19. data/lib/bashly/libraries.yml +39 -0
  20. data/lib/bashly/library.rb +63 -0
  21. data/lib/bashly/refinements/compose_refinements.rb +45 -0
  22. data/lib/bashly/{models → script}/argument.rb +1 -1
  23. data/lib/bashly/{models → script}/base.rb +4 -2
  24. data/lib/bashly/{models → script}/command.rb +11 -22
  25. data/lib/bashly/{models → script}/environment_variable.rb +1 -1
  26. data/lib/bashly/{models → script}/flag.rb +1 -1
  27. data/lib/bashly/{models/script.rb → script/wrapper.rb} +21 -3
  28. data/lib/bashly/templates/lib/colors.sh +41 -31
  29. data/lib/bashly/templates/lib/config.sh +34 -35
  30. data/lib/bashly/templates/lib/sample_function.sh +10 -10
  31. data/lib/bashly/templates/lib/validations/validate_dir_exists.sh +4 -0
  32. data/lib/bashly/templates/lib/validations/validate_file_exists.sh +4 -0
  33. data/lib/bashly/templates/lib/validations/validate_integer.sh +4 -0
  34. data/lib/bashly/templates/lib/validations/validate_not_empty.sh +4 -0
  35. data/lib/bashly/templates/lib/yaml.sh +12 -15
  36. data/lib/bashly/templates/strings.yml +1 -0
  37. data/lib/bashly/version.rb +1 -1
  38. data/lib/bashly/views/argument/validations.erb +8 -0
  39. data/lib/bashly/views/command/command_filter.erb +1 -1
  40. data/lib/bashly/views/command/default_assignments.erb +2 -2
  41. data/lib/bashly/views/command/default_initialize_script.erb +6 -6
  42. data/lib/bashly/views/command/environment_variables_filter.erb +1 -1
  43. data/lib/bashly/views/command/fixed_flags_filter.erb +1 -1
  44. data/lib/bashly/views/command/initialize.erb +1 -6
  45. data/lib/bashly/views/command/parse_requirements.erb +1 -1
  46. data/lib/bashly/views/command/parse_requirements_case.erb +2 -1
  47. data/lib/bashly/views/command/required_args_filter.erb +1 -5
  48. data/lib/bashly/views/command/required_flags_filter.erb +1 -4
  49. data/lib/bashly/views/command/run.erb +4 -4
  50. data/lib/bashly/views/command/usage_commands.erb +1 -1
  51. data/lib/bashly/views/flag/case.erb +2 -1
  52. data/lib/bashly/views/flag/validations.erb +8 -0
  53. data/lib/bashly/views/wrapper/bash3_bouncer.erb +5 -0
  54. data/lib/bashly/views/{script → wrapper}/header.erb +1 -0
  55. data/lib/bashly/views/{script → wrapper}/wrapper.erb +0 -0
  56. data/lib/bashly.rb +3 -1
  57. metadata +28 -10
@@ -0,0 +1,27 @@
1
+ module Bashly
2
+ module Libraries
3
+ class CompletionsYAML < Completions
4
+ def files
5
+ [
6
+ {
7
+ path: target_path,
8
+ content: command.completion_data.to_yaml
9
+ }
10
+ ]
11
+ end
12
+
13
+ def post_install_message
14
+ <<~EOF
15
+ This file can be converted to a completions script using the !txtgrn!completely!txtrst! gem.
16
+ EOF
17
+ end
18
+
19
+ private
20
+
21
+ def target_path
22
+ @target_path ||= args[0] || "#{Settings.target_dir}/completions.yml"
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ colors:
2
+ files:
3
+ - source: "templates/lib/colors.sh"
4
+ target: "%{user_source_dir}/lib/colors.sh"
5
+
6
+ config:
7
+ files:
8
+ - source: "templates/lib/config.sh"
9
+ target: "%{user_source_dir}/lib/config.sh"
10
+
11
+ yaml:
12
+ files:
13
+ - source: "templates/lib/yaml.sh"
14
+ target: "%{user_source_dir}/lib/yaml.sh"
15
+
16
+ lib:
17
+ files:
18
+ - source: "templates/lib/sample_function.sh"
19
+ target: "%{user_source_dir}/lib/sample_function.sh"
20
+
21
+ strings:
22
+ files:
23
+ - source: "templates/strings.yml"
24
+ target: "%{user_source_dir}/bashly-strings.yml"
25
+
26
+ validations:
27
+ files:
28
+ - source: "templates/lib/validations/validate_dir_exists.sh"
29
+ target: "%{user_source_dir}/lib/validations/validate_dir_exists.sh"
30
+ - source: "templates/lib/validations/validate_file_exists.sh"
31
+ target: "%{user_source_dir}/lib/validations/validate_file_exists.sh"
32
+ - source: "templates/lib/validations/validate_integer.sh"
33
+ target: "%{user_source_dir}/lib/validations/validate_integer.sh"
34
+ - source: "templates/lib/validations/validate_not_empty.sh"
35
+ target: "%{user_source_dir}/lib/validations/validate_not_empty.sh"
36
+
37
+ completions: :CompletionsFunction
38
+ completions_script: :CompletionsScript
39
+ completions_yaml: :CompletionsYAML
@@ -0,0 +1,63 @@
1
+ module Bashly
2
+ class Library
3
+ class << self
4
+ def exist?(name)
5
+ config.has_key? name.to_s
6
+ end
7
+
8
+ def config
9
+ @config ||= YAML.load_file(config_path)
10
+ end
11
+
12
+ def config_path
13
+ @config_path ||= File.expand_path 'libraries.yml', __dir__
14
+ end
15
+ end
16
+
17
+ include AssetHelper
18
+ attr_reader :name, :args
19
+
20
+ def initialize(name, *args)
21
+ @name, @args = name.to_s, args
22
+ end
23
+
24
+ def files
25
+ if custom_handler
26
+ custom_handler.files
27
+
28
+ else
29
+ config['files'].map do |file|
30
+ { path: file['target'] % target_file_args,
31
+ content: asset_content(file['source']) }
32
+ end
33
+ end
34
+ end
35
+
36
+ def post_install_message
37
+ if custom_handler
38
+ custom_handler.post_install_message
39
+ else
40
+ config['post_install_message']
41
+ end
42
+ end
43
+
44
+ def find_file(path)
45
+ files.select { |f| f[:path] == path }.first
46
+ end
47
+
48
+ private
49
+
50
+ def custom_handler
51
+ return nil unless config.is_a? Symbol
52
+ @custom_handler ||= Bashly::Libraries.const_get(config).new(*args)
53
+ end
54
+
55
+ def config
56
+ @config ||= self.class.config[name]
57
+ end
58
+
59
+ def target_file_args
60
+ { user_source_dir: Settings.source_dir }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+
3
+ module ComposeRefinements
4
+ refine Hash do
5
+ def compose(keyword = 'import')
6
+ result = {}
7
+ each do |k, v|
8
+ if k.to_s == keyword
9
+ sub = safe_load_yaml(v).compose keyword
10
+ if sub.is_a? Array
11
+ result = sub
12
+ else
13
+ result.merge! sub
14
+ end
15
+ elsif v.respond_to? :compose
16
+ result[k] = v.compose keyword
17
+ else
18
+ result[k] = v
19
+ end
20
+ end
21
+ result
22
+ end
23
+
24
+ def safe_load_yaml(path)
25
+ loaded = YAML.load_file path
26
+ return loaded if loaded.is_a? Array or loaded.is_a? Hash
27
+ raise Bashly::ConfigurationError, "Cannot find a valid YAML in !txtgrn!#{path}"
28
+
29
+ rescue Errno::ENOENT
30
+ raise Bashly::ConfigurationError, "Cannot find import file !txtgrn!#{path}"
31
+ end
32
+ end
33
+
34
+ refine Array do
35
+ def compose(keyword = 'import')
36
+ map do |x|
37
+ if x.respond_to? :compose
38
+ x.compose keyword
39
+ else
40
+ x
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Argument < Base
4
4
  def usage_string
5
5
  required ? name.upcase : "[#{name.upcase}]"
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Base
4
4
  include Renderable
5
5
 
@@ -23,15 +23,17 @@ module Bashly
23
23
  long
24
24
  name
25
25
  parent_name
26
+ private
26
27
  required
27
28
  short
29
+ validate
28
30
  version
29
31
  ]
30
32
 
31
33
  def initialize(options)
32
34
  raise Error, "Invalid options provided" unless options.respond_to? :keys
33
35
  @options = options
34
- verify if respond_to? :verify
36
+ validate_options if respond_to? :validate_options
35
37
  end
36
38
 
37
39
  def optional
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Command < Base
4
4
  include Completions
5
5
 
@@ -32,18 +32,15 @@ module Bashly
32
32
 
33
33
  # Returns a label for the catch_all directive
34
34
  def catch_all_label
35
- return nil unless catch_all
36
-
37
- if catch_all.is_a? String
38
- "#{catch_all.upcase}..."
39
- elsif catch_all.is_a?(Hash) and catch_all['label'].is_a?(String)
40
- "#{catch_all['label'].upcase}..."
41
- else
42
- "..."
35
+ case catch_all
36
+ when nil then nil
37
+ when String then "#{catch_all.upcase}..."
38
+ when Hash then "#{catch_all['label'].upcase}..."
39
+ else "..."
43
40
  end
44
41
  end
45
42
 
46
- # Returns a used defined help string for the catch_all directive
43
+ # Returns a user defined help string for the catch_all directive
47
44
  def catch_all_help
48
45
  return nil unless catch_all
49
46
 
@@ -154,7 +151,7 @@ module Bashly
154
151
  default_content = placeholder ? "echo \"error: cannot load file\"" : ''
155
152
 
156
153
  content = if File.exist? path
157
- File.read path
154
+ File.read(path).remove_front_matter
158
155
  else
159
156
  default_content
160
157
  end
@@ -213,9 +210,9 @@ module Bashly
213
210
  end
214
211
 
215
212
  # Raise an exception if there are some serious issues with the command
216
- # definition.
217
- def verify
218
- verify_commands if commands.any?
213
+ # definition. This is called by Base#initialize.
214
+ def validate_options
215
+ Bashly::ConfigValidator.new(options).validate
219
216
  end
220
217
 
221
218
  # Returns an array of all the args with a whitelist
@@ -228,14 +225,6 @@ module Bashly
228
225
  flags.select &:allowed
229
226
  end
230
227
 
231
- private
232
-
233
- def verify_commands
234
- if args.any? or flags.any?
235
- raise ConfigurationError, "Error in the !txtgrn!#{full_name}!txtrst! command.\nThe !txtgrn!commands!txtrst! key cannot be at the same level as the !txtgrn!args!txtrst! or !txtgrn!flags!txtrst! keys."
236
- end
237
- end
238
-
239
228
  end
240
229
  end
241
230
  end
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class EnvironmentVariable < Base
4
4
  def usage_string(extended: false)
5
5
  result = [name.upcase]
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Flag < Base
4
4
  def aliases
5
5
  if long and short
@@ -1,6 +1,6 @@
1
1
  module Bashly
2
- module Models
3
- class Script
2
+ module Script
3
+ class Wrapper
4
4
  include Renderable
5
5
 
6
6
  attr_reader :command, :function_name
@@ -22,12 +22,30 @@ module Bashly
22
22
  private
23
23
 
24
24
  def header
25
- @header ||= render('header')
25
+ @header ||= header!
26
+ end
27
+
28
+ def header!
29
+ if File.exist? custom_header_path
30
+ File.read custom_header_path
31
+ else
32
+ default_header
33
+ end
34
+ end
35
+
36
+ def default_header
37
+ result = render('header')
38
+ result += render('bash3_bouncer') unless function_name
39
+ result
26
40
  end
27
41
 
28
42
  def body
29
43
  @body ||= command.render('master_script')
30
44
  end
45
+
46
+ def custom_header_path
47
+ @custom_header_path ||= "#{Settings.source_dir}/header.sh"
48
+ end
31
49
  end
32
50
  end
33
51
  end
@@ -1,32 +1,42 @@
1
- # ---
2
- # Color functions
3
- # This file is a part of Bashly standard library
4
- #
5
- # Usage:
6
- # Use any of the functions below to color or format a portion of a string.
7
- #
8
- # echo "before $(red this is red) after"
9
- # echo "before $(green_bold this is green_bold) after"
10
- #
11
- # ---
1
+ ## Color functions [@bashly-upgrade colors]
2
+ ## This file is a part of Bashly standard library
3
+ ##
4
+ ## Usage:
5
+ ## Use any of the functions below to color or format a portion of a string.
6
+ ##
7
+ ## echo "before $(red this is red) after"
8
+ ## echo "before $(green_bold this is green_bold) after"
9
+ ##
10
+ ## Color output will be disabled if `NO_COLOR` environment variable is set
11
+ ## in compliance with https://no-color.org/
12
+ ##
13
+ print_in_color() {
14
+ local color="$1"
15
+ shift
16
+ if [[ -z ${NO_COLOR+x} ]]; then
17
+ printf "$color%b\e[0m\n" "$*";
18
+ else
19
+ printf "%b\n" "$*";
20
+ fi
21
+ }
12
22
 
13
- red() { printf "\e[31m%b\e[0m\n" "$*"; }
14
- green() { printf "\e[32m%b\e[0m\n" "$*"; }
15
- yellow() { printf "\e[33m%b\e[0m\n" "$*"; }
16
- blue() { printf "\e[34m%b\e[0m\n" "$*"; }
17
- magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
18
- cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
19
- bold() { printf "\e[1m%b\e[0m\n" "$*"; }
20
- underlined() { printf "\e[4m%b\e[0m\n" "$*"; }
21
- red_bold() { printf "\e[1;31m%b\e[0m\n" "$*"; }
22
- green_bold() { printf "\e[1;32m%b\e[0m\n" "$*"; }
23
- yellow_bold() { printf "\e[1;33m%b\e[0m\n" "$*"; }
24
- blue_bold() { printf "\e[1;34m%b\e[0m\n" "$*"; }
25
- magenta_bold() { printf "\e[1;35m%b\e[0m\n" "$*"; }
26
- cyan_bold() { printf "\e[1;36m%b\e[0m\n" "$*"; }
27
- red_underlined() { printf "\e[4;31m%b\e[0m\n" "$*"; }
28
- green_underlined() { printf "\e[4;32m%b\e[0m\n" "$*"; }
29
- yellow_underlined() { printf "\e[4;33m%b\e[0m\n" "$*"; }
30
- blue_underlined() { printf "\e[4;34m%b\e[0m\n" "$*"; }
31
- magenta_underlined() { printf "\e[4;35m%b\e[0m\n" "$*"; }
32
- cyan_underlined() { printf "\e[4;36m%b\e[0m\n" "$*"; }
23
+ red() { print_in_color "\e[31m" "$*"; }
24
+ green() { print_in_color "\e[32m" "$*"; }
25
+ yellow() { print_in_color "\e[33m" "$*"; }
26
+ blue() { print_in_color "\e[34m" "$*"; }
27
+ magenta() { print_in_color "\e[35m" "$*"; }
28
+ cyan() { print_in_color "\e[36m" "$*"; }
29
+ bold() { print_in_color "\e[1m" "$*"; }
30
+ underlined() { print_in_color "\e[4m" "$*"; }
31
+ red_bold() { print_in_color "\e[1;31m" "$*"; }
32
+ green_bold() { print_in_color "\e[1;32m" "$*"; }
33
+ yellow_bold() { print_in_color "\e[1;33m" "$*"; }
34
+ blue_bold() { print_in_color "\e[1;34m" "$*"; }
35
+ magenta_bold() { print_in_color "\e[1;35m" "$*"; }
36
+ cyan_bold() { print_in_color "\e[1;36m" "$*"; }
37
+ red_underlined() { print_in_color "\e[4;31m" "$*"; }
38
+ green_underlined() { print_in_color "\e[4;32m" "$*"; }
39
+ yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
40
+ blue_underlined() { print_in_color "\e[4;34m" "$*"; }
41
+ magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
42
+ cyan_underlined() { print_in_color "\e[4;36m" "$*"; }
@@ -1,24 +1,23 @@
1
- # ---
2
- # Config functions
3
- # This file is a part of Bashly standard library
4
- #
5
- # Usage:
6
- # - In your script, set the CONFIG_FILE variable. For rxample:
7
- # CONFIG_FILE=settings.ini.
8
- # If it is unset, it will default to 'config.ini'.
9
- # - Use any of the functions below to access the config file.
10
- # ---
11
-
12
- # Create a new config file.
13
- # There is normally no need to use this function, it is used by other
14
- # functions as needed.
1
+ ## Config functions [@bashly-upgrade config]
2
+ ## This file is a part of Bashly standard library
3
+ ##
4
+ ## Usage:
5
+ ## - In your script, set the CONFIG_FILE variable. For rxample:
6
+ ## CONFIG_FILE=settings.ini.
7
+ ## If it is unset, it will default to 'config.ini'.
8
+ ## - Use any of the functions below to access the config file.
9
+ ##
10
+ ## Create a new config file.
11
+ ## There is normally no need to use this function, it is used by other
12
+ ## functions as needed.
13
+ ##
15
14
  config_init() {
16
15
  CONFIG_FILE=${CONFIG_FILE:=config.ini}
17
16
  [[ -f "$CONFIG_FILE" ]] || touch "$CONFIG_FILE"
18
17
  }
19
18
 
20
- # Get a value from the config.
21
- # Usage: result=$(config_get hello)
19
+ ## Get a value from the config.
20
+ ## Usage: result=$(config_get hello)
22
21
  config_get() {
23
22
  local key=$1
24
23
  local regex="^$key *= *(.+)$"
@@ -36,8 +35,8 @@ config_get() {
36
35
  echo "$value"
37
36
  }
38
37
 
39
- # Add or update a key=value pair in the config.
40
- # Usage: config_set key value
38
+ ## Add or update a key=value pair in the config.
39
+ ## Usage: config_set key value
41
40
  config_set() {
42
41
  local key=$1
43
42
  shift
@@ -68,8 +67,8 @@ config_set() {
68
67
  printf "%b\n" "$output" > "$CONFIG_FILE"
69
68
  }
70
69
 
71
- # Delete a key from the config.
72
- # Usage: config_del key
70
+ ## Delete a key from the config.
71
+ ## Usage: config_del key
73
72
  config_del() {
74
73
  local key=$1
75
74
 
@@ -87,19 +86,19 @@ config_del() {
87
86
  printf "%b\n" "$output" > "$CONFIG_FILE"
88
87
  }
89
88
 
90
- # Show the config file
89
+ ## Show the config file
91
90
  config_show() {
92
91
  config_init
93
92
  cat "$CONFIG_FILE"
94
93
  }
95
94
 
96
- # Return an array of the keys in the config file.
97
- # Usage:
98
- #
99
- # for k in $(config_keys); do
100
- # echo "- $k = $(config_get "$k")";
101
- # done
102
- #
95
+ ## Return an array of the keys in the config file.
96
+ ## Usage:
97
+ ##
98
+ ## for k in $(config_keys); do
99
+ ## echo "- $k = $(config_get "$k")";
100
+ ## done
101
+ ##
103
102
  config_keys() {
104
103
  local regex="^([a-zA-Z0-9_\-\/\.]+) *="
105
104
 
@@ -117,13 +116,13 @@ config_keys() {
117
116
  echo "${keys[@]}"
118
117
  }
119
118
 
120
- # Returns true if the specified key exists in the config file.
121
- # Usage:
122
- #
123
- # if config_has_key "key" ; then
124
- # echo "key exists"
125
- # fi
126
- #
119
+ ## Returns true if the specified key exists in the config file.
120
+ ## Usage:
121
+ ##
122
+ ## if config_has_key "key" ; then
123
+ ## echo "key exists"
124
+ ## fi
125
+ ##
127
126
  config_has_key() {
128
127
  [[ $(config_get "$1") ]]
129
128
  }
@@ -1,13 +1,13 @@
1
- # Add any function here that is needed in more than one parts of your
2
- # application, or that you otherwise wish to extract from the main function
3
- # scripts.
4
- #
5
- # Note that code here should be wrapped inside bash functions, and it is
6
- # recommended to have a separate file for each function.
7
- #
8
- # Subdirectories will also be scanned for *.sh, so you have no reason not
9
- # to organize your code neatly.
10
- #
1
+ ## Add any function here that is needed in more than one parts of your
2
+ ## application, or that you otherwise wish to extract from the main function
3
+ ## scripts.
4
+ ##
5
+ ## Note that code here should be wrapped inside bash functions, and it is
6
+ ## recommended to have a separate file for each function.
7
+ ##
8
+ ## Subdirectories will also be scanned for *.sh, so you have no reason not
9
+ ## to organize your code neatly.
10
+ ##
11
11
  sample_function() {
12
12
  echo "it works"
13
13
  }
@@ -0,0 +1,4 @@
1
+ ## [@bashly-upgrade validations]
2
+ validate_dir_exists() {
3
+ [[ -d "$1" ]] || echo "must be an existing directory"
4
+ }
@@ -0,0 +1,4 @@
1
+ ## [@bashly-upgrade validations]
2
+ validate_file_exists() {
3
+ [[ -f "$1" ]] || echo "must be an existing file"
4
+ }
@@ -0,0 +1,4 @@
1
+ ## [@bashly-upgrade validations]
2
+ validate_integer() {
3
+ [[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
4
+ }
@@ -0,0 +1,4 @@
1
+ ## [@bashly-upgrade validations]
2
+ validate_not_empty() {
3
+ [[ -z "$1" ]] && echo "must not be empty"
4
+ }
@@ -1,18 +1,15 @@
1
- # ---
2
- # YAML parser
3
- # This file is a part of Bashly standard library
4
- # Does not support arrays, only hashes
5
- #
6
- # Source: https://stackoverflow.com/a/21189044/413924
7
- #
8
- # Usage:
9
- #
10
- # yaml_load "settings.yml" # print variables
11
- # yaml_load "settings.yml" "config_" # use prefix
12
- # eval $(yaml_load "settings.yml") # create variables in scope
13
- #
14
- # ---
15
-
1
+ ## YAML parser [@bashly-upgrade yaml]
2
+ ## This file is a part of Bashly standard library
3
+ ## Does not support arrays, only hashes
4
+ ##
5
+ ## Source: https://stackoverflow.com/a/21189044/413924
6
+ ##
7
+ ## Usage:
8
+ ##
9
+ ## yaml_load "settings.yml" # print variables
10
+ ## yaml_load "settings.yml" "config_" # use prefix
11
+ ## eval $(yaml_load "settings.yml") # create variables in scope
12
+ ##
16
13
  yaml_load() {
17
14
  local prefix=$2
18
15
  local s='[[:space:]]*' w='[a-zA-Z0-9_]*'
@@ -32,3 +32,4 @@ missing_dependency: "missing dependency: %{dependency}"
32
32
  disallowed_flag: "%{name} must be one of: %{allowed}"
33
33
  disallowed_argument: "%{name} must be one of: %{allowed}"
34
34
  unsupported_bash_version: "bash version 4 or higher is required"
35
+ validation_error: "validation error in %s:\\n%s"
@@ -1,3 +1,3 @@
1
1
  module Bashly
2
- VERSION = "0.6.7"
2
+ VERSION = "0.7.1"
3
3
  end
@@ -0,0 +1,8 @@
1
+ # :argument.validations
2
+ % if validate
3
+ if [[ -n $(validate_<%= validate %> "$1") ]]; then
4
+ printf "<%= strings[:validation_error] %>\n" "<%= name.upcase %>" "$(validate_<%= validate %> "$1")"
5
+ exit 1
6
+ fi
7
+
8
+ % end
@@ -1,6 +1,6 @@
1
1
  # :command.command_filter
2
2
  % if commands.any?
3
- action=$1
3
+ action=${1:-}
4
4
 
5
5
  case $action in
6
6
  -* )
@@ -1,7 +1,7 @@
1
1
  # :command.default_assignments
2
2
  % default_args.each do |arg|
3
- [[ -n ${args[<%= arg.name %>]} ]] || args[<%= arg.name %>]="<%= arg.default %>"
3
+ [[ -n ${args[<%= arg.name %>]:-} ]] || args[<%= arg.name %>]="<%= arg.default %>"
4
4
  % end
5
5
  % default_flags.each do |flag|
6
- [[ -n ${args[<%= flag.long %>]} ]] || args[<%= flag.long %>]="<%= flag.default %>"
6
+ [[ -n ${args[<%= flag.long %>]:-} ]] || args[<%= flag.long %>]="<%= flag.default %>"
7
7
  % end
@@ -1,6 +1,6 @@
1
- # Code here runs inside the initialize() function
2
- # Use it for anything that you need to run before any other function, like
3
- # setting environment vairables:
4
- # CONFIG_FILE=settings.ini
5
- #
6
- # Feel free to empty (but not delete) this file.
1
+ ## Code here runs inside the initialize() function
2
+ ## Use it for anything that you need to run before any other function, like
3
+ ## setting environment vairables:
4
+ ## CONFIG_FILE=settings.ini
5
+ ##
6
+ ## Feel free to empty (but not delete) this file.