nrser 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nrser/exec.rb +52 -0
- data/lib/nrser/version.rb +1 -1
- data/spec/nrser/exec/run_spec.rb +19 -0
- data/spec/nrser/exec/sub_spec.rb +19 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f304a245c2445a67bec1ddf07dce65e08a4e55bd
|
4
|
+
data.tar.gz: c702f259bb2dea50381d7e8c56a57eeed03713aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b75a5917d0a3752d3d72731bf8b47d0d1a4691bc4288d295cb2bc69629565a27b59d531e752dd34a57e12ca69a787b8d6d1d6b0f199db29d4bda6a9a90c18d28
|
7
|
+
data.tar.gz: 1049973338a16728e22deee3afa4ef5ee4e8ef1da5b3ed696312b07e025d06eabcd5e3ed84bd3cabe67b8baff1dc233e6ce451dfcd653125627039da62cc60cb
|
data/lib/nrser/exec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
|
2
|
+
module NRSER; end
|
3
|
+
|
4
|
+
module NRSER::Exec
|
5
|
+
# substitute stuff into a shell command after escaping with
|
6
|
+
# `Shellwords.escape`.
|
7
|
+
#
|
8
|
+
# arguments after the first may be multiple values that will
|
9
|
+
# be treated like a positional list for substitution, or a single
|
10
|
+
# hash that will be treated like a key substitution.
|
11
|
+
#
|
12
|
+
# any substitution value that is an Array will be treated like a list of
|
13
|
+
# path segments and joined with `File.join`.
|
14
|
+
def self.sub command, subs
|
15
|
+
quoted = case subs
|
16
|
+
when Hash
|
17
|
+
Hash[
|
18
|
+
subs.map do |key, sub|
|
19
|
+
sub = File.join(*sub) if sub.is_a? Array
|
20
|
+
# shellwords in 1.9.3 can't handle symbols
|
21
|
+
sub = sub.to_s if sub.is_a? Symbol
|
22
|
+
[key, Shellwords.escape(sub)]
|
23
|
+
end
|
24
|
+
]
|
25
|
+
when Array
|
26
|
+
subs.map do |sub|
|
27
|
+
sub = File.join(*sub) if sub.is_a? Array
|
28
|
+
# shellwords in 1.9.3 can't handle symbols
|
29
|
+
sub = sub.to_s if sub.is_a? Symbol
|
30
|
+
Shellwords.escape sub
|
31
|
+
end
|
32
|
+
else
|
33
|
+
raise "should be Hash or Array: #{ subs.inspect }"
|
34
|
+
end
|
35
|
+
command % quoted
|
36
|
+
end # ::sub
|
37
|
+
|
38
|
+
def self.run cmd, subs = nil
|
39
|
+
cmd = sub(cmd, subs) unless subs.nil?
|
40
|
+
output = `#{ cmd } 2>&1`
|
41
|
+
exitstatus = $?.exitstatus
|
42
|
+
|
43
|
+
if exitstatus == 0
|
44
|
+
return output
|
45
|
+
else
|
46
|
+
raise SystemCallError.new <<-BLOCK.unblock, exitstatus
|
47
|
+
hey - cmd `#{ cmd }` failed with status #{ $?.exitstatus }
|
48
|
+
and output #{ output.inspect }
|
49
|
+
BLOCK
|
50
|
+
end
|
51
|
+
end # ::exec
|
52
|
+
end
|
data/lib/nrser/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'nrser/exec'
|
4
|
+
|
5
|
+
describe "NRSER::Exec.run" do
|
6
|
+
|
7
|
+
it "subs a string with spaces" do
|
8
|
+
expect(
|
9
|
+
NRSER::Exec.run "echo %{msg}", msg: "hey there"
|
10
|
+
).to eq "hey there\n"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "subs a string with $" do
|
14
|
+
expect(
|
15
|
+
NRSER::Exec.run "echo %{msg}", msg: "$HOME"
|
16
|
+
).to eq "$HOME\n"
|
17
|
+
end
|
18
|
+
|
19
|
+
end # describe run
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'nrser/exec'
|
4
|
+
|
5
|
+
describe "NRSER::Exec.sub" do
|
6
|
+
|
7
|
+
it "runs a string with spaces" do
|
8
|
+
expect(
|
9
|
+
NRSER::Exec.sub "echo %{msg}", msg: "hey there"
|
10
|
+
).to eq 'echo hey\ there'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "runs a string with $" do
|
14
|
+
expect(
|
15
|
+
NRSER::Exec.sub "echo %{msg}", msg: "$HOME"
|
16
|
+
).to eq 'echo \$HOME'
|
17
|
+
end
|
18
|
+
|
19
|
+
end # describe sub
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nrser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nrser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,10 +67,13 @@ files:
|
|
67
67
|
- README.md
|
68
68
|
- Rakefile
|
69
69
|
- lib/nrser.rb
|
70
|
+
- lib/nrser/exec.rb
|
70
71
|
- lib/nrser/version.rb
|
71
72
|
- nrser.gemspec
|
72
73
|
- spec/nrser/common_prefix_spec.rb
|
73
74
|
- spec/nrser/dedent_spec.rb
|
75
|
+
- spec/nrser/exec/run_spec.rb
|
76
|
+
- spec/nrser/exec/sub_spec.rb
|
74
77
|
- spec/nrser/format_exception_spec.rb
|
75
78
|
- spec/nrser/indent_spec.rb
|
76
79
|
- spec/nrser/template_spec.rb
|
@@ -104,6 +107,8 @@ summary: basic ruby utils i use in a lot of stuff.
|
|
104
107
|
test_files:
|
105
108
|
- spec/nrser/common_prefix_spec.rb
|
106
109
|
- spec/nrser/dedent_spec.rb
|
110
|
+
- spec/nrser/exec/run_spec.rb
|
111
|
+
- spec/nrser/exec/sub_spec.rb
|
107
112
|
- spec/nrser/format_exception_spec.rb
|
108
113
|
- spec/nrser/indent_spec.rb
|
109
114
|
- spec/nrser/template_spec.rb
|