nub 0.0.56 → 0.0.57
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +121 -61
- data/lib/nub/commander.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ce123bdfd67999eff41bd51cf0fb9f495e7ca3905a0943407291a2be9cff5d7
|
4
|
+
data.tar.gz: 14c438fe160de88062825296ef1c2a212b926c78e1608c8e99b965d640d850a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c833de5c3599e0695376d0bed75366ae12f44349a0854cbc7d354ec0467ad15b16630859ab25be563fa74cb00a91021f3299b3aa688046185c1782013ab50ef9
|
7
|
+
data.tar.gz: 69fb2df991e50a62c18033d12c30af8ded19305f7e41a5d00411c536eac98215965326fe79d21fa65514ba346876c077026681830e6685497c61f4e456d5b313
|
data/README.md
CHANGED
@@ -19,6 +19,9 @@ Collection of ruby utils I've used in several of my projects and wanted re-usabl
|
|
19
19
|
* [Allowed Values](#allowed-values)
|
20
20
|
* [Global Options](#global-options)
|
21
21
|
* [Configuration](#configuration)
|
22
|
+
* [Named Option Examples](#named-option-examples)
|
23
|
+
* [Positional Option Examples](#positional-option-examples)
|
24
|
+
* [Chained Command Expression Examples](#chained-command-expression-examples)
|
22
25
|
* [Help](#help)
|
23
26
|
* [Examples](#examples)
|
24
27
|
* [Indicators](#indicators)
|
@@ -38,7 +41,7 @@ Run: `bundle install --system`
|
|
38
41
|
## Commander <a name="commander"></a>
|
39
42
|
Commander was created mainly because all available options parsers seemed overly complicated and
|
40
43
|
overweight and partly because I enjoyed understanding every bit going into it. Commander offers
|
41
|
-
***git*** like command syntax
|
44
|
+
***git*** or ***kubectl*** like command syntax.
|
42
45
|
|
43
46
|
There are two kinds of paramaters that commander deals with ***commands*** and ***options***.
|
44
47
|
|
@@ -53,19 +56,20 @@ sub-command, which follow the same rules in a recursive fashion as any command,
|
|
53
56
|
Command options modify how the command behaves.
|
54
57
|
|
55
58
|
#### Chained Commands <a name="chained-commands"></a>
|
56
|
-
|
59
|
+
Chained command expressions allow a cleaner multi-command type expression with reusable options.
|
57
60
|
|
58
61
|
Whenever more than one command is used in the command line expression the expression is interpreted
|
59
62
|
as being a ***chained command expression*** a.k.a ***chained commands***. Chained commands are
|
60
63
|
executed left to right, such that you can execute the first command then the second command or more
|
61
64
|
in a single command line expression. Each command in a chained command expression may have its own
|
62
|
-
specific options (those coming after the command but before the next command)
|
63
|
-
|
64
|
-
|
65
|
+
specific options (those coming after the command but before the next command) which are taken into
|
66
|
+
account for the command as usual. However if options are omitted the options from the next command
|
67
|
+
will be used in the order they are given to satisfy the options of the command before. Only options
|
68
|
+
of the same type and position will be used.
|
65
69
|
|
66
70
|
### Options <a name="options"></a>
|
67
71
|
Options are additional parameters that are given that modify the behavior of a command. There are
|
68
|
-
two kinds of options available for use
|
72
|
+
two kinds of options available for use ***positional*** and ***named***.
|
69
73
|
|
70
74
|
#### Positional Options <a name="positional-options"></a>
|
71
75
|
Positional options are identified by the absence of preceding dash/dashes and are interpreted
|
@@ -105,75 +109,131 @@ properly.
|
|
105
109
|
Example ruby configuration:
|
106
110
|
```ruby
|
107
111
|
if __FILE__ == $0
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
112
|
+
# Create examples for app
|
113
|
+
app = 'reduce'
|
114
|
+
examples = "Full ISO Build: sudo ./#{app} clean build all -p personal\n".colorize(:green)
|
115
|
+
examples += "Rebuild initramfs: sudo ./#{app} clean build initramfs,iso -p personal\n".colorize(:green)
|
116
|
+
examples += "Rebuild multiboot: sudo ./#{app} clean build multiboot,iso -p personal\n".colorize(:green)
|
117
|
+
examples += "Clean pacman dbs: sudo ./#{app} clean --pacman\n".colorize(:green)
|
118
|
+
examples += "Build k8snode deployment: sudo ./#{app} clean build iso -d k8snode -p personal\n".colorize(:green)
|
119
|
+
examples += "Pack k8snode deployment: ./#{app} pack k8snode\n".colorize(:green)
|
120
|
+
examples += "Deploy nodes: sudo ./#{app} deploy k8snode 10,11,12\n".colorize(:green)
|
121
|
+
examples += "Deploy container: sudo ./#{app} deploy build --run\n".colorize(:green)
|
122
|
+
|
123
|
+
# Create a new instance of commander
|
124
|
+
cmdr = Commander.new(app:app, version:'0.0.1', examples:examples)
|
125
|
+
cmdr.add_global('-p|--profile=PROFILE', 'Profile to use', type:String)
|
126
|
+
cmdr.add('info', 'List build info')
|
127
|
+
cmdr.add('list', 'List out components', nodes:[
|
128
|
+
Option.new(nil, 'Components to list', type:Array, allowed:{
|
129
|
+
all: 'List all components',
|
130
|
+
boxes: 'List all boxes',
|
131
|
+
isos: 'List all isos',
|
132
|
+
images: 'List all docker images'
|
133
|
+
}),
|
134
|
+
Option.new('--raw', "Produce output suitable for automation"),
|
116
135
|
])
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
136
|
+
cmdr.add('clean', 'Clean ISO components', nodes:[
|
137
|
+
Option.new(nil, 'Components to clean', type:Array, allowed:{
|
138
|
+
all: 'Clean all components including deployments',
|
139
|
+
initramfs: 'Clean initramfs image',
|
140
|
+
multiboot: 'Clean grub multiboot image',
|
141
|
+
iso: 'Clean bootable ISO'
|
142
|
+
}),
|
143
|
+
Option.new('--pacman', "Clean pacman repos"),
|
144
|
+
Option.new('--cache', "Clean pacman/ruby package cache"),
|
145
|
+
Option.new('--vms', "Clean VMs that are no longer deployed"),
|
146
|
+
Option.new('-d|--deployments=DEPLOYMENTS', "Deployments to clean", type:Array)
|
147
|
+
])
|
148
|
+
cmdr.add('build', 'Build ISO components', nodes:[
|
149
|
+
Option.new(nil, 'Components to build', type:Array, allowed:{
|
150
|
+
all: 'Build all components including deployments',
|
151
|
+
initramfs: 'Build initramfs image',
|
152
|
+
multiboot: 'Build grub multiboot image',
|
153
|
+
iso: 'Clean bootable ISO',
|
154
|
+
}),
|
155
|
+
Option.new('-d|--deployments=DEPLOYMENTS', "Deployments to build", type:Array)
|
121
156
|
])
|
122
|
-
cmdr.add('
|
123
|
-
Option.new(nil,
|
157
|
+
cmdr.add('pack', 'Pack ISO deployments into vagrant boxes', nodes:[
|
158
|
+
Option.new(nil, "Deployments to pack", type:Array, required:true),
|
159
|
+
Option.new('--disk-size=DISK_SIZE', "Set the disk size in MB e.g. 10000", type:String),
|
160
|
+
Option.new('--force', "Pack the given deployment/s even if they already exist")
|
124
161
|
])
|
162
|
+
cmdr.add('deploy', 'Deploy VMs or containers', nodes:[
|
163
|
+
Option.new(nil, "Deployments to pack", type:Array, required:true),
|
164
|
+
Option.new(nil, "Comma delimited list of last octet IPs (e.g. 10,11,12", type:Array),
|
165
|
+
Option.new('-n|--name=NAME', "Give a name to the nodes being deployed", type:String),
|
166
|
+
Option.new('-f|--force', "Deploy the given deployment/s even if they already exist"),
|
167
|
+
Option.new('-r|--run', "Run the container with defaults"),
|
168
|
+
Option.new('-e|--exec=CMD', "Specific command to run in container", type:String),
|
169
|
+
Option.new('--ipv6', "Enable IPv6 on the given nodes"),
|
170
|
+
Option.new('--vagrantfile', "Export the Vagrantfile only"),
|
171
|
+
])
|
172
|
+
|
173
|
+
# Invoke commander parse
|
125
174
|
cmdr.parse!
|
126
175
|
|
127
|
-
|
128
|
-
|
129
|
-
|
176
|
+
# Execute 'info' command
|
177
|
+
reduce.info if cmdr[:info]
|
178
|
+
|
179
|
+
# Execute 'list' command
|
180
|
+
reduce.list(cmdr[:list][:list0]) if cmdr[:list]
|
181
|
+
|
182
|
+
# Execute 'clean' command
|
183
|
+
reduce.clean(cmdr[:clean][:clean0], deployments: cmdr[:clean][:deployments],
|
184
|
+
pacman: cmdr[:clean][:pacman], cache: cmdr[:clean][:cache], vms: cmdr[:vms]) if cmdr[:clean]
|
185
|
+
|
186
|
+
# Execute 'build' command
|
187
|
+
reduce.build(cmdr[:clean][:clean0], deployments: cmdr[:clean][:deployments]) if cmdr[:build]
|
188
|
+
|
189
|
+
# Execute 'pack' command
|
190
|
+
reduce.pack(cmdr[:pack][:pack0], disksize: cmdr[:pack][:disksize], force: cmdr[:pack][:force]) if cmdr[:pack]
|
191
|
+
|
192
|
+
# Execute 'deploy' command
|
193
|
+
reduce.deploy(cmdr[:deploy][:pack0], nodes: cmdr[:deploy][:deploy1], name: cmdr[:deploy][:name],
|
194
|
+
run: cmdr[:deploy][:run], exec: cmdr[:deploy][:exec], ipv6: cmdr[:ipv6],
|
195
|
+
vagrantfile: cmdr[:vagrantfile], force: cmdr[:force]) if cmdr[:deploy]
|
130
196
|
end
|
131
197
|
```
|
132
198
|
|
133
|
-
|
199
|
+
#### Named Option Examples <a name="named-option-examples"></a>
|
134
200
|
```bash
|
135
|
-
#
|
136
|
-
#
|
137
|
-
./app clean
|
138
|
-
# Chained commands 'clean' and 'build' with their own specific 'all' positional option which is
|
139
|
-
# exactly equivalent to the previous usage
|
140
|
-
./app clean all build all
|
141
|
-
```
|
201
|
+
# The parameter '-d' coming after the command 'clean' is a named option using short hand form with
|
202
|
+
# an input type of Array without any checking as configured above
|
203
|
+
./app clean -d base
|
142
204
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
Option.new('-s|--skip=COMPONENT', 'Skip the given components', allowed:['iso', 'image'], type:String)
|
152
|
-
])
|
153
|
-
# Create command with a single positional option with an allowed check for value
|
154
|
-
cmdr.add('build', 'Build components', options:[
|
155
|
-
Option.new(nil, 'Build given components', allowed:['all', 'iso', 'image'])
|
156
|
-
])
|
157
|
-
cmdr.parse!
|
205
|
+
# This is a variation using long form which is exactly equivalent
|
206
|
+
./app clean --deployments base
|
207
|
+
|
208
|
+
# This is a variation using long form with assignement syntax which is also exactly equivalent
|
209
|
+
./app clean --deployments=base
|
210
|
+
|
211
|
+
# This is a variation providing multiple values for the array named parameter 'deployments'
|
212
|
+
./app clean -d base,lite,heavy
|
158
213
|
```
|
159
214
|
|
160
|
-
|
215
|
+
#### Positional Option Examples <a name="positional-option-examples"></a>
|
161
216
|
```bash
|
162
|
-
# The parameter 'all' coming after the command 'clean' is a positional option with a default
|
163
|
-
# String that is checked against the
|
217
|
+
# The parameter 'all' coming after the command 'clean' is a positional array option with a default
|
218
|
+
# type of String that is checked against the configured allowed values ['all', 'initramfs',
|
219
|
+
# 'multiboot', 'ios'].
|
164
220
|
./app clean all
|
165
|
-
|
166
|
-
#
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
#
|
175
|
-
#
|
176
|
-
|
221
|
+
|
222
|
+
# This is a variation of the previous command line expression with multiple values specified for the
|
223
|
+
# array.
|
224
|
+
./app clean initramfs,multiboot,iso
|
225
|
+
```
|
226
|
+
|
227
|
+
#### Chained Command Expression Examples <a name="chained-command-expression-examples"></a>
|
228
|
+
```bash
|
229
|
+
# Chained commands 'clean' and 'build' share the 'all' positional option. Additionally there is one
|
230
|
+
# global option '-p personal'. Thus 'clean' will be executed first with the 'all' option in the
|
231
|
+
# context of '-p personal' then the 'build' command will be executed second with the 'all' option
|
232
|
+
# in the context of '-p personal'.
|
233
|
+
./app clean build all -p standard
|
234
|
+
|
235
|
+
# The above chained command is exactly equivalent to its expanded counter part below
|
236
|
+
./app clean all build all -p personal
|
177
237
|
```
|
178
238
|
|
179
239
|
### Help <a name="help"></a>
|
data/lib/nub/commander.rb
CHANGED
@@ -538,7 +538,7 @@ class Commander
|
|
538
538
|
match.value = arg[@value_regex, 1]
|
539
539
|
|
540
540
|
# Set symbol converting dashes to underscores for named options
|
541
|
-
if (match.opt = options.find{|x|
|
541
|
+
if (match.opt = options.find{|x| (short && short == x.short) || (long && long == x.long)})
|
542
542
|
match.sym = match.opt.to_sym
|
543
543
|
end
|
544
544
|
end
|
@@ -551,7 +551,7 @@ class Commander
|
|
551
551
|
short = x[@short_regex, 1]
|
552
552
|
long = x[@long_regex, 1]
|
553
553
|
value = x[@value_regex, 1]
|
554
|
-
if short == arg.short || long == arg.long
|
554
|
+
if (short && short == arg.short) || (long && long == arg.long)
|
555
555
|
match.opt = arg
|
556
556
|
match.value = value
|
557
557
|
match.sym = match.opt.to_sym
|