sys_cmd 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10dbd56d23cb306a8131e7dca5bb2b4030029c12
4
- data.tar.gz: 2c8af860be280b526ce6872f905192b26da8ac15
3
+ metadata.gz: 01dda967130fa079c9737dc1b506855dff005337
4
+ data.tar.gz: ff4830b731167310c474fc62ee986efe9f09aae1
5
5
  SHA512:
6
- metadata.gz: f70e9c7c2ef3442d106b07d9a8141e9059890481c9efe8c449905a6ec15c4a8e5e0658f98d72e835633a60e548cd70b86a6efa2287e24e3e3b15984f11836879
7
- data.tar.gz: 0a81e9ea7b04dcac8acec39760578aaaa8ecfc636e292759a52628ad244c763476a77753ac2a417259e3c7355e88fb433ffced9c00fc034398a235002b078902
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
- cmd = SysCmd.command 'ffmpeg' do
35
- option '-i', file: 'input video file.mkv'
36
- option '-vcodec', 'mjpeg'
37
- file 'output.mkv'
38
- end
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
- cmd = SysCmd.command 'ffmpeg' do |cmd|
46
- cmd.option '-i', file: 'input video file.mkv'
47
- cmd.option '-vcodec', 'mjpeg'
48
- cmd.file 'output.mkv'
49
- end
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
- puts cmd.to_s # ffmpeg -i input\ video\ file.mkv -vcodec mjpeg output.mkv
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
- wcmd = SysCmd.command 'ffmpeg', os: :windows do |cmd|
60
- cmd.option '-i', file: 'input video file.mkv'
61
- cmd.option '-vcodec', 'mjpeg'
62
- cmd.file 'output.mkv'
63
- end
64
- puts cmd.to_s # ffmpeg -i "input video file.mkv" -vcodec mjpeg "output.mkv"
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
- cmd.run
73
- if cmd.success?
74
- puts cmd.output
75
- end
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
- cmd = SysCmd.command 'echo' do
81
- argument '$BASH'
82
- end
83
- cmd.run
84
- puts cmd.output # /bin/bash
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
- cmd.run direct: true
91
- puts cmd.output # $BASH
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
- options = {
99
- curl: "/usr/local/bin/curl"
100
- }
101
- cmd = SysCmd.command 'curl', options do
102
- file 'http://jsonip.com'
103
- end
104
- puts cmd.to_s # /usr/local/bin/curl http://jsonip.com
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
 
@@ -1,3 +1,3 @@
1
1
  module SysCmd
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.1
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-04-27 00:00:00.000000000 Z
11
+ date: 2015-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: os