cmds 0.0.9 → 0.1.0
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/README.md +4 -4
- data/Rakefile +7 -2
- data/bin/rake +3 -0
- data/bin/rspec +3 -0
- data/cmds.gemspec +1 -1
- data/lib/cmds/cmd.rb +376 -0
- data/lib/cmds/debug.rb +2 -2
- data/lib/cmds/erb_context.rb +3 -3
- data/lib/cmds/io_handler.rb +2 -2
- data/lib/cmds/pipe.rb +1 -1
- data/lib/cmds/result.rb +10 -6
- data/lib/cmds/shell_eruby.rb +2 -2
- data/lib/cmds/spawn.rb +251 -0
- data/lib/cmds/sugar.rb +145 -222
- data/lib/cmds/util/defaults.rb +71 -0
- data/lib/cmds/util/params.rb +56 -0
- data/lib/cmds/util/tokenize_option.rb +118 -0
- data/lib/cmds/util/tokenize_options.rb +50 -0
- data/lib/cmds/util.rb +35 -195
- data/lib/cmds/version.rb +2 -2
- data/lib/cmds.rb +3 -36
- data/scratch/proxy.rb +1 -1
- data/spec/cmds/assert_spec.rb +1 -1
- data/spec/cmds/capture_spec.rb +9 -25
- data/spec/cmds/chomp_spec.rb +7 -7
- data/spec/cmds/curry_spec.rb +1 -1
- data/spec/cmds/err_spec.rb +9 -6
- data/spec/cmds/error_spec.rb +6 -6
- data/spec/cmds/ok_spec.rb +2 -2
- data/spec/cmds/out_spec.rb +7 -7
- data/spec/cmds/{sub_spec.rb → prepare_spec.rb} +20 -32
- data/spec/cmds/stream_spec.rb +4 -4
- data/spec/cmds/util/tokenize_option_spec.rb +119 -0
- data/spec/cmds_spec.rb +11 -0
- metadata +15 -12
- data/lib/cmds/capture.rb +0 -47
- data/lib/cmds/stream.rb +0 -239
- data/spec/cmds/expand_option_hash_spec.rb +0 -61
- data/test/bin/dspec +0 -1
data/spec/cmds/chomp_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Cmds.chomp" do
|
4
4
|
it "gets echo output" do
|
5
|
-
expect( Cmds.chomp "echo %s",
|
5
|
+
expect( Cmds.chomp "echo %s", "hey there!" ).to eq "hey there!"
|
6
6
|
end
|
7
7
|
|
8
8
|
it "reads input" do
|
@@ -16,7 +16,7 @@ end # Cmds.chomp
|
|
16
16
|
|
17
17
|
describe "Cmds.chomp!" do
|
18
18
|
it "gets echo output" do
|
19
|
-
expect( Cmds.chomp! "echo %s",
|
19
|
+
expect( Cmds.chomp! "echo %s", "hey there!" ).to eq "hey there!"
|
20
20
|
end
|
21
21
|
|
22
22
|
it "reads input" do
|
@@ -34,12 +34,12 @@ end # Cmds.chomp!
|
|
34
34
|
|
35
35
|
describe "Cmds#chomp" do
|
36
36
|
it "gets echo output" do
|
37
|
-
expect( Cmds.new("echo %s").chomp
|
37
|
+
expect( Cmds::Cmd.new("echo %s").chomp "hey there!" ).to eq "hey there!"
|
38
38
|
end
|
39
39
|
|
40
40
|
it "reads input" do
|
41
41
|
expect(
|
42
|
-
Cmds.new("ruby -e %{script}").chomp(script: "puts STDIN.read") {
|
42
|
+
Cmds::Cmd.new("ruby -e %{script}").chomp(script: "puts STDIN.read") {
|
43
43
|
"hey there!"
|
44
44
|
}
|
45
45
|
).to eq "hey there!"
|
@@ -48,18 +48,18 @@ end # Cmds#chomp
|
|
48
48
|
|
49
49
|
describe "Cmds#chomp!" do
|
50
50
|
it "gets echo output" do
|
51
|
-
expect( Cmds.new("echo %s").chomp!
|
51
|
+
expect( Cmds::Cmd.new("echo %s").chomp! "hey there!" ).to eq "hey there!"
|
52
52
|
end
|
53
53
|
|
54
54
|
it "reads input" do
|
55
55
|
expect(
|
56
|
-
Cmds.new("ruby -e %{script}").chomp!(script: "puts STDIN.read") {
|
56
|
+
Cmds::Cmd.new("ruby -e %{script}").chomp!(script: "puts STDIN.read") {
|
57
57
|
"hey there!"
|
58
58
|
}
|
59
59
|
).to eq "hey there!"
|
60
60
|
end
|
61
61
|
|
62
62
|
it "errors when the command fails" do
|
63
|
-
expect { Cmds.new("false").chomp! }.to raise_error SystemCallError
|
63
|
+
expect { Cmds::Cmd.new("false").chomp! }.to raise_error SystemCallError
|
64
64
|
end
|
65
65
|
end # Cmds#chomp!
|
data/spec/cmds/curry_spec.rb
CHANGED
data/spec/cmds/err_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Cmds.err" do
|
4
4
|
it "gets echo error output" do
|
5
|
-
expect( Cmds.err "echo %s 1>&2",
|
5
|
+
expect( Cmds.err "echo %s 1>&2", "hey there!" ).to eq "hey there!\n"
|
6
6
|
end
|
7
7
|
|
8
8
|
it "reads input" do
|
@@ -16,14 +16,17 @@ end # Cmds.err
|
|
16
16
|
|
17
17
|
describe "Cmds#err" do
|
18
18
|
it "gets echo error output" do
|
19
|
-
expect(
|
19
|
+
expect(
|
20
|
+
Cmds::Cmd.new("echo %s 1>&2").err "hey there!"
|
21
|
+
).to eq "hey there!\n"
|
20
22
|
end
|
21
23
|
|
22
24
|
it "reads input" do
|
23
25
|
expect(
|
24
|
-
Cmds.new("ruby -e %{script}").
|
25
|
-
"
|
26
|
-
|
26
|
+
Cmds::Cmd.new("ruby -e %{script}").
|
27
|
+
err(script: "$stderr.puts STDIN.read") {
|
28
|
+
"hey there!"
|
29
|
+
}
|
27
30
|
).to eq "hey there!\n"
|
28
31
|
end
|
29
|
-
end # Cmds
|
32
|
+
end # Cmds.err
|
data/spec/cmds/error_spec.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "Cmds
|
3
|
+
describe "Cmds.error?" do
|
4
4
|
it "works through instance method" do
|
5
|
-
expect( Cmds.new("true").error? ).to be false
|
6
|
-
expect( Cmds.new("false").error? ).to be true
|
5
|
+
expect( Cmds::Cmd.new("true").error? ).to be false
|
6
|
+
expect( Cmds::Cmd.new("false").error? ).to be true
|
7
7
|
end
|
8
8
|
|
9
9
|
it "works through class method" do
|
10
|
-
expect( Cmds.error? "true").to be false
|
11
|
-
expect( Cmds.error? "false").to be true
|
10
|
+
expect( Cmds.error? "true" ).to be false
|
11
|
+
expect( Cmds.error? "false" ).to be true
|
12
12
|
end
|
13
|
-
end # Cmds
|
13
|
+
end # Cmds.error?
|
data/spec/cmds/ok_spec.rb
CHANGED
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Cmds::ok?" do
|
4
4
|
it "works through instance method" do
|
5
|
-
expect( Cmds.new("true").ok? ).to be true
|
6
|
-
expect( Cmds.new("false").ok? ).to be false
|
5
|
+
expect( Cmds::Cmd.new("true").ok? ).to be true
|
6
|
+
expect( Cmds::Cmd.new("false").ok? ).to be false
|
7
7
|
end
|
8
8
|
|
9
9
|
it "works through class method" do
|
data/spec/cmds/out_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Cmds.out" do
|
4
4
|
it "gets echo output" do
|
5
|
-
expect( Cmds.out "echo %s",
|
5
|
+
expect( Cmds.out "echo %s", "hey there!" ).to eq "hey there!\n"
|
6
6
|
end
|
7
7
|
|
8
8
|
it "reads input" do
|
@@ -16,7 +16,7 @@ end # Cmds.out
|
|
16
16
|
|
17
17
|
describe "Cmds.out!" do
|
18
18
|
it "gets echo output" do
|
19
|
-
expect( Cmds.out! "echo %s",
|
19
|
+
expect( Cmds.out! "echo %s", "hey there!" ).to eq "hey there!\n"
|
20
20
|
end
|
21
21
|
|
22
22
|
it "reads input" do
|
@@ -34,12 +34,12 @@ end # Cmds.out!
|
|
34
34
|
|
35
35
|
describe "Cmds#out" do
|
36
36
|
it "gets echo output" do
|
37
|
-
expect( Cmds.new("echo %s").out
|
37
|
+
expect( Cmds::Cmd.new("echo %s").out "hey there!" ).to eq "hey there!\n"
|
38
38
|
end
|
39
39
|
|
40
40
|
it "reads input" do
|
41
41
|
expect(
|
42
|
-
Cmds.new("ruby -e %{script}").out(script: "puts STDIN.read") {
|
42
|
+
Cmds::Cmd.new("ruby -e %{script}").out(script: "puts STDIN.read") {
|
43
43
|
"hey there!"
|
44
44
|
}
|
45
45
|
).to eq "hey there!\n"
|
@@ -48,18 +48,18 @@ end # Cmds#out
|
|
48
48
|
|
49
49
|
describe "Cmds#out!" do
|
50
50
|
it "gets echo output" do
|
51
|
-
expect( Cmds.new("echo %s").out!
|
51
|
+
expect( Cmds::Cmd.new("echo %s").out! "hey there!" ).to eq "hey there!\n"
|
52
52
|
end
|
53
53
|
|
54
54
|
it "reads input" do
|
55
55
|
expect(
|
56
|
-
Cmds.new("ruby -e %{script}").out!(script: "puts STDIN.read") {
|
56
|
+
Cmds::Cmd.new("ruby -e %{script}").out!(script: "puts STDIN.read") {
|
57
57
|
"hey there!"
|
58
58
|
}
|
59
59
|
).to eq "hey there!\n"
|
60
60
|
end
|
61
61
|
|
62
62
|
it "errors when the command fails" do
|
63
|
-
expect { Cmds.new("false").out! }.to raise_error SystemCallError
|
63
|
+
expect { Cmds::Cmd.new("false").out! }.to raise_error SystemCallError
|
64
64
|
end
|
65
65
|
end # Cmds#out!
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "Cmds
|
3
|
+
describe "Cmds.prepare" do
|
4
4
|
it "should work with a keyword substitutions" do
|
5
5
|
expect(
|
6
|
-
Cmds.
|
6
|
+
Cmds.prepare "psql <%= opts %> <%= database %> < <%= filepath %>",
|
7
7
|
[],
|
8
8
|
database: "blah",
|
9
9
|
filepath: "/where ever/it/is.psql",
|
@@ -17,41 +17,39 @@ describe "Cmds::sub" do
|
|
17
17
|
|
18
18
|
it "should work with positional substitutions" do
|
19
19
|
expect(
|
20
|
-
Cmds.
|
20
|
+
Cmds.prepare "psql <%= arg %> <%= arg %> < <%= arg %>",
|
21
21
|
{
|
22
22
|
username: "bingo bob",
|
23
23
|
host: "localhost",
|
24
24
|
port: 12345,
|
25
25
|
},
|
26
26
|
"blah",
|
27
|
-
"/where ever/it/is.psql"
|
28
|
-
]
|
27
|
+
"/where ever/it/is.psql"
|
29
28
|
).to eq 'psql --host=localhost --port=12345 --username=bingo\ bob blah < /where\ ever/it/is.psql'
|
30
29
|
end
|
31
30
|
|
32
31
|
it "should work with no arguments" do
|
33
32
|
expect(
|
34
|
-
Cmds.
|
33
|
+
Cmds.prepare "blah <% if true %>blow<% end %>"
|
35
34
|
).to eq "blah blow"
|
36
35
|
end
|
37
|
-
|
36
|
+
|
38
37
|
it "should work with positional and keyword substitutions" do
|
39
38
|
expect(
|
40
|
-
Cmds.
|
39
|
+
Cmds.prepare "blah <%= arg %> <%= y %>", "ex", y: "why"
|
41
40
|
).to eq "blah ex why"
|
42
41
|
end
|
43
|
-
|
42
|
+
|
44
43
|
it "should work with direct reference to args" do
|
45
44
|
expect(
|
46
|
-
Cmds.
|
45
|
+
Cmds.prepare "psql <%= @args[1] %> <%= @args[0] %> < <%= @args[2] %>",
|
47
46
|
"blah",
|
48
|
-
"/where ever/it/is.psql",
|
49
47
|
{
|
50
48
|
username: "bingo bob",
|
51
49
|
host: "localhost",
|
52
50
|
port: 12345,
|
53
51
|
},
|
54
|
-
|
52
|
+
"/where ever/it/is.psql"
|
55
53
|
).to eq 'psql --host=localhost --port=12345 --username=bingo\ bob blah < /where\ ever/it/is.psql'
|
56
54
|
end
|
57
55
|
|
@@ -68,7 +66,7 @@ describe "Cmds::sub" do
|
|
68
66
|
|
69
67
|
it "should work when value is present" do
|
70
68
|
expect(
|
71
|
-
Cmds.
|
69
|
+
Cmds.prepare tpl, current_host: 'xyz',
|
72
70
|
domain: 'com.nrser.blah',
|
73
71
|
filepath: '/tmp/export.plist'
|
74
72
|
).to eq "defaults -currentHost xyz export com.nrser.blah /tmp/export.plist"
|
@@ -76,7 +74,7 @@ describe "Cmds::sub" do
|
|
76
74
|
|
77
75
|
it "should work when value is missing" do
|
78
76
|
expect(
|
79
|
-
Cmds.
|
77
|
+
Cmds.prepare tpl, domain: 'com.nrser.blah',
|
80
78
|
filepath: '/tmp/export.plist'
|
81
79
|
).to eq "defaults export com.nrser.blah /tmp/export.plist"
|
82
80
|
end
|
@@ -94,7 +92,7 @@ describe "Cmds::sub" do
|
|
94
92
|
|
95
93
|
it "should loop correctly and escape values" do
|
96
94
|
expect(
|
97
|
-
Cmds.
|
95
|
+
Cmds.prepare tpl, domain: "com.nrser.blah",
|
98
96
|
key: 'k',
|
99
97
|
values: {x: '<ex>', y: 'why'}
|
100
98
|
).to eq "defaults write com.nrser.blah k -dict x \\<ex\\> y why"
|
@@ -109,54 +107,44 @@ describe "Cmds::sub" do
|
|
109
107
|
}
|
110
108
|
|
111
109
|
it "should omit the missing subs" do
|
112
|
-
expect(Cmds.
|
110
|
+
expect(Cmds.prepare tpl, x: "ex", z: "zee").to eq "blah ex zee"
|
113
111
|
end
|
114
112
|
|
115
113
|
it "should omit a sub if it's value is false" do
|
116
|
-
expect(Cmds.
|
114
|
+
expect(Cmds.prepare "%{x?}", x: false).to eq ""
|
117
115
|
end
|
118
116
|
end
|
119
117
|
|
120
118
|
context "errors" do
|
121
|
-
it "should raise TypeError if subs in not an array or a hash" do
|
122
|
-
expect{Cmds.sub "a <%= b %> <%= c %>", "dee!"}.to raise_error TypeError
|
123
|
-
end
|
124
|
-
|
125
119
|
it "should error when a kwarg is missing" do
|
126
120
|
expect {
|
127
|
-
Cmds.
|
121
|
+
Cmds.prepare "a <%= b %> <%= c %>", b: 'bee!'
|
128
122
|
}.to raise_error KeyError
|
129
123
|
end
|
130
124
|
|
131
125
|
it "should error when an arg is missing" do
|
132
126
|
expect {
|
133
|
-
Cmds.
|
127
|
+
Cmds.prepare "a <%= arg %> <%= arg %>", 'bee!'
|
134
128
|
}.to raise_error IndexError
|
135
129
|
end
|
136
|
-
|
137
|
-
it "should error when more than two args are passed" do
|
138
|
-
expect {
|
139
|
-
Cmds.sub "blah", 1, 2, 3
|
140
|
-
}.to raise_error ArgumentError
|
141
|
-
end
|
142
130
|
end # errors
|
143
131
|
|
144
132
|
context "shortcuts" do
|
145
133
|
it "should replace %s" do
|
146
134
|
expect(
|
147
|
-
Cmds.
|
135
|
+
Cmds.prepare "./test/echo_cmd.rb %s", "hello world!"
|
148
136
|
).to eq './test/echo_cmd.rb hello\ world\!'
|
149
137
|
end
|
150
138
|
|
151
139
|
it "should replace %{key}" do
|
152
140
|
expect(
|
153
|
-
Cmds.
|
141
|
+
Cmds.prepare "./test/echo_cmd.rb %{key}", key: "hello world!"
|
154
142
|
).to eq './test/echo_cmd.rb hello\ world\!'
|
155
143
|
end
|
156
144
|
|
157
145
|
it "should replace %<key>s" do
|
158
146
|
expect(
|
159
|
-
Cmds.
|
147
|
+
Cmds.prepare "./test/echo_cmd.rb %<key>s", key: "hello world!"
|
160
148
|
).to eq './test/echo_cmd.rb hello\ world\!'
|
161
149
|
end
|
162
150
|
end # shortcuts
|
data/spec/cmds/stream_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe "Cmds::stream" do
|
|
5
5
|
|
6
6
|
it "writes to $stdout and $stderr by default" do
|
7
7
|
out, err = temp_outs do
|
8
|
-
Cmds.new('./test/tick.rb <%= times %>').stream times: times
|
8
|
+
Cmds::Cmd.new('./test/tick.rb <%= times %>').stream times: times
|
9
9
|
end
|
10
10
|
|
11
11
|
expect(out).to eq times.times.map{|_| "#{_}\n"}.join
|
@@ -15,7 +15,7 @@ describe "Cmds::stream" do
|
|
15
15
|
it "handles writes in blocks" do
|
16
16
|
out_count = 0
|
17
17
|
err_count = 0
|
18
|
-
Cmds.new('./test/tick.rb <%= times %>').stream(times: times) do |io|
|
18
|
+
Cmds::Cmd.new('./test/tick.rb <%= times %>').stream(times: times) do |io|
|
19
19
|
io.on_out do |line|
|
20
20
|
out_count += 1
|
21
21
|
end
|
@@ -32,7 +32,7 @@ describe "Cmds::stream" do
|
|
32
32
|
it "accepts string value input from a block" do
|
33
33
|
|
34
34
|
out, err = temp_outs do
|
35
|
-
Cmds.new("wc -l").stream do
|
35
|
+
Cmds::Cmd.new("wc -l").stream do
|
36
36
|
<<-BLOCK
|
37
37
|
one
|
38
38
|
two
|
@@ -47,7 +47,7 @@ describe "Cmds::stream" do
|
|
47
47
|
|
48
48
|
it "accepts stream value input from a block" do
|
49
49
|
out, err = temp_outs do
|
50
|
-
Cmds.new("wc -l").stream do
|
50
|
+
Cmds::Cmd.new("wc -l").stream do
|
51
51
|
File.open "./test/lines.txt"
|
52
52
|
end
|
53
53
|
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Cmds.tokenize_option" do
|
4
|
+
context "default options" do
|
5
|
+
it "handles integer value" do
|
6
|
+
expect(Cmds.tokenize_option 'x', 1).to eq ["-x 1"]
|
7
|
+
expect(Cmds.tokenize_option 'blah', 1).to eq ["--blah=1"]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "handles string value" do
|
11
|
+
expect(
|
12
|
+
Cmds.tokenize_option 'x', "hey there!"
|
13
|
+
).to eq ["-x hey\\ there\\!"]
|
14
|
+
|
15
|
+
expect(
|
16
|
+
Cmds.tokenize_option 'blah', "hey there!"
|
17
|
+
).to eq ["--blah=hey\\ there\\!"]
|
18
|
+
end
|
19
|
+
|
20
|
+
it "emits just name token for true value" do
|
21
|
+
expect(Cmds.tokenize_option 'x', true).to eq ["-x"]
|
22
|
+
expect(Cmds.tokenize_option 'blah', true).to eq ["--blah"]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "omits token for false value" do
|
26
|
+
expect(Cmds.tokenize_option 'x', false).to eq []
|
27
|
+
expect(Cmds.tokenize_option 'blah', false).to eq []
|
28
|
+
end
|
29
|
+
|
30
|
+
it "omits token for nil value" do
|
31
|
+
expect(Cmds.tokenize_option 'x', nil).to eq []
|
32
|
+
expect(Cmds.tokenize_option 'blah', nil).to eq []
|
33
|
+
end
|
34
|
+
end # default options
|
35
|
+
|
36
|
+
describe "array_mode option" do
|
37
|
+
context "array_mode default" do
|
38
|
+
it "emits joined tokens for array value" do
|
39
|
+
expect(
|
40
|
+
Cmds.tokenize_option 'b', [1, 2, 3]
|
41
|
+
).to eq ['-b 1,2,3']
|
42
|
+
|
43
|
+
expect(
|
44
|
+
Cmds.tokenize_option 'blah', [1, 2, 3]
|
45
|
+
).to eq ['--blah=1,2,3']
|
46
|
+
end
|
47
|
+
|
48
|
+
it "flattens nested arrays" do
|
49
|
+
expect(
|
50
|
+
Cmds.tokenize_option 'blah', [1, [:a, [:b, 3]]]
|
51
|
+
).to eq ['--blah=1,a,b,3']
|
52
|
+
end
|
53
|
+
end # default
|
54
|
+
|
55
|
+
context "array_mode = :join" do
|
56
|
+
def f(name, value)
|
57
|
+
Cmds.tokenize_option name, value, array_mode: :join
|
58
|
+
end
|
59
|
+
|
60
|
+
it "emits joined tokens for array value" do
|
61
|
+
expect(f 'b', [1, 2, 3]).to eq ['-b 1,2,3']
|
62
|
+
|
63
|
+
expect(f 'blah', [1, 2, 3]).to eq ['--blah=1,2,3']
|
64
|
+
end
|
65
|
+
|
66
|
+
it "flattens nested arrays" do
|
67
|
+
expect(f 'blah', [1, [:a, [:b, 3]]]).to eq ['--blah=1,a,b,3']
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "array_join_string option" do
|
71
|
+
context "array_join_string = ', ' (has space in it)" do
|
72
|
+
def f(name, value)
|
73
|
+
Cmds.tokenize_option name, value,
|
74
|
+
array_mode: :join,
|
75
|
+
array_join_string: ', '
|
76
|
+
end
|
77
|
+
|
78
|
+
it "encodes the value as a single shell token" do
|
79
|
+
tokens = f 'b', [1, 2, 3]
|
80
|
+
expect(f 'b', [1, 2, 3]).to eq ['-b 1,\\ 2,\\ 3']
|
81
|
+
|
82
|
+
expect(tokens[0].shellsplit).to eq ['-b', '1, 2, 3']
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end # array_join_string option
|
86
|
+
end # array_mode = :join
|
87
|
+
|
88
|
+
context "array_mode = :repeat" do
|
89
|
+
def f(name, value)
|
90
|
+
Cmds.tokenize_option name, value, array_mode: :repeat
|
91
|
+
end
|
92
|
+
|
93
|
+
it "emits multiple tokens for array value" do
|
94
|
+
expect(f 'b', [1, 2, 3]).to eq ['-b 1', '-b 2', '-b 3']
|
95
|
+
expect(f 'blah', [1, 2, 3]).to eq ['--blah=1', '--blah=2', '--blah=3']
|
96
|
+
end
|
97
|
+
|
98
|
+
it "flattens nested arrays" do
|
99
|
+
expect(f 'blah', [1, [:a, [:b, 3]]]).
|
100
|
+
to eq ['--blah=1', '--blah=a', '--blah=b', '--blah=3']
|
101
|
+
end
|
102
|
+
end # array_mode = :repeat
|
103
|
+
|
104
|
+
context "array_mode = :json" do
|
105
|
+
def f(name, value)
|
106
|
+
Cmds.tokenize_option name, value, array_mode: :json
|
107
|
+
end
|
108
|
+
|
109
|
+
it "emits single json token for array value" do
|
110
|
+
short_tokens = f 'b', [1, 2, 3]
|
111
|
+
|
112
|
+
expect(short_tokens.length).to be 1
|
113
|
+
expect(JSON.load short_tokens[0].shellsplit[1]).to eq [1, 2, 3]
|
114
|
+
end
|
115
|
+
end # array_mode = :json
|
116
|
+
|
117
|
+
end # array_mode option
|
118
|
+
|
119
|
+
end # .tokenize_option
|
data/spec/cmds_spec.rb
CHANGED
@@ -4,4 +4,15 @@ describe Cmds do
|
|
4
4
|
it 'has a version number' do
|
5
5
|
expect(Cmds::VERSION).not_to be nil
|
6
6
|
end
|
7
|
+
|
8
|
+
it "has dees syntax" do
|
9
|
+
expect(Cmds.chomp! "echo 'here'").to eq 'here'
|
10
|
+
|
11
|
+
expect(
|
12
|
+
Cmds::Cmd.new("head %{opts} %s").
|
13
|
+
prepare("/dev/random", opts: {c: 64})
|
14
|
+
).to eq "head -c 64 /dev/random"
|
15
|
+
|
16
|
+
# expect(Cmds.chomp! "echo %s", 'here').to eq 'here'
|
17
|
+
end
|
7
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nrser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nrser
|
@@ -138,18 +138,24 @@ files:
|
|
138
138
|
- Rakefile
|
139
139
|
- ansible/dev.yml
|
140
140
|
- ansible/hosts
|
141
|
+
- bin/rake
|
142
|
+
- bin/rspec
|
141
143
|
- cmds.gemspec
|
142
144
|
- lib/cmds.rb
|
143
|
-
- lib/cmds/
|
145
|
+
- lib/cmds/cmd.rb
|
144
146
|
- lib/cmds/debug.rb
|
145
147
|
- lib/cmds/erb_context.rb
|
146
148
|
- lib/cmds/io_handler.rb
|
147
149
|
- lib/cmds/pipe.rb
|
148
150
|
- lib/cmds/result.rb
|
149
151
|
- lib/cmds/shell_eruby.rb
|
150
|
-
- lib/cmds/
|
152
|
+
- lib/cmds/spawn.rb
|
151
153
|
- lib/cmds/sugar.rb
|
152
154
|
- lib/cmds/util.rb
|
155
|
+
- lib/cmds/util/defaults.rb
|
156
|
+
- lib/cmds/util/params.rb
|
157
|
+
- lib/cmds/util/tokenize_option.rb
|
158
|
+
- lib/cmds/util/tokenize_options.rb
|
153
159
|
- lib/cmds/version.rb
|
154
160
|
- scratch/erb.rb
|
155
161
|
- scratch/popen3.rb
|
@@ -161,17 +167,16 @@ files:
|
|
161
167
|
- spec/cmds/erb_context_spec.rb
|
162
168
|
- spec/cmds/err_spec.rb
|
163
169
|
- spec/cmds/error_spec.rb
|
164
|
-
- spec/cmds/expand_option_hash_spec.rb
|
165
170
|
- spec/cmds/ok_spec.rb
|
166
171
|
- spec/cmds/out_spec.rb
|
172
|
+
- spec/cmds/prepare_spec.rb
|
167
173
|
- spec/cmds/replace_shortcuts_spec.rb
|
168
174
|
- spec/cmds/stream_spec.rb
|
169
|
-
- spec/cmds/
|
175
|
+
- spec/cmds/util/tokenize_option_spec.rb
|
170
176
|
- spec/cmds_spec.rb
|
171
177
|
- spec/debug_helper.rb
|
172
178
|
- spec/spec_helper.rb
|
173
179
|
- test/answers.txt
|
174
|
-
- test/bin/dspec
|
175
180
|
- test/echo_cmd.rb
|
176
181
|
- test/lines.txt
|
177
182
|
- test/questions.rb
|
@@ -196,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
196
201
|
version: '0'
|
197
202
|
requirements: []
|
198
203
|
rubyforge_project:
|
199
|
-
rubygems_version: 2.
|
204
|
+
rubygems_version: 2.6.11
|
200
205
|
signing_key:
|
201
206
|
specification_version: 4
|
202
207
|
summary: helps read, write and remember commands.
|
@@ -208,19 +213,17 @@ test_files:
|
|
208
213
|
- spec/cmds/erb_context_spec.rb
|
209
214
|
- spec/cmds/err_spec.rb
|
210
215
|
- spec/cmds/error_spec.rb
|
211
|
-
- spec/cmds/expand_option_hash_spec.rb
|
212
216
|
- spec/cmds/ok_spec.rb
|
213
217
|
- spec/cmds/out_spec.rb
|
218
|
+
- spec/cmds/prepare_spec.rb
|
214
219
|
- spec/cmds/replace_shortcuts_spec.rb
|
215
220
|
- spec/cmds/stream_spec.rb
|
216
|
-
- spec/cmds/
|
221
|
+
- spec/cmds/util/tokenize_option_spec.rb
|
217
222
|
- spec/cmds_spec.rb
|
218
223
|
- spec/debug_helper.rb
|
219
224
|
- spec/spec_helper.rb
|
220
225
|
- test/answers.txt
|
221
|
-
- test/bin/dspec
|
222
226
|
- test/echo_cmd.rb
|
223
227
|
- test/lines.txt
|
224
228
|
- test/questions.rb
|
225
229
|
- test/tick.rb
|
226
|
-
has_rdoc:
|
data/lib/cmds/capture.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
class Cmds
|
2
|
-
# invokes the command and returns a Result with the captured outputs
|
3
|
-
def capture *subs, &input_block
|
4
|
-
Cmds.debug "entering Cmds#capture",
|
5
|
-
subs: subs,
|
6
|
-
input_block: input_block
|
7
|
-
|
8
|
-
# merge any stored args and kwds and replace input if provided
|
9
|
-
options = merge_options subs, input_block
|
10
|
-
Cmds.debug "merged options:",
|
11
|
-
options: options
|
12
|
-
|
13
|
-
# build the command string
|
14
|
-
cmd = Cmds.sub @template, options[:args], options[:kwds]
|
15
|
-
Cmds.debug "built command string: #{ cmd.inspect }"
|
16
|
-
|
17
|
-
out = ''
|
18
|
-
err = ''
|
19
|
-
|
20
|
-
Cmds.debug "calling Cmds#really_stream..."
|
21
|
-
status = really_stream cmd, options do |io|
|
22
|
-
# send the input to stream, which sends it to spawn
|
23
|
-
io.in = options[:input]
|
24
|
-
|
25
|
-
# and concat the output lines as they come in
|
26
|
-
io.on_out do |line|
|
27
|
-
out += line
|
28
|
-
end
|
29
|
-
|
30
|
-
io.on_err do |line|
|
31
|
-
err += line
|
32
|
-
end
|
33
|
-
end
|
34
|
-
Cmds.debug "Cmds#really_stream completed",
|
35
|
-
status: status
|
36
|
-
|
37
|
-
# build a Result
|
38
|
-
# result = Cmds::Result.new cmd, status, out_reader.value, err_reader.value
|
39
|
-
result = Cmds::Result.new cmd, status, out, err
|
40
|
-
|
41
|
-
# tell the Result to assert if the Cmds has been told to, which will
|
42
|
-
# raise a SystemCallError with the exit status if it was non-zero
|
43
|
-
result.assert if @assert
|
44
|
-
|
45
|
-
return result
|
46
|
-
end # #capture
|
47
|
-
end
|