bashly 0.6.8 → 0.6.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f0b22d0ac9bf41f91fed37e60996fa5175346a9e2e5e56158b760ededfef2a0
4
- data.tar.gz: 39ffcd5e199b6b79fd04a4bd38e93d4703ad7aebce0ec4970b8250280112c5db
3
+ metadata.gz: c76c3000789b54fa2d9be8f7a61d93556e6a8665f4ea7769588797a25fa78f49
4
+ data.tar.gz: a5a0eae36b00cf879082836211f2aa21b38b280993e2dccfc907f3e6fcb99104
5
5
  SHA512:
6
- metadata.gz: fd57501ae8a6fc8b3072320dc7358b2097ef9b280b5e8b80efa52155c91a9fe56abbb73821ffc2b8a8a8e55caf7a61dd46ef29d8c88f49b2232cddfccff77a32
7
- data.tar.gz: 83f7293f9b543d6330daedf6421678b3c321166751708dba0985d7aa18a9191510a3832bd2ac8d2a3510b09f9eb2064403798b38894c7227a63f8c86f52f04dc
6
+ metadata.gz: ae77cd9b8fa000d9e12f2948fb5c41bd02e34bd6bdcdd91f3a3a627778f66c9472e956e92fb65b502fa78fe2e77ba6c4dfb12c1f35abed36f1012ce502b7cab9
7
+ data.tar.gz: 5e14659441abf6b11ca53a5d0d656e5097519acc7a2e842f3cdfb6374fd52a0403c6ddeacf4f193da1ab18687de909b074e4d581ce4700c9fc28e2b589ef961d
data/README.md CHANGED
@@ -40,7 +40,7 @@ a [docker image](https://hub.docker.com/r/dannyben/bashly).
40
40
  file for you ([example](https://github.com/DannyBen/bashly/tree/master/examples/minimal#bashlyyml)).
41
41
  2. Bashly then automatically generates a bash script (when you run
42
42
  `bashly generate`) that can parse and validate user input, provide help
43
- messages, and run your code for each command ([example](https://github.com/DannyBen/bashly/blob/master/examples/minimal/download)).
43
+ messages, and run your code for each command.
44
44
  3. Your code for each command is kept in a separate file, and can be merged
45
45
  again if you change it ([example](https://github.com/DannyBen/bashly/blob/master/examples/minimal/src/root_command.sh)).
46
46
 
@@ -67,11 +67,13 @@ Bashly is responsible for:
67
67
  ## Contributing / Support
68
68
 
69
69
  If you experience any issue, have a question or a suggestion, or if you wish
70
- to contribute, feel free to [open an issue][issues].
70
+ to contribute, feel free to [open an issue][issues] or
71
+ [start a discussion][discussions].
71
72
 
72
73
 
73
74
 
74
75
  [issues]: https://github.com/DannyBen/bashly/issues
76
+ [discussions]: https://github.com/DannyBen/bashly/discussions
75
77
  [docs]: https://bashly.dannyb.co/
76
78
  [examples]: https://github.com/DannyBen/bashly/tree/master/examples#bashly-examples
77
79
 
@@ -8,6 +8,7 @@ module Bashly
8
8
  usage "bashly add config [--force]"
9
9
  usage "bashly add colors [--force]"
10
10
  usage "bashly add yaml [--force]"
11
+ usage "bashly add validations [--force]"
11
12
  usage "bashly add comp FORMAT [OUTPUT]"
12
13
  usage "bashly add (-h|--help)"
13
14
 
@@ -21,6 +22,7 @@ module Bashly
21
22
  command "config", "Add standard functions for handling INI files to the lib directory."
22
23
  command "colors", "Add standard functions for printing colorful and formatted text to the lib directory."
23
24
  command "yaml", "Add standard functions for reading YAML files to the lib directory."
25
+ command "validations", "Add argument validation functions to the lib directory."
24
26
  command "comp", "Generate a bash completions script or function."
25
27
 
26
28
  example "bashly add strings --force"
@@ -34,19 +36,23 @@ module Bashly
34
36
  end
35
37
 
36
38
  def lib_command
37
- safe_copy_lib "sample_function.sh"
39
+ safe_copy_file "sample_function.sh"
38
40
  end
39
41
 
40
42
  def config_command
41
- safe_copy_lib "config.sh"
43
+ safe_copy_file "config.sh"
42
44
  end
43
45
 
44
46
  def colors_command
45
- safe_copy_lib "colors.sh"
47
+ safe_copy_file "colors.sh"
46
48
  end
47
49
 
48
50
  def yaml_command
49
- safe_copy_lib "yaml.sh"
51
+ safe_copy_file "yaml.sh"
52
+ end
53
+
54
+ def validations_command
55
+ safe_copy_dir "validations"
50
56
  end
51
57
 
52
58
  def comp_command
@@ -68,8 +74,14 @@ module Bashly
68
74
 
69
75
  private
70
76
 
71
- def safe_copy_lib(libfile)
72
- safe_copy asset("templates/lib/#{libfile}"), "#{Settings.source_dir}/lib/#{libfile}"
77
+ def safe_copy_dir(dir)
78
+ Dir[asset("templates/lib/#{dir}/*.sh")].sort.each do |file|
79
+ safe_copy_file "#{dir}/#{File.basename file}"
80
+ end
81
+ end
82
+
83
+ def safe_copy_file(file)
84
+ safe_copy asset("templates/lib/#{file}"), "#{Settings.source_dir}/lib/#{file}"
73
85
  end
74
86
 
75
87
  def safe_copy(source, target)
@@ -31,12 +31,16 @@ module Bashly
31
31
  flags.map(&:name) + flags.map(&:short)
32
32
  end
33
33
 
34
+ def completion_allowed_args
35
+ flags.map(&:allowed).flatten + args.map(&:allowed).flatten
36
+ end
37
+
34
38
  def completion_words(with_version: false)
35
39
  trivial_flags = %w[--help -h]
36
40
  trivial_flags += %w[--version -v] if with_version
37
41
  all = (
38
42
  command_names + trivial_flags +
39
- completion_flag_names
43
+ completion_flag_names + completion_allowed_args
40
44
  )
41
45
 
42
46
  all += completions if completions
@@ -25,6 +25,7 @@ module Bashly
25
25
  parent_name
26
26
  required
27
27
  short
28
+ validate
28
29
  version
29
30
  ]
30
31
 
@@ -4,29 +4,42 @@
4
4
  #
5
5
  # Usage:
6
6
  # Use any of the functions below to color or format a portion of a string.
7
- #
7
+ #
8
8
  # echo "before $(red this is red) after"
9
9
  # echo "before $(green_bold this is green_bold) after"
10
10
  #
11
+ # Color output will be disabled if `NO_COLOR` environment variable is set
12
+ # in compliance with https://no-color.org/
13
+ #
11
14
  # ---
12
15
 
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" "$*"; }
16
+ print_in_color() {
17
+ local color="$1"
18
+ shift
19
+ if [[ -z ${NO_COLOR+x} ]]; then
20
+ printf "$color%b\e[0m\n" "$*";
21
+ else
22
+ printf "%b\n" "$*";
23
+ fi
24
+ }
25
+
26
+ red() { print_in_color "\e[31m" "$*"; }
27
+ green() { print_in_color "\e[32m" "$*"; }
28
+ yellow() { print_in_color "\e[33m" "$*"; }
29
+ blue() { print_in_color "\e[34m" "$*"; }
30
+ magenta() { print_in_color "\e[35m" "$*"; }
31
+ cyan() { print_in_color "\e[36m" "$*"; }
32
+ bold() { print_in_color "\e[1m" "$*"; }
33
+ underlined() { print_in_color "\e[4m" "$*"; }
34
+ red_bold() { print_in_color "\e[1;31m" "$*"; }
35
+ green_bold() { print_in_color "\e[1;32m" "$*"; }
36
+ yellow_bold() { print_in_color "\e[1;33m" "$*"; }
37
+ blue_bold() { print_in_color "\e[1;34m" "$*"; }
38
+ magenta_bold() { print_in_color "\e[1;35m" "$*"; }
39
+ cyan_bold() { print_in_color "\e[1;36m" "$*"; }
40
+ red_underlined() { print_in_color "\e[4;31m" "$*"; }
41
+ green_underlined() { print_in_color "\e[4;32m" "$*"; }
42
+ yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
43
+ blue_underlined() { print_in_color "\e[4;34m" "$*"; }
44
+ magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
45
+ cyan_underlined() { print_in_color "\e[4;36m" "$*"; }
@@ -0,0 +1,3 @@
1
+ validate_dir_exists() {
2
+ [[ -d "$1" ]] || echo "must be an existing directory"
3
+ }
@@ -0,0 +1,3 @@
1
+ validate_file_exists() {
2
+ [[ -f "$1" ]] || echo "must be an existing file"
3
+ }
@@ -0,0 +1,3 @@
1
+ validate_integer() {
2
+ [[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
3
+ }
@@ -0,0 +1,3 @@
1
+ validate_not_empty() {
2
+ [[ -z "$1" ]] && echo "must not be empty"
3
+ }
@@ -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.8"
2
+ VERSION = "0.6.9"
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
@@ -2,7 +2,8 @@
2
2
  % if args.any?
3
3
  % condition = "if"
4
4
  % args.each do |arg|
5
- <%= condition %> [[ ! ${args[<%= arg.name %>]} ]]; then
5
+ <%= condition %> [[ -z ${args[<%= arg.name %>]+x} ]]; then
6
+ <%= arg.render(:validations).indent 2 %>
6
7
  args[<%= arg.name %>]=$1
7
8
  shift
8
9
  % condition = "elif"
@@ -1,6 +1,7 @@
1
1
  # :command.required_args_filter
2
2
  % required_args.each do |arg|
3
- if [[ $1 && $1 != -* ]]; then
3
+ if [[ -n ${1+x} && $1 != -* ]]; then
4
+ <%= arg.render(:validations).indent 2 %>
4
5
  args[<%= arg.name %>]=$1
5
6
  shift
6
7
  else
@@ -1,7 +1,8 @@
1
1
  # :flag.case
2
2
  <%= aliases.join " | " %> )
3
3
  % if arg
4
- if [[ $2 ]]; then
4
+ if [[ -n ${2+x} ]]; then
5
+ <%= render(:validations).indent 4 %>
5
6
  args[<%= name %>]="$2"
6
7
  shift
7
8
  shift
@@ -0,0 +1,8 @@
1
+ # :flag.validations
2
+ % if validate
3
+ if [[ -n $(validate_<%= validate %> "$2") ]]; then
4
+ printf "<%= strings[:validation_error] %>\n" "<%= usage_string %>" "$(validate_<%= validate %> "$2")"
5
+ exit 1
6
+ fi
7
+
8
+ % end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bashly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.8
4
+ version: 0.6.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-12 00:00:00.000000000 Z
11
+ date: 2021-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole
@@ -101,11 +101,16 @@ files:
101
101
  - lib/bashly/templates/lib/colors.sh
102
102
  - lib/bashly/templates/lib/config.sh
103
103
  - lib/bashly/templates/lib/sample_function.sh
104
+ - lib/bashly/templates/lib/validations/validate_dir_exists.sh
105
+ - lib/bashly/templates/lib/validations/validate_file_exists.sh
106
+ - lib/bashly/templates/lib/validations/validate_integer.sh
107
+ - lib/bashly/templates/lib/validations/validate_not_empty.sh
104
108
  - lib/bashly/templates/lib/yaml.sh
105
109
  - lib/bashly/templates/minimal.yml
106
110
  - lib/bashly/templates/strings.yml
107
111
  - lib/bashly/version.rb
108
112
  - lib/bashly/views/argument/usage.erb
113
+ - lib/bashly/views/argument/validations.erb
109
114
  - lib/bashly/views/command/catch_all_filter.erb
110
115
  - lib/bashly/views/command/command_fallback.erb
111
116
  - lib/bashly/views/command/command_filter.erb
@@ -143,6 +148,7 @@ files:
143
148
  - lib/bashly/views/environment_variable/usage.erb
144
149
  - lib/bashly/views/flag/case.erb
145
150
  - lib/bashly/views/flag/usage.erb
151
+ - lib/bashly/views/flag/validations.erb
146
152
  - lib/bashly/views/script/bash3_bouncer.erb
147
153
  - lib/bashly/views/script/header.erb
148
154
  - lib/bashly/views/script/wrapper.erb