lino 2.2.0.pre.1 → 2.2.0.pre.6

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: f6b8d73d1b83fd4e09bd2cd61bcc0b0717bb14c43976688dbe26d7b84037f725
4
- data.tar.gz: b1e0ee813a1300a0935a78eaa224677ec60d36181a573e3f4a0ec0e9fb426c61
3
+ metadata.gz: 9aefe3408616b3ae2841a469c5968f05a3d93f069b481ab3a311b37c5c99a19c
4
+ data.tar.gz: 36aab763064d14623437733908fdf4dcc1662431aa74ef5b296d8966799e6713
5
5
  SHA512:
6
- metadata.gz: e78ac95f2c066ca9de9b1b04aceae7ab7ed0a744df486558414abfb8bf725f3bb3a9f37c53220fbd68b7a5bbb2ec9d0eb98da2e40d08943692ac07a7799f0a07
7
- data.tar.gz: 1697191e1593ceef0785913ec5091ba06cad2af156ba97db7196157da9bf017cfc710d434b71012a44b3470c059669a86f06b1e5fdb865593437ab89a4d662dc
6
+ metadata.gz: a0492eb20f00aa07778fe583f1ae8e59516b2ca15a745a5c1f754401db2c6ab5c16f6ee5804b0517c434180a4dab376d499c6d881e7dac1db9a6433e2e6b8aa1
7
+ data.tar.gz: d3d77fe67e292480d87d323887ecd93292149ab7cb3a510b32c9accd721d3c465b8a12cfa7b84c8ad9b2a8310c5702efc39fb91c19a787a1a1d2e299061701be
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lino (2.2.0.pre.1)
4
+ lino (2.2.0.pre.6)
5
5
  hamster (~> 3.0)
6
6
  open4 (~> 1.3)
7
7
 
data/README.md CHANGED
@@ -41,10 +41,13 @@ command_line.execute
41
41
  ### `Lino::CommandLineBuilder`
42
42
 
43
43
  The `CommandLineBuilder` allows a number of different styles of commands to be
44
- built:
44
+ built.
45
+
46
+ #### Flags
47
+
48
+ Flags can be added with `#with_flag`:
45
49
 
46
50
  ```ruby
47
- # commands with flags
48
51
  Lino::CommandLineBuilder.for_command('ls')
49
52
  .with_flag('-l')
50
53
  .with_flag('-a')
@@ -52,8 +55,24 @@ Lino::CommandLineBuilder.for_command('ls')
52
55
  .to_s
53
56
 
54
57
  # => ls -l -a
58
+ ```
59
+
60
+ or `#with_flags`:
61
+
62
+ ```ruby
63
+ Lino::CommandLineBuilder.for_command('ls')
64
+ .with_flags(%w[-l -a])
65
+ .build
66
+ .to_s
67
+
68
+ # => ls -l -a
69
+ ```
70
+
71
+ #### Options
72
+
73
+ Options with values can be added with `#with_option`:
55
74
 
56
- # commands with options
75
+ ```ruby
57
76
  Lino::CommandLineBuilder.for_command('gpg')
58
77
  .with_option('--recipient', 'tobyclemson@gmail.com')
59
78
  .with_option('--sign', './doc.txt')
@@ -61,16 +80,57 @@ Lino::CommandLineBuilder.for_command('gpg')
61
80
  .to_s
62
81
 
63
82
  # => gpg --recipient tobyclemson@gmail.com --sign ./doc.txt
83
+ ```
84
+
85
+ or `#with_options`, either as a hash:
86
+
87
+ ```ruby
88
+ Lino::CommandLineBuilder.for_command('gpg')
89
+ .with_options({
90
+ '--recipient' => 'tobyclemson@gmail.com',
91
+ '--sign' => './doc.txt'
92
+ })
93
+ .build
94
+ .to_s
64
95
 
65
- # commands with an option repeated multiple times
96
+ # => gpg --recipient tobyclemson@gmail.com --sign ./doc.txt
97
+ ```
98
+
99
+ or as an array:
100
+
101
+ ```ruby
102
+ Lino::CommandLineBuilder.for_command('gpg')
103
+ .with_options(
104
+ [
105
+ { option: '--recipient', value: 'tobyclemson@gmail.com' },
106
+ { option: '--sign', value: './doc.txt' }
107
+ ]
108
+ )
109
+ .build
110
+ .to_s
111
+
112
+ # => gpg --recipient tobyclemson@gmail.com --sign ./doc.txt
113
+ ```
114
+
115
+ Some commands allow options to be repeated:
116
+
117
+ ```ruby
66
118
  Lino::CommandLineBuilder.for_command('example.sh')
67
119
  .with_repeated_option('--opt', ['file1.txt', nil, '', 'file2.txt'])
68
120
  .build
69
121
  .to_s
70
122
 
71
123
  # => example.sh --opt file1.txt --opt file2.txt
124
+ ```
125
+
126
+ > Note: `lino` ignores `nil` or empty option values in the resulting command
127
+ > line.
72
128
 
73
- # commands with arguments
129
+ #### Arguments
130
+
131
+ Arguments can be added using `#with_argument`:
132
+
133
+ ```ruby
74
134
  Lino::CommandLineBuilder.for_command('diff')
75
135
  .with_argument('./file1.txt')
76
136
  .with_argument('./file2.txt')
@@ -78,16 +138,28 @@ Lino::CommandLineBuilder.for_command('diff')
78
138
  .to_s
79
139
 
80
140
  # => diff ./file1.txt ./file2.txt
141
+ ```
81
142
 
82
- # ... or alternatively
143
+ or `#with_arguments`, as an array:
144
+
145
+ ```ruby
83
146
  Lino::CommandLineBuilder.for_command('diff')
84
147
  .with_arguments(['./file1.txt', nil, '', './file2.txt'])
85
148
  .build
86
149
  .to_s
87
150
 
88
151
  # => diff ./file1.txt ./file2.txt
152
+ ```
153
+
154
+ > Note: `lino` ignores `nil` or empty argument values in the resulting command
155
+ > line.
156
+
157
+ #### Option Separators
89
158
 
90
- # commands with custom option separator
159
+ By default, `lino` separates option values from the option by a space. This
160
+ can be overridden globally using `#with_option_separator`:
161
+
162
+ ```ruby
91
163
  Lino::CommandLineBuilder.for_command('java')
92
164
  .with_option_separator(':')
93
165
  .with_option('-splash', './images/splash.jpg')
@@ -96,44 +168,132 @@ Lino::CommandLineBuilder.for_command('java')
96
168
  .to_s
97
169
 
98
170
  # => java -splash:./images/splash.jpg ./application.jar
171
+ ```
172
+
173
+ The option separator can be overridden on an option by option basis:
174
+
175
+ ```ruby
176
+ Lino::CommandLineBuilder.for_command('java')
177
+ .with_option('-splash', './images/splash.jpg', separator: ':')
178
+ .with_argument('./application.jar')
179
+ .build
180
+ .to_s
181
+
182
+ # => java -splash:./images/splash.jpg ./application.jar
183
+ ```
184
+
185
+ > Note: `#with_options` supports separator overriding when the options are
186
+ > passed as an array of hashes and a `separator` key is included in the
187
+ > hash.
188
+
189
+ > Note: `#with_repeated_option` also supports the `separator` named parameter.
190
+
191
+ > Note: option specific separators take precedence over the global option
192
+ > separator
193
+
194
+ #### Option Quoting
195
+
196
+ By default, `lino` does not quote option values. This can be overridden
197
+ globally using `#with_option_quoting`:
198
+
199
+ ```ruby
200
+ Lino::CommandLineBuilder.for_command('gpg')
201
+ .with_option_quoting('"')
202
+ .with_option('--sign', 'some file.txt')
203
+ .build
204
+ .to_s
205
+
206
+ # => gpg --sign "some file.txt"
207
+ ```
208
+
209
+ The option quoting can be overridden on an option by option basis:
210
+
211
+ ```ruby
212
+ Lino::CommandLineBuilder.for_command('java')
213
+ .with_option('-splash', './images/splash.jpg', quoting: '"')
214
+ .with_argument('./application.jar')
215
+ .build
216
+ .to_s
217
+
218
+ # => java -splash "./images/splash.jpg" ./application.jar
219
+ ```
220
+
221
+ > Note: `#with_options` supports quoting overriding when the options are
222
+ > passed as an array of hashes and a `quoting` key is included in the
223
+ > hash.
224
+
225
+ > Note: `#with_repeated_option` also supports the `quoting` named parameter.
226
+
227
+ > Note: option specific quoting take precedence over the global option
228
+ > quoting
229
+
230
+ #### Subcommands
231
+
232
+ Subcommands can be added using `#with_subcommand`:
99
233
 
100
- # commands using a subcommand style
234
+ ```ruby
101
235
  Lino::CommandLineBuilder.for_command('git')
102
236
  .with_flag('--no-pager')
103
- .with_subcommand('log') do |sub|
104
- sub.with_option('--since', '2016-01-01')
105
- end
237
+ .with_subcommand('log')
106
238
  .build
107
239
  .to_s
108
240
 
109
- # => git --no-pager log --since 2016-01-01
241
+ # => git --no-pager log
242
+ ```
110
243
 
111
- # commands with multiple levels of subcommand
244
+ Multi-level subcommands can be added using multiple `#with_subcommand`
245
+ invocations:
246
+
247
+ ```ruby
112
248
  Lino::CommandLineBuilder.for_command('gcloud')
113
249
  .with_subcommand('sql')
114
250
  .with_subcommand('instances')
115
251
  .with_subcommand('set-root-password')
116
- .with_subcommand('some-database') do |sub|
117
- sub.with_option('--password', 'super-secure')
118
- end
252
+ .with_subcommand('some-database')
119
253
  .build
120
254
  .to_s
121
-
122
- # => gcloud sql instances set-root-password some-database --password super-secure
123
255
 
124
- # ... or alternatively
256
+ # => gcloud sql instances set-root-password some-database
257
+ ```
258
+
259
+ or using `#with_subcommands`:
260
+
261
+ ```ruby
125
262
  Lino::CommandLineBuilder.for_command('gcloud')
126
263
  .with_subcommands(
127
264
  %w[sql instances set-root-password some-database]
128
- ) do |sub|
129
- sub.with_option('--password', 'super-secure')
130
- end
265
+ )
131
266
  .build
132
267
  .to_s
133
268
 
134
- # => gcloud sql instances set-root-password some-database --password super-secure
269
+ # => gcloud sql instances set-root-password some-database
270
+ ```
271
+
272
+ Subcommands also support options via `#with_flag`, `#with_flags`,
273
+ `#with_option`, `#with_options` and `#with_repeated_option` just like commands,
274
+ via a block, for example:
275
+
276
+ ```ruby
277
+ Lino::CommandLineBuilder.for_command('git')
278
+ .with_flag('--no-pager')
279
+ .with_subcommand('log') do |sub|
280
+ sub.with_option('--since', '2016-01-01')
281
+ end
282
+ .build
283
+ .to_s
284
+
285
+ # => git --no-pager log --since 2016-01-01
286
+ ```
287
+
288
+ > Note: `#with_subcommands` also supports a block, which applies in the context
289
+ > of the last subcommand in the passed array.
290
+
291
+ #### Environment Variables
292
+
293
+ Command lines can be prefixed with environment variables using
294
+ `#with_environment_variable`:
135
295
 
136
- # commands controlled by environment variables
296
+ ```ruby
137
297
  Lino::CommandLineBuilder.for_command('node')
138
298
  .with_environment_variable('PORT', '3030')
139
299
  .with_environment_variable('LOG_LEVEL', 'debug')
@@ -142,31 +302,73 @@ Lino::CommandLineBuilder.for_command('node')
142
302
  .to_s
143
303
 
144
304
  # => PORT=3030 LOG_LEVEL=debug node ./server.js
305
+ ```
306
+
307
+ or `#with_environment_variables`, either as a hash:
308
+
309
+ ```ruby
310
+ Lino::CommandLineBuilder.for_command('node')
311
+ .with_environment_variables({
312
+ 'PORT' => '3030',
313
+ 'LOG_LEVEL' => 'debug'
314
+ })
315
+ .build
316
+ .to_s
317
+
318
+ # => PORT=3030 LOG_LEVEL=debug node ./server.js
319
+ ```
320
+
321
+ or as an array:
322
+
323
+ ```ruby
324
+ Lino::CommandLineBuilder.for_command('node')
325
+ .with_environment_variables(
326
+ [
327
+ { name: 'PORT', value: '3030' },
328
+ { name: 'LOG_LEVEL', value: 'debug' }
329
+ ]
330
+ )
331
+ .build
332
+ .to_s
333
+
334
+ # => PORT=3030 LOG_LEVEL=debug node ./server.js
335
+ ```
336
+
337
+ #### Option Placement
338
+
339
+ By default, `lino` places top-level options after the command, before all
340
+ subcommands and arguments.
145
341
 
146
- # note: by default, options are placed after the command, before all subcommands
147
- # and arguments
342
+ This is equivalent to calling `#with_options_after_command`:
148
343
 
149
- # this can be expressed explicitly
344
+ ```ruby
150
345
  Lino::CommandLineBuilder.for_command('gcloud')
151
346
  .with_options_after_command
152
347
  .with_option('--password', 'super-secure')
153
- .with_subcommands(%w[sql instances set-root-password some-database])
348
+ .with_subcommands(%w[sql instances set-root-password])
154
349
  .build
155
350
  .to_s
156
351
 
157
- # => gcloud --password super-secure sql instances set-root-password some-database
352
+ # => gcloud --password super-secure sql instances set-root-password
353
+ ```
354
+
355
+ Alternatively, top-level options can be placed after all subcommands using
356
+ `#with_options_after_subcommands`:
158
357
 
159
- # options can also come after subcommands
358
+ ```ruby
160
359
  Lino::CommandLineBuilder.for_command('gcloud')
161
360
  .with_options_after_subcommands
162
361
  .with_option('--password', 'super-secure')
163
- .with_subcommands(%w[sql instances set-root-password some-database])
362
+ .with_subcommands(%w[sql instances set-root-password])
164
363
  .build
165
364
  .to_s
166
365
 
167
- # => gcloud sql instances set-root-password some-database --password super-secure
366
+ # => gcloud sql instances set-root-password --password super-secure
367
+ ```
368
+
369
+ or, after all arguments, using `#with_options_after_arguments`:
168
370
 
169
- # options can also come after arguments, although usages of this are rare
371
+ ```ruby
170
372
  Lino::CommandLineBuilder.for_command('ls')
171
373
  .with_options_after_arguments
172
374
  .with_flag('-l')
@@ -177,6 +379,59 @@ Lino::CommandLineBuilder.for_command('ls')
177
379
  # => ls /some/directory -l
178
380
  ```
179
381
 
382
+ #### Appliables
383
+
384
+ Command and subcommand builders both support passing 'appliables' that are
385
+ applied to the builder allowing an operation to be encapsulated in an object.
386
+
387
+ Given an appliable type:
388
+
389
+ ```ruby
390
+ class AppliableOption
391
+ def initialize(option, value)
392
+ @option = option
393
+ @value = value
394
+ end
395
+
396
+ def apply(builder)
397
+ builder.with_option(@option, @value)
398
+ end
399
+ end
400
+ ```
401
+
402
+ an instance of the appliable can be applied using `#with_appliable`:
403
+
404
+ ```ruby
405
+ Lino::CommandLineBuilder.for_command('gpg')
406
+ .with_appliable(AppliableOption.new('--recipient', 'tobyclemson@gmail.com'))
407
+ .with_flag('--sign')
408
+ .with_argument('/some/file.txt')
409
+ .build
410
+ .to_s
411
+
412
+ # => gpg --recipient tobyclemson@gmail.com --sign /some/file.txt
413
+ ```
414
+
415
+ or multiple with `#with_appliables`:
416
+
417
+ ```ruby
418
+ Lino::CommandLineBuilder.for_command('gpg')
419
+ .with_appliables([
420
+ AppliableOption.new('--recipient', 'user@example.com'),
421
+ AppliableOption.new('--output', '/signed.txt')
422
+ ])
423
+ .with_flag('--sign')
424
+ .with_argument('/file.txt')
425
+ .build
426
+ .to_s
427
+
428
+ # => gpg --recipient user@example.com --output /signed.txt --sign /file.txt
429
+ ```
430
+
431
+ > Note: an 'appliable' is any object that has an `#apply` method.
432
+
433
+ > Note: `lino` ignores `nil` or empty appliables in the resulting command line.
434
+
180
435
  ### `Lino::CommandLine`
181
436
 
182
437
  A `CommandLine` can be executed using the `#execute` method:
data/Rakefile CHANGED
@@ -16,6 +16,7 @@ task default: %i[
16
16
 
17
17
  namespace :encryption do
18
18
  namespace :passphrase do
19
+ desc 'Generate encryption passphrase for CI GPG key'
19
20
  task :generate do
20
21
  File.open('config/secrets/ci/encryption.passphrase', 'w') do |f|
21
22
  f.write(SecureRandom.base64(36))
@@ -96,6 +97,7 @@ RakeGithub.define_repository_tasks(
96
97
  end
97
98
 
98
99
  namespace :pipeline do
100
+ desc 'Prepare CircleCI Pipeline'
99
101
  task prepare: %i[
100
102
  circle_ci:project:follow
101
103
  circle_ci:env_vars:ensure
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'utilities'
4
+
5
+ module Lino
6
+ module Appliables
7
+ include Lino::Utilities
8
+
9
+ def with_appliable(appliable)
10
+ return self if missing?(appliable)
11
+
12
+ appliable.apply(self)
13
+ end
14
+
15
+ def with_appliables(appliables)
16
+ return self if missing?(appliables)
17
+
18
+ appliables.inject(self) do |s, appliable|
19
+ s.with_appliable(appliable)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -5,6 +5,7 @@ require_relative 'utilities'
5
5
  require_relative 'command_line'
6
6
  require_relative 'subcommand_builder'
7
7
  require_relative 'options'
8
+ require_relative 'appliables'
8
9
 
9
10
  module Lino
10
11
  # rubocop:disable Metrics/ClassLength
@@ -20,6 +21,7 @@ module Lino
20
21
 
21
22
  include Lino::Utilities
22
23
  include Lino::Options
24
+ include Lino::Appliables
23
25
 
24
26
  class << self
25
27
  def for_command(command)
@@ -95,6 +97,8 @@ module Lino
95
97
  end
96
98
 
97
99
  def with_arguments(arguments)
100
+ return self if missing?(arguments)
101
+
98
102
  arguments.inject(self) { |s, argument| s.with_argument(argument) }
99
103
  end
100
104
 
@@ -109,6 +113,17 @@ module Lino
109
113
  )
110
114
  end
111
115
 
116
+ def with_environment_variables(environment_variables)
117
+ return self if missing?(environment_variables)
118
+
119
+ environment_variables.entries.inject(self) do |s, var|
120
+ s.with_environment_variable(
121
+ var.include?(:name) ? var[:name] : var[0],
122
+ var.include?(:value) ? var[:value] : var[1]
123
+ )
124
+ end
125
+ end
126
+
112
127
  def build
113
128
  components = formatted_components
114
129
  command_line = SELECTORS[@option_placement]
data/lib/lino/options.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'utilities'
4
+
3
5
  module Lino
4
6
  module Options
7
+ include Lino::Utilities
8
+
5
9
  def with_option(option, value, separator: nil, quoting: nil)
6
10
  return self if missing?(value)
7
11
 
@@ -14,6 +18,19 @@ module Lino
14
18
  ))
15
19
  end
16
20
 
21
+ def with_options(options)
22
+ return self if missing?(options)
23
+
24
+ options.entries.inject(self) do |s, entry|
25
+ s.with_option(
26
+ entry.include?(:option) ? entry[:option] : entry[0],
27
+ entry.include?(:value) ? entry[:value] : entry[1],
28
+ separator: (entry.include?(:separator) ? entry[:separator] : nil),
29
+ quoting: (entry.include?(:quoting) ? entry[:quoting] : nil)
30
+ )
31
+ end
32
+ end
33
+
17
34
  def with_repeated_option(option, values, separator: nil, quoting: nil)
18
35
  values.inject(self) do |s, value|
19
36
  s.with_option(option, value, separator: separator, quoting: quoting)
@@ -21,7 +38,15 @@ module Lino
21
38
  end
22
39
 
23
40
  def with_flag(flag)
41
+ return self if missing?(flag)
42
+
24
43
  with(options: @options.add({ components: [flag] }))
25
44
  end
45
+
46
+ def with_flags(flags)
47
+ return self if missing?(flags)
48
+
49
+ flags.inject(self) { |s, flag| s.with_flag(flag) }
50
+ end
26
51
  end
27
52
  end
@@ -3,11 +3,13 @@
3
3
  require 'hamster'
4
4
  require_relative 'utilities'
5
5
  require_relative 'options'
6
+ require_relative 'appliables'
6
7
 
7
8
  module Lino
8
9
  class SubcommandBuilder
9
10
  include Lino::Utilities
10
11
  include Lino::Options
12
+ include Lino::Appliables
11
13
 
12
14
  class <<self
13
15
  def for_subcommand(subcommand)
data/lib/lino/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lino
4
- VERSION = '2.2.0.pre.1'
4
+ VERSION = '2.2.0.pre.6'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lino
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0.pre.1
4
+ version: 2.2.0.pre.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby Clemson
@@ -222,6 +222,7 @@ files:
222
222
  - bin/console
223
223
  - bin/setup
224
224
  - lib/lino.rb
225
+ - lib/lino/appliables.rb
225
226
  - lib/lino/command_line.rb
226
227
  - lib/lino/command_line_builder.rb
227
228
  - lib/lino/options.rb