sys_cmd 0.2.1 → 0.3.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 +64 -35
- data/lib/sys_cmd.rb +9 -0
- data/lib/sys_cmd/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01dda967130fa079c9737dc1b506855dff005337
|
4
|
+
data.tar.gz: ff4830b731167310c474fc62ee986efe9f09aae1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 928a7ee0a1a7073afb4cdf638646c4081963889362fd0b327f85d8f20946b208261f9d70640bd07f0f312ede928e362b28c28dde0c3104820b797129023ce27a
|
7
|
+
data.tar.gz: bc9c4e8fe7fd332812ab6b409913c7aeb6e913ef5dfd097d5d522c6e967055d26b086d047eb8fcb2cd7018231a6e049cabb63e3232ff18b6b8cd1ba65a6e95bc
|
data/README.md
CHANGED
@@ -31,37 +31,45 @@ Or install it yourself as:
|
|
31
31
|
A command can be defined with a simple DSL (passing a block that defines
|
32
32
|
the command arguments to the SysCmd.command method):
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
```ruby
|
35
|
+
cmd = SysCmd.command 'ffmpeg' do
|
36
|
+
option '-i', file: 'input video file.mkv'
|
37
|
+
option '-vcodec', 'mjpeg'
|
38
|
+
file 'output.mkv'
|
39
|
+
end
|
40
|
+
```
|
39
41
|
|
40
42
|
The block is executed with +instance_eval+ inside the command definition
|
41
43
|
(an instance of SysCmd::Definicion), so +self+ and instance variables refer
|
42
44
|
to the definition. If this is not desirable an argument can be passed to
|
43
45
|
the block with the +Definition+ object:
|
44
46
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
```ruby
|
48
|
+
cmd = SysCmd.command 'ffmpeg' do |cmd|
|
49
|
+
cmd.option '-i', file: 'input video file.mkv'
|
50
|
+
cmd.option '-vcodec', 'mjpeg'
|
51
|
+
cmd.file 'output.mkv'
|
52
|
+
end
|
53
|
+
```
|
50
54
|
|
51
55
|
The command can be converted to a String which represents it with
|
52
56
|
arguments quoted for the target OS/shell (here we assume a UN*X system)
|
53
57
|
|
54
|
-
|
58
|
+
```ruby
|
59
|
+
puts cmd.to_s # ffmpeg -i input\ video\ file.mkv -vcodec mjpeg output.mkv
|
60
|
+
```
|
55
61
|
|
56
62
|
A command can be generated for a system different from the current host
|
57
63
|
by passing the +:os+ option:
|
58
64
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
+
```ruby
|
66
|
+
wcmd = SysCmd.command 'ffmpeg', os: :windows do |cmd|
|
67
|
+
cmd.option '-i', file: 'input video file.mkv'
|
68
|
+
cmd.option '-vcodec', 'mjpeg'
|
69
|
+
cmd.file 'output.mkv'
|
70
|
+
end
|
71
|
+
puts cmd.to_s # ffmpeg -i "input video file.mkv" -vcodec mjpeg "output.mkv"
|
72
|
+
```
|
65
73
|
|
66
74
|
Currently only +:windows+ (for CMD.EXE syntax) and +:unix+ (for bash syntax) are
|
67
75
|
accepted for the +:os+ parameter. +:unix+ represent any UN*X-like system
|
@@ -69,40 +77,61 @@ accepted for the +:os+ parameter. +:unix+ represent any UN*X-like system
|
|
69
77
|
|
70
78
|
A Command can also be executed:
|
71
79
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
80
|
+
```ruby
|
81
|
+
cmd.run
|
82
|
+
if cmd.success?
|
83
|
+
puts cmd.output
|
84
|
+
end
|
85
|
+
```
|
76
86
|
|
77
87
|
By default execution is done by launching a shell to interpret the command.
|
78
88
|
Unquoted arguments will be interpreted by the shell in that case:
|
79
89
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
90
|
+
```ruby
|
91
|
+
cmd = SysCmd.command 'echo' do
|
92
|
+
argument '$BASH'
|
93
|
+
end
|
94
|
+
cmd.run
|
95
|
+
puts cmd.output # /bin/bash
|
96
|
+
```
|
85
97
|
|
86
98
|
Shell execution can be avoided by passing the +:direct+ option with value
|
87
99
|
+true+ to the +run+ method. In that case the command is executed directly,
|
88
100
|
and no shell interpretation takes place, so:
|
89
101
|
|
90
|
-
|
91
|
-
|
102
|
+
```ruby
|
103
|
+
cmd.run direct: true
|
104
|
+
puts cmd.output # $BASH
|
105
|
+
```
|
92
106
|
|
93
107
|
If the command options include
|
94
108
|
an option with the name of the command being defined it is used to
|
95
109
|
replace the command name. This can be handy to pass user configuration
|
96
110
|
to define the location/name of commands in a particular system:
|
97
111
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
112
|
+
```ruby
|
113
|
+
options = {
|
114
|
+
curl: "/usr/local/bin/curl"
|
115
|
+
}
|
116
|
+
cmd = SysCmd.command 'curl', options do
|
117
|
+
file 'http://jsonip.com'
|
118
|
+
end
|
119
|
+
puts cmd.to_s # /usr/local/bin/curl http://jsonip.com
|
120
|
+
```
|
121
|
+
|
122
|
+
A string can be provided as the standard input for a command
|
123
|
+
by passing the :stdin_data option to the `run` method:
|
105
124
|
|
125
|
+
```ruby
|
126
|
+
cmd = SysCmd.command 'curl' do
|
127
|
+
option '--config -'
|
128
|
+
file 'http://jsonip.com'
|
129
|
+
end
|
130
|
+
cmd.run stdin_data: %{
|
131
|
+
--head
|
132
|
+
}
|
133
|
+
puts cmd.output # "HTTP/1.1 200 OK ...
|
134
|
+
```
|
106
135
|
|
107
136
|
## Development
|
108
137
|
|
data/lib/sys_cmd.rb
CHANGED
@@ -229,6 +229,9 @@ module SysCmd
|
|
229
229
|
# The +:return+ option can be used to make this method return other
|
230
230
|
# attribute of the executed command.
|
231
231
|
#
|
232
|
+
# The +:stdin_data+ option can be used to pass a String as the
|
233
|
+
# command's standar input.
|
234
|
+
#
|
232
235
|
def run(options = {})
|
233
236
|
@output = @status = @error_output = @error = nil
|
234
237
|
if options[:direct]
|
@@ -236,6 +239,9 @@ module SysCmd
|
|
236
239
|
else
|
237
240
|
command = [@command]
|
238
241
|
end
|
242
|
+
if options[:stdin_data]
|
243
|
+
command << { stdin_data: options[:stdin_data] }
|
244
|
+
end
|
239
245
|
begin
|
240
246
|
case options[:error_output]
|
241
247
|
when :mix # mix stderr with stdout
|
@@ -389,6 +395,9 @@ module SysCmd
|
|
389
395
|
end
|
390
396
|
|
391
397
|
def escape_filename(name)
|
398
|
+
if @type == :windows
|
399
|
+
name = name.gsub('/', '\\')
|
400
|
+
end
|
392
401
|
escape name
|
393
402
|
end
|
394
403
|
|
data/lib/sys_cmd/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sys_cmd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Javier Goizueta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: os
|