command_kit 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +15 -0
  3. data/.rubocop.yml +138 -0
  4. data/ChangeLog.md +29 -0
  5. data/Gemfile +3 -0
  6. data/README.md +141 -121
  7. data/Rakefile +3 -2
  8. data/command_kit.gemspec +4 -4
  9. data/examples/command.rb +1 -1
  10. data/gemspec.yml +7 -0
  11. data/lib/command_kit/arguments.rb +1 -1
  12. data/lib/command_kit/colors.rb +221 -45
  13. data/lib/command_kit/command.rb +1 -1
  14. data/lib/command_kit/commands.rb +4 -4
  15. data/lib/command_kit/help/man.rb +4 -25
  16. data/lib/command_kit/inflector.rb +47 -17
  17. data/lib/command_kit/main.rb +7 -9
  18. data/lib/command_kit/man.rb +44 -0
  19. data/lib/command_kit/open_app.rb +69 -0
  20. data/lib/command_kit/options/option.rb +1 -6
  21. data/lib/command_kit/options/parser.rb +15 -17
  22. data/lib/command_kit/options.rb +2 -2
  23. data/lib/command_kit/os/linux.rb +157 -0
  24. data/lib/command_kit/os.rb +159 -11
  25. data/lib/command_kit/package_manager.rb +200 -0
  26. data/lib/command_kit/pager.rb +46 -4
  27. data/lib/command_kit/printing/indent.rb +2 -2
  28. data/lib/command_kit/printing.rb +1 -1
  29. data/lib/command_kit/sudo.rb +40 -0
  30. data/lib/command_kit/terminal.rb +5 -0
  31. data/lib/command_kit/version.rb +1 -1
  32. data/spec/arguments/argument_spec.rb +1 -1
  33. data/spec/colors_spec.rb +256 -0
  34. data/spec/commands_spec.rb +1 -1
  35. data/spec/exception_handler_spec.rb +1 -1
  36. data/spec/help/man_spec.rb +0 -32
  37. data/spec/inflector_spec.rb +70 -8
  38. data/spec/man_spec.rb +46 -0
  39. data/spec/open_app_spec.rb +85 -0
  40. data/spec/options/option_spec.rb +2 -2
  41. data/spec/os/linux_spec.rb +154 -0
  42. data/spec/os_spec.rb +200 -13
  43. data/spec/package_manager_spec.rb +806 -0
  44. data/spec/pager_spec.rb +71 -6
  45. data/spec/sudo_spec.rb +51 -0
  46. data/spec/terminal_spec.rb +30 -0
  47. data/spec/usage_spec.rb +1 -1
  48. metadata +19 -4
@@ -20,7 +20,7 @@ module CommandKit
20
20
  #
21
21
  module Indent
22
22
  #
23
- # Initializes the indententation level to zero.
23
+ # Initializes the indentation level to zero.
24
24
  #
25
25
  def initialize(**kwargs)
26
26
  @indent = 0
@@ -40,7 +40,7 @@ module CommandKit
40
40
  # increased.
41
41
  #
42
42
  # @return [Integer]
43
- # If no block is given, the indententation level will be returned.
43
+ # If no block is given, the indentation level will be returned.
44
44
  #
45
45
  # @example
46
46
  # puts "values:"
@@ -9,7 +9,7 @@ module CommandKit
9
9
  module Printing
10
10
  include Stdio
11
11
 
12
- # Platform independency new-line constant
12
+ # Platform independent new-line constant
13
13
  #
14
14
  # @return [String]
15
15
  #
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'command_kit/os'
4
+
5
+ module CommandKit
6
+ #
7
+ # Allows running commands with `sudo`.
8
+ #
9
+ # @since 0.2.0
10
+ #
11
+ module Sudo
12
+ include OS
13
+
14
+ #
15
+ # Runs the command under sudo, if the user isn't already root.
16
+ #
17
+ # @param [String] command
18
+ # The command to execute.
19
+ #
20
+ # @param [Array<String>] arguments
21
+ # Additional arguments for the command.
22
+ #
23
+ # @return [Boolean, nil]
24
+ # Specifies whether the command was successfully ran or not.
25
+ #
26
+ # @api public
27
+ #
28
+ def sudo(command,*arguments)
29
+ if windows?
30
+ system('runas','/user:administrator',command,*arguments)
31
+ else
32
+ if Process.uid == 0
33
+ system(command,*arguments)
34
+ else
35
+ system('sudo',command,*arguments)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -71,6 +71,11 @@ module CommandKit
71
71
  IO.respond_to?(:console) && stdout.tty?
72
72
  end
73
73
 
74
+ #
75
+ # @since 0.2.0
76
+ #
77
+ alias tty? terminal?
78
+
74
79
  #
75
80
  # Returns the terminal object, if {Stdio#stdout stdout} is connected to a
76
81
  # terminal.
@@ -1,4 +1,4 @@
1
1
  module CommandKit
2
2
  # command_kit version
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
@@ -126,7 +126,7 @@ describe CommandKit::Arguments::Argument do
126
126
  let(:repeats) { false }
127
127
 
128
128
  it "must return the usage name unchanged" do
129
- expect(subject.usage).to eq("#{usage}")
129
+ expect(subject.usage).to eq(usage)
130
130
  end
131
131
  end
132
132
  end
data/spec/colors_spec.rb CHANGED
@@ -65,6 +65,38 @@ describe CommandKit::Colors do
65
65
  it { expect(subject::WHITE).to eq("\e[37m") }
66
66
  end
67
67
 
68
+ describe "ON_BLACK" do
69
+ it { expect(subject::ON_BLACK).to eq("\e[40m") }
70
+ end
71
+
72
+ describe "ON_RED" do
73
+ it { expect(subject::ON_RED).to eq("\e[41m") }
74
+ end
75
+
76
+ describe "ON_GREEN" do
77
+ it { expect(subject::ON_GREEN).to eq("\e[42m") }
78
+ end
79
+
80
+ describe "ON_YELLOW" do
81
+ it { expect(subject::ON_YELLOW).to eq("\e[43m") }
82
+ end
83
+
84
+ describe "ON_BLUE" do
85
+ it { expect(subject::ON_BLUE).to eq("\e[44m") }
86
+ end
87
+
88
+ describe "ON_MAGENTA" do
89
+ it { expect(subject::ON_MAGENTA).to eq("\e[45m") }
90
+ end
91
+
92
+ describe "ON_CYAN" do
93
+ it { expect(subject::ON_CYAN).to eq("\e[46m") }
94
+ end
95
+
96
+ describe "ON_WHITE" do
97
+ it { expect(subject::ON_WHITE).to eq("\e[47m") }
98
+ end
99
+
68
100
  describe "RESET_COLOR" do
69
101
  it { expect(subject::RESET_COLOR).to eq("\e[39m") }
70
102
  end
@@ -186,6 +218,102 @@ describe CommandKit::Colors do
186
218
  it { expect(subject.white).to eq("\e[37m") }
187
219
  end
188
220
  end
221
+
222
+ describe ".on_black" do
223
+ context "when given a string" do
224
+ it "must wrap the string with \\e[40m and \\e[39m" do
225
+ expect(subject.on_black(str)).to eq("\e[40m#{str}\e[49m")
226
+ end
227
+ end
228
+
229
+ context "when given no arguments" do
230
+ it { expect(subject.on_black).to eq("\e[40m") }
231
+ end
232
+ end
233
+
234
+ describe ".on_red" do
235
+ context "when given a string" do
236
+ it "must wrap the string with \\e[41m and \\e[39m" do
237
+ expect(subject.on_red(str)).to eq("\e[41m#{str}\e[49m")
238
+ end
239
+ end
240
+
241
+ context "when given no arguments" do
242
+ it { expect(subject.on_red).to eq("\e[41m") }
243
+ end
244
+ end
245
+
246
+ describe ".on_green" do
247
+ context "when given a string" do
248
+ it "must wrap the string with \\e[42m and \\e[39m" do
249
+ expect(subject.on_green(str)).to eq("\e[42m#{str}\e[49m")
250
+ end
251
+ end
252
+
253
+ context "when given no arguments" do
254
+ it { expect(subject.on_green).to eq("\e[42m") }
255
+ end
256
+ end
257
+
258
+ describe ".on_yellow" do
259
+ context "when given a string" do
260
+ it "must wrap the string with \\e[43m and \\e[39m" do
261
+ expect(subject.on_yellow(str)).to eq("\e[43m#{str}\e[49m")
262
+ end
263
+ end
264
+
265
+ context "when given no arguments" do
266
+ it { expect(subject.on_yellow).to eq("\e[43m") }
267
+ end
268
+ end
269
+
270
+ describe ".on_blue" do
271
+ context "when given a string" do
272
+ it "must wrap the string with \\e[44m and \\e[39m" do
273
+ expect(subject.on_blue(str)).to eq("\e[44m#{str}\e[49m")
274
+ end
275
+ end
276
+
277
+ context "when given no arguments" do
278
+ it { expect(subject.on_blue).to eq("\e[44m") }
279
+ end
280
+ end
281
+
282
+ describe ".on_magenta" do
283
+ context "when given a string" do
284
+ it "must wrap the string with \\e[45m and \\e[39m" do
285
+ expect(subject.on_magenta(str)).to eq("\e[45m#{str}\e[49m")
286
+ end
287
+ end
288
+
289
+ context "when given no arguments" do
290
+ it { expect(subject.on_magenta).to eq("\e[45m") }
291
+ end
292
+ end
293
+
294
+ describe ".on_cyan" do
295
+ context "when given a string" do
296
+ it "must wrap the string with \\e[46m and \\e[39m" do
297
+ expect(subject.on_cyan(str)).to eq("\e[46m#{str}\e[49m")
298
+ end
299
+ end
300
+
301
+ context "when given no arguments" do
302
+ it { expect(subject.on_cyan).to eq("\e[46m") }
303
+ end
304
+ end
305
+
306
+ describe ".on_white" do
307
+ context "when given a string" do
308
+ it "must wrap the string with \\e[47m and \\e[39m" do
309
+ expect(subject.on_white(str)).to eq("\e[47m#{str}\e[49m")
310
+ end
311
+ end
312
+
313
+ context "when given no arguments" do
314
+ it { expect(subject.on_white).to eq("\e[47m") }
315
+ end
316
+ end
189
317
  end
190
318
 
191
319
  describe CommandKit::Colors::PlainText do
@@ -241,6 +369,38 @@ describe CommandKit::Colors do
241
369
  it { expect(subject::WHITE).to eq('') }
242
370
  end
243
371
 
372
+ describe "ON_BLACK" do
373
+ it { expect(subject::ON_BLACK).to eq('') }
374
+ end
375
+
376
+ describe "ON_RED" do
377
+ it { expect(subject::ON_RED).to eq('') }
378
+ end
379
+
380
+ describe "ON_GREEN" do
381
+ it { expect(subject::ON_GREEN).to eq('') }
382
+ end
383
+
384
+ describe "ON_YELLOW" do
385
+ it { expect(subject::ON_YELLOW).to eq('') }
386
+ end
387
+
388
+ describe "ON_BLUE" do
389
+ it { expect(subject::ON_BLUE).to eq('') }
390
+ end
391
+
392
+ describe "ON_MAGENTA" do
393
+ it { expect(subject::ON_MAGENTA).to eq('') }
394
+ end
395
+
396
+ describe "ON_CYAN" do
397
+ it { expect(subject::ON_CYAN).to eq('') }
398
+ end
399
+
400
+ describe "ON_WHITE" do
401
+ it { expect(subject::ON_WHITE).to eq('') }
402
+ end
403
+
244
404
  describe "RESET_COLOR" do
245
405
  it { expect(subject::RESET_COLOR).to eq('') }
246
406
  end
@@ -360,6 +520,102 @@ describe CommandKit::Colors do
360
520
  it { expect(subject.white).to eq('') }
361
521
  end
362
522
  end
523
+
524
+ describe ".on_black" do
525
+ context "when given a string" do
526
+ it "must return that string" do
527
+ expect(subject.on_black(str)).to eq(str)
528
+ end
529
+ end
530
+
531
+ context "when given no arguments" do
532
+ it { expect(subject.on_black).to eq('') }
533
+ end
534
+ end
535
+
536
+ describe ".on_red" do
537
+ context "when given a string" do
538
+ it "must return that string" do
539
+ expect(subject.on_red(str)).to eq(str)
540
+ end
541
+ end
542
+
543
+ context "when given no arguments" do
544
+ it { expect(subject.on_red).to eq('') }
545
+ end
546
+ end
547
+
548
+ describe ".on_green" do
549
+ context "when given a string" do
550
+ it "must return that string" do
551
+ expect(subject.on_green(str)).to eq(str)
552
+ end
553
+ end
554
+
555
+ context "when given no arguments" do
556
+ it { expect(subject.on_green).to eq('') }
557
+ end
558
+ end
559
+
560
+ describe ".on_yellow" do
561
+ context "when given a string" do
562
+ it "must return that string" do
563
+ expect(subject.on_yellow(str)).to eq(str)
564
+ end
565
+ end
566
+
567
+ context "when given no arguments" do
568
+ it { expect(subject.on_yellow).to eq('') }
569
+ end
570
+ end
571
+
572
+ describe ".on_blue" do
573
+ context "when given a string" do
574
+ it "must return that string" do
575
+ expect(subject.on_blue(str)).to eq(str)
576
+ end
577
+ end
578
+
579
+ context "when given no arguments" do
580
+ it { expect(subject.on_blue).to eq('') }
581
+ end
582
+ end
583
+
584
+ describe ".on_magenta" do
585
+ context "when given a string" do
586
+ it "must return that string" do
587
+ expect(subject.on_magenta(str)).to eq(str)
588
+ end
589
+ end
590
+
591
+ context "when given no arguments" do
592
+ it { expect(subject.on_magenta).to eq('') }
593
+ end
594
+ end
595
+
596
+ describe ".on_cyan" do
597
+ context "when given a string" do
598
+ it "must return that string" do
599
+ expect(subject.on_cyan(str)).to eq(str)
600
+ end
601
+ end
602
+
603
+ context "when given no arguments" do
604
+ it { expect(subject.on_cyan).to eq('') }
605
+ end
606
+ end
607
+
608
+ describe ".on_white" do
609
+ context "when given a string" do
610
+ it "must return that string" do
611
+ expect(subject.on_white(str)).to eq(str)
612
+ end
613
+ end
614
+
615
+ context "when given no arguments" do
616
+ it { expect(subject.on_white).to eq('') }
617
+ end
618
+ end
363
619
  end
364
620
 
365
621
  describe "#ansi?" do
@@ -191,7 +191,7 @@ describe CommandKit::Commands do
191
191
  it "must contain the mapping of aliases to command names" do
192
192
  expect(subject.command_aliases).to eq({
193
193
  't1' => 'test1',
194
- 't2' => 'test2',
194
+ 't2' => 'test2'
195
195
  })
196
196
  end
197
197
  end
@@ -73,7 +73,7 @@ describe CommandKit::ExceptionHandler do
73
73
  include CommandKit::ExceptionHandler
74
74
 
75
75
  def run
76
- raise(RuntimeError.new("error"))
76
+ raise("error")
77
77
  end
78
78
 
79
79
  end
@@ -234,38 +234,6 @@ describe CommandKit::Help::Man do
234
234
 
235
235
  subject { command_class.new }
236
236
 
237
- describe "#man" do
238
- let(:man_page) { 'foo' }
239
-
240
- it "must call system() with the given man page" do
241
- expect(subject).to receive(:system).with('man',man_page)
242
-
243
- subject.man(man_page)
244
- end
245
-
246
- context "when given a non-String man-page argument" do
247
- let(:man_page_arg) { double(:non_string_arg) }
248
-
249
- it "must call #to_s on the man-page argument" do
250
- expect(man_page_arg).to receive(:to_s).and_return(man_page)
251
-
252
- expect(subject).to receive(:system).with('man',man_page)
253
-
254
- subject.man(man_page_arg)
255
- end
256
- end
257
-
258
- context "when given the section: keyword argument" do
259
- let(:section) { 7 }
260
-
261
- it "must call system() with the given section number and man page" do
262
- expect(subject).to receive(:system).with('man',section.to_s,man_page)
263
-
264
- subject.man(man_page, section: section)
265
- end
266
- end
267
- end
268
-
269
237
  describe "#help_man" do
270
238
  context "when .man_dir is not set" do
271
239
  let(:command_class) { TestHelpMan::EmptyCommand }
@@ -28,6 +28,10 @@ describe CommandKit::Inflector do
28
28
  end
29
29
  end
30
30
 
31
+ ascii = (0..255).map(&:chr)
32
+ alpha = ('a'..'z').to_a + ('A'..'Z').to_a
33
+ numeric = ('0'..'9').to_a
34
+
31
35
  describe ".underscore" do
32
36
  it "must convert CamelCase to camel_case" do
33
37
  expect(subject.underscore('CamelCase')).to eq('camel_case')
@@ -37,18 +41,48 @@ describe CommandKit::Inflector do
37
41
  expect(subject.underscore('camelCase')).to eq('camel_case')
38
42
  end
39
43
 
40
- it "must convert Camel to camel" do
41
- expect(subject.underscore('Camel')).to eq('camel')
44
+ it "must convert Camelcase to camelcase" do
45
+ expect(subject.underscore('Camelcase')).to eq('camelcase')
42
46
  end
43
47
 
44
- it "must convert CAMEL to camel" do
45
- expect(subject.underscore('CAMEL')).to eq('camel')
48
+ it "must convert CAMELCASE to camelcase" do
49
+ expect(subject.underscore('CAMELCASE')).to eq('camelcase')
46
50
  end
47
51
 
48
- context "when given a String containing '/' characters" do
49
- it "must replace dashes with underscores" do
50
- expect(subject.underscore('foo-bar')).to eq('foo_bar')
51
- end
52
+ it "must convert CAMELCase to camel_case" do
53
+ expect(subject.underscore('CAMELCase')).to eq('camel_case')
54
+ end
55
+
56
+ it "must convert CamelCASE to camel_case" do
57
+ expect(subject.underscore('CAMELCase')).to eq('camel_case')
58
+ end
59
+
60
+ it "must convert FooBARBaz to foo_bar_baz" do
61
+ expect(subject.underscore('FooBARBaz')).to eq('foo_bar_baz')
62
+ end
63
+
64
+ it "must convert foo_bar_baz to foo_bar_baz" do
65
+ expect(subject.underscore('foo_bar_baz')).to eq('foo_bar_baz')
66
+ end
67
+
68
+ it "must convert foo___bar___baz to foo___bar___baz" do
69
+ expect(subject.underscore('foo___bar___baz')).to eq('foo___bar___baz')
70
+ end
71
+
72
+ it "must convert foo-bar-baz to foo_bar_baz" do
73
+ expect(subject.underscore('foo-bar-baz')).to eq('foo_bar_baz')
74
+ end
75
+
76
+ it "must convert foo---bar---baz to foo___bar___baz" do
77
+ expect(subject.underscore('foo---bar---baz')).to eq('foo___bar___baz')
78
+ end
79
+
80
+ it "must convert foo_BAR_baz to foo_bar_baz" do
81
+ expect(subject.underscore('foo_BAR_baz')).to eq('foo_bar_baz')
82
+ end
83
+
84
+ it "must convert foo-BAR-baz to foo_bar_baz" do
85
+ expect(subject.underscore('foo-BAR-baz')).to eq('foo_bar_baz')
52
86
  end
53
87
 
54
88
  context "when given a non-String" do
@@ -56,6 +90,20 @@ describe CommandKit::Inflector do
56
90
  expect(subject.underscore(:CamelCase)).to eq('camel_case')
57
91
  end
58
92
  end
93
+
94
+ separators = %w[_ -]
95
+
96
+ (ascii - alpha - numeric - separators).each do |char|
97
+ context "when the given String contains a #{char.inspect} character" do
98
+ let(:string) { "Foo#{char}Bar" }
99
+
100
+ it do
101
+ expect {
102
+ subject.underscore(string)
103
+ }.to raise_error(ArgumentError,"cannot convert string to underscored: #{string.inspect}")
104
+ end
105
+ end
106
+ end
59
107
  end
60
108
 
61
109
  describe ".dasherize" do
@@ -100,5 +148,19 @@ describe CommandKit::Inflector do
100
148
  expect(subject.camelize(:foo_bar)).to eq('FooBar')
101
149
  end
102
150
  end
151
+
152
+ separators = %w[_ - /]
153
+
154
+ (ascii - alpha - numeric - separators).each do |char|
155
+ context "when the given String contains a #{char.inspect} character" do
156
+ let(:string) { "foo#{char}bar" }
157
+
158
+ it do
159
+ expect {
160
+ subject.camelize(string)
161
+ }.to raise_error(ArgumentError,"cannot convert string to CamelCase: #{string.inspect}")
162
+ end
163
+ end
164
+ end
103
165
  end
104
166
  end