bashly 0.6.9 → 0.7.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 +4 -4
- data/lib/bashly/cli.rb +1 -0
- data/lib/bashly/commands/add.rb +29 -88
- data/lib/bashly/commands/generate.rb +51 -10
- data/lib/bashly/commands/init.rb +1 -1
- data/lib/bashly/commands/preview.rb +2 -2
- data/lib/bashly/commands/validate.rb +19 -0
- data/lib/bashly/concerns/asset_helper.rb +4 -0
- data/lib/bashly/concerns/command_scopes.rb +68 -0
- data/lib/bashly/config_validator.rb +142 -0
- data/lib/bashly/extensions/file.rb +13 -0
- data/lib/bashly/extensions/string.rb +5 -1
- data/lib/bashly/libraries/base.rb +19 -0
- data/lib/bashly/libraries/completions.rb +14 -0
- data/lib/bashly/libraries/completions_function.rb +38 -0
- data/lib/bashly/libraries/completions_script.rb +29 -0
- data/lib/bashly/libraries/completions_yaml.rb +27 -0
- data/lib/bashly/libraries.yml +39 -0
- data/lib/bashly/library.rb +63 -0
- data/lib/bashly/refinements/compose_refinements.rb +45 -0
- data/lib/bashly/{models → script}/argument.rb +1 -1
- data/lib/bashly/{models → script}/base.rb +5 -4
- data/lib/bashly/script/catch_all.rb +49 -0
- data/lib/bashly/{models → script}/command.rb +10 -112
- data/lib/bashly/{models → script}/environment_variable.rb +1 -1
- data/lib/bashly/{models → script}/flag.rb +1 -1
- data/lib/bashly/{models/script.rb → script/wrapper.rb} +2 -2
- data/lib/bashly/templates/lib/colors.sh +12 -15
- data/lib/bashly/templates/lib/config.sh +34 -35
- data/lib/bashly/templates/lib/sample_function.sh +10 -10
- data/lib/bashly/templates/lib/validations/validate_dir_exists.sh +1 -0
- data/lib/bashly/templates/lib/validations/validate_file_exists.sh +1 -0
- data/lib/bashly/templates/lib/validations/validate_integer.sh +1 -0
- data/lib/bashly/templates/lib/validations/validate_not_empty.sh +1 -0
- data/lib/bashly/templates/lib/yaml.sh +12 -15
- data/lib/bashly/version.rb +1 -1
- data/lib/bashly/views/command/catch_all_filter.erb +2 -2
- data/lib/bashly/views/command/command_filter.erb +1 -1
- data/lib/bashly/views/command/default_assignments.erb +2 -2
- data/lib/bashly/views/command/default_initialize_script.erb +6 -6
- data/lib/bashly/views/command/default_root_script.erb +1 -1
- data/lib/bashly/views/command/environment_variables_filter.erb +1 -1
- data/lib/bashly/views/command/fixed_flags_filter.erb +1 -1
- data/lib/bashly/views/command/initialize.erb +1 -1
- data/lib/bashly/views/command/parse_requirements.erb +1 -1
- data/lib/bashly/views/command/parse_requirements_case.erb +2 -2
- data/lib/bashly/views/command/parse_requirements_while.erb +2 -2
- data/lib/bashly/views/command/required_args_filter.erb +1 -6
- data/lib/bashly/views/command/required_flags_filter.erb +1 -4
- data/lib/bashly/views/command/root_command.erb +1 -1
- data/lib/bashly/views/command/run.erb +4 -4
- data/lib/bashly/views/command/usage.erb +1 -1
- data/lib/bashly/views/command/usage_args.erb +3 -3
- data/lib/bashly/views/command/usage_commands.erb +1 -1
- data/lib/bashly/views/{script → wrapper}/bash3_bouncer.erb +0 -0
- data/lib/bashly/views/{script → wrapper}/header.erb +0 -0
- data/lib/bashly/views/{script → wrapper}/wrapper.erb +0 -0
- data/lib/bashly.rb +3 -1
- metadata +27 -14
@@ -0,0 +1,29 @@
|
|
1
|
+
module Bashly
|
2
|
+
module Libraries
|
3
|
+
class CompletionsScript < Completions
|
4
|
+
def files
|
5
|
+
[
|
6
|
+
{
|
7
|
+
path: target_path,
|
8
|
+
content: command.completion_script
|
9
|
+
}
|
10
|
+
]
|
11
|
+
end
|
12
|
+
|
13
|
+
def post_install_message
|
14
|
+
<<~EOF
|
15
|
+
In order to enable completions, run:
|
16
|
+
|
17
|
+
!txtpur!$ source #{target_path}
|
18
|
+
EOF
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def target_path
|
24
|
+
@target_path ||= args[0] || "#{Settings.target_dir}/completions.bash"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -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
|
2
|
+
module Script
|
3
3
|
class Base
|
4
4
|
include Renderable
|
5
5
|
|
@@ -23,6 +23,7 @@ module Bashly
|
|
23
23
|
long
|
24
24
|
name
|
25
25
|
parent_name
|
26
|
+
private
|
26
27
|
required
|
27
28
|
short
|
28
29
|
validate
|
@@ -32,7 +33,7 @@ module Bashly
|
|
32
33
|
def initialize(options)
|
33
34
|
raise Error, "Invalid options provided" unless options.respond_to? :keys
|
34
35
|
@options = options
|
35
|
-
|
36
|
+
validate_options if respond_to? :validate_options
|
36
37
|
end
|
37
38
|
|
38
39
|
def optional
|
@@ -52,9 +53,9 @@ module Bashly
|
|
52
53
|
respond_to?(method_name) ? options[key] : super
|
53
54
|
end
|
54
55
|
|
55
|
-
def
|
56
|
+
def respond_to_missing?(method_name, include_private = false)
|
56
57
|
OPTION_KEYS.include?(method_name) || super
|
57
58
|
end
|
58
59
|
end
|
59
60
|
end
|
60
|
-
end
|
61
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Bashly
|
2
|
+
module Script
|
3
|
+
class CatchAll
|
4
|
+
class << self
|
5
|
+
def from_config(config)
|
6
|
+
options = case config
|
7
|
+
when nil
|
8
|
+
{ enabled: false }
|
9
|
+
when String
|
10
|
+
{ label: config }
|
11
|
+
when Hash
|
12
|
+
{ label: config['label'], help: config['help'], required: config['required'] }
|
13
|
+
else
|
14
|
+
{}
|
15
|
+
end
|
16
|
+
|
17
|
+
new **options
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(label: nil, help: nil, required: false, enabled: true)
|
22
|
+
@label, @help, @required, @enabled = label, help, required, enabled
|
23
|
+
end
|
24
|
+
|
25
|
+
def enabled?
|
26
|
+
@enabled
|
27
|
+
end
|
28
|
+
|
29
|
+
def label
|
30
|
+
enabled? ? "#{@label&.upcase}..." : nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def help
|
34
|
+
enabled? ? @help : nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def required?
|
38
|
+
@required
|
39
|
+
end
|
40
|
+
|
41
|
+
def usage_string
|
42
|
+
return nil unless enabled?
|
43
|
+
required? ? label : "[#{label}]"
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
@@ -1,7 +1,8 @@
|
|
1
1
|
module Bashly
|
2
|
-
module
|
2
|
+
module Script
|
3
3
|
class Command < Base
|
4
4
|
include Completions
|
5
|
+
include CommandScopes
|
5
6
|
|
6
7
|
# Returns the name to be used as an action.
|
7
8
|
# - If it is the root command, the action is "root"
|
@@ -30,44 +31,8 @@ module Bashly
|
|
30
31
|
help ? "#{full_name} - #{summary}" : full_name
|
31
32
|
end
|
32
33
|
|
33
|
-
|
34
|
-
|
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
|
-
"..."
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# Returns a used defined help string for the catch_all directive
|
47
|
-
def catch_all_help
|
48
|
-
return nil unless catch_all
|
49
|
-
|
50
|
-
if catch_all.is_a?(Hash) and catch_all['help'].is_a?(String)
|
51
|
-
catch_all['help']
|
52
|
-
else
|
53
|
-
nil
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# Returns true if catch_all is required
|
58
|
-
def catch_all_required?
|
59
|
-
catch_all.is_a?(Hash) and catch_all['required']
|
60
|
-
end
|
61
|
-
|
62
|
-
# Returns a string suitable for catch_all Usage pattern
|
63
|
-
def catch_all_usage
|
64
|
-
return nil unless catch_all
|
65
|
-
catch_all_required? ? catch_all_label : "[#{catch_all_label}]"
|
66
|
-
end
|
67
|
-
|
68
|
-
# Returns only the names of the Commands
|
69
|
-
def command_names
|
70
|
-
commands.map &:name
|
34
|
+
def catch_all
|
35
|
+
@catch_all ||= CatchAll.from_config options['catch_all']
|
71
36
|
end
|
72
37
|
|
73
38
|
# Returns an array of the Commands
|
@@ -79,40 +44,6 @@ module Bashly
|
|
79
44
|
end
|
80
45
|
end
|
81
46
|
|
82
|
-
# Returns a flat array containing all the commands in this tree.
|
83
|
-
# This includes self + children + grandchildres + ...
|
84
|
-
def deep_commands
|
85
|
-
result = []
|
86
|
-
commands.each do |command|
|
87
|
-
result << command
|
88
|
-
if command.commands.any?
|
89
|
-
result += command.deep_commands
|
90
|
-
end
|
91
|
-
end
|
92
|
-
result
|
93
|
-
end
|
94
|
-
|
95
|
-
# Returns an array of all the default Args
|
96
|
-
def default_args
|
97
|
-
args.select &:default
|
98
|
-
end
|
99
|
-
|
100
|
-
# If any of this command's subcommands has the default option set to
|
101
|
-
# true, this default command will be returned, nil otherwise.
|
102
|
-
def default_command
|
103
|
-
commands.find { |c| c.default }
|
104
|
-
end
|
105
|
-
|
106
|
-
# Returns an array of all the default Environment Variables
|
107
|
-
def default_environment_variables
|
108
|
-
environment_variables.select &:default
|
109
|
-
end
|
110
|
-
|
111
|
-
# Returns an array of all the default Flags
|
112
|
-
def default_flags
|
113
|
-
flags.select &:default
|
114
|
-
end
|
115
|
-
|
116
47
|
# Returns an array of EnvironmentVariables
|
117
48
|
def environment_variables
|
118
49
|
return [] unless options["environment_variables"]
|
@@ -124,7 +55,7 @@ module Bashly
|
|
124
55
|
# Returns the bash filename that is expected to hold the user code
|
125
56
|
# for this command
|
126
57
|
def filename
|
127
|
-
"#{action_name.to_underscore}_command.sh"
|
58
|
+
options["filename"] || "#{action_name.to_underscore}_command.sh"
|
128
59
|
end
|
129
60
|
|
130
61
|
# Returns an array of Flags
|
@@ -154,7 +85,7 @@ module Bashly
|
|
154
85
|
default_content = placeholder ? "echo \"error: cannot load file\"" : ''
|
155
86
|
|
156
87
|
content = if File.exist? path
|
157
|
-
File.read
|
88
|
+
File.read(path).remove_front_matter
|
158
89
|
else
|
159
90
|
default_content
|
160
91
|
end
|
@@ -168,21 +99,6 @@ module Bashly
|
|
168
99
|
options['parents'] || []
|
169
100
|
end
|
170
101
|
|
171
|
-
# Returns an array of all the required Arguments
|
172
|
-
def required_args
|
173
|
-
args.select &:required
|
174
|
-
end
|
175
|
-
|
176
|
-
# Returns an array of all the required EnvironmentVariables
|
177
|
-
def required_environment_variables
|
178
|
-
environment_variables.select &:required
|
179
|
-
end
|
180
|
-
|
181
|
-
# Returns an array of all the required Flags
|
182
|
-
def required_flags
|
183
|
-
flags.select &:required
|
184
|
-
end
|
185
|
-
|
186
102
|
# Returns trus if this is the root command (no parents)
|
187
103
|
def root_command?
|
188
104
|
parents.empty?
|
@@ -201,7 +117,7 @@ module Bashly
|
|
201
117
|
result << arg.usage_string
|
202
118
|
end
|
203
119
|
result << "[options]" unless flags.empty?
|
204
|
-
result <<
|
120
|
+
result << catch_all.usage_string if catch_all.enabled?
|
205
121
|
result.join " "
|
206
122
|
end
|
207
123
|
|
@@ -213,27 +129,9 @@ module Bashly
|
|
213
129
|
end
|
214
130
|
|
215
131
|
# Raise an exception if there are some serious issues with the command
|
216
|
-
# definition.
|
217
|
-
def
|
218
|
-
|
219
|
-
end
|
220
|
-
|
221
|
-
# Returns an array of all the args with a whitelist
|
222
|
-
def whitelisted_args
|
223
|
-
args.select &:allowed
|
224
|
-
end
|
225
|
-
|
226
|
-
# Returns an array of all the flags with a whitelist arg
|
227
|
-
def whitelisted_flags
|
228
|
-
flags.select &:allowed
|
229
|
-
end
|
230
|
-
|
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
|
132
|
+
# definition. This is called by Base#initialize.
|
133
|
+
def validate_options
|
134
|
+
Bashly::ConfigValidator.new(options).validate
|
237
135
|
end
|
238
136
|
|
239
137
|
end
|
@@ -1,18 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
#
|
14
|
-
# ---
|
15
|
-
|
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
|
+
##
|
16
13
|
print_in_color() {
|
17
14
|
local color="$1"
|
18
15
|
shift
|
@@ -1,24 +1,23 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
21
|
-
|
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
|
-
|
40
|
-
|
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
|
-
|
72
|
-
|
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
|
-
|
89
|
+
## Show the config file
|
91
90
|
config_show() {
|
92
91
|
config_init
|
93
92
|
cat "$CONFIG_FILE"
|
94
93
|
}
|
95
94
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
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
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
}
|