lino 2.2.0.pre.3 → 2.4.0.pre.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cee8c3683bbc6d1aa1088315b0271fe95850ab79a815ca239230dc354d2296b1
4
- data.tar.gz: fe01bf1be359a9d52be65af820228c6859592a60c107a533f330c5bbf06bd34f
3
+ metadata.gz: c8e3b24a8c6e75d46c7b35d031a103086e6581ddf565fcd6b29f5899a40b85e0
4
+ data.tar.gz: 58e3a47d18114356c513de7cfcdef552faad11ad738d88445297cfb4c48b8a12
5
5
  SHA512:
6
- metadata.gz: 002a1329d9ed0d050827c159e152ac6752e3e5d46ba225ffdac40accf9765189ce7c83fb9ef26c1ad876ecb018b3e24f50eeff60104b51cb8b342fb2b1d7dc3d
7
- data.tar.gz: 01d1c73ed195be4dcdc15e0365ca8865b6756133be2f95c4ef6a6f6824819c045720502c9a9e597ffe25244099a72435aae2f16e940a950bbae8d1f4f3c57094
6
+ metadata.gz: bf1f78f7538dc573f9ce6d2bfd77c98c8cec8fc2922a81249d89ff903b89d1ace6c999b28aa9d8c2ce259029053b849eee422b450a208cdd40a7ba0c18bade97
7
+ data.tar.gz: ff587023b46e059be16b08c6b113d396f618621edd9211aff9ca33d7413c5afa164b6e5f5206308a1b675b339cea3e4f34a6e540288000d7a161dc65e94f30ea
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lino (2.2.0.pre.3)
4
+ lino (2.4.0.pre.1)
5
5
  hamster (~> 3.0)
6
6
  open4 (~> 1.3)
7
7
 
@@ -133,4 +133,4 @@ DEPENDENCIES
133
133
  simplecov (~> 0.16)
134
134
 
135
135
  BUNDLED WITH
136
- 2.2.15
136
+ 2.2.16
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)
@@ -50,6 +52,8 @@ module Lino
50
52
  # rubocop:enable Metrics/ParameterLists
51
53
 
52
54
  def with_subcommand(subcommand, &block)
55
+ return self if missing?(subcommand)
56
+
53
57
  with(
54
58
  subcommands: @subcommands.add(
55
59
  (block || ->(sub) { sub }).call(
@@ -60,6 +64,8 @@ module Lino
60
64
  end
61
65
 
62
66
  def with_subcommands(subcommands, &block)
67
+ return self if missing?(subcommands)
68
+
63
69
  without_block = subcommands[0...-1]
64
70
  with_block = subcommands.last
65
71
 
@@ -111,6 +117,17 @@ module Lino
111
117
  )
112
118
  end
113
119
 
120
+ def with_environment_variables(environment_variables)
121
+ return self if missing?(environment_variables)
122
+
123
+ environment_variables.entries.inject(self) do |s, var|
124
+ s.with_environment_variable(
125
+ var.include?(:name) ? var[:name] : var[0],
126
+ var.include?(:value) ? var[:value] : var[1]
127
+ )
128
+ end
129
+ end
130
+
114
131
  def build
115
132
  components = formatted_components
116
133
  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)
@@ -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.3'
4
+ VERSION = '2.4.0.pre.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lino
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0.pre.3
4
+ version: 2.4.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby Clemson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-05 00:00:00.000000000 Z
11
+ date: 2021-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hamster
@@ -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