command_runner_ng 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/command_runner.rb +39 -9
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a81766b72e2152688fa7ec9329557394dbecb320
4
- data.tar.gz: 4443fc0fa5f5b0c3524607505283f8c20e4bc4d3
2
+ SHA256:
3
+ metadata.gz: 98450272fb53a9e40131a84b04dbf6d52d8afcb597977ef5e9c19081524fe63c
4
+ data.tar.gz: 17ff322d1aa87e0e195fafd816520acbe7d175fc924604dd8632fa2354ae10ab
5
5
  SHA512:
6
- metadata.gz: 3e479989e2f54239a84f27561e8c754838b0924a323ebdc1341fc5cc539a468b05e69af1940164370a6ddca0060bcc820ad46c7fb57b04d995b392bb0f749e1d
7
- data.tar.gz: 9bffe871f6c0155167ee0f97410345b59d92e10f0acff36f88bef8808f18917a167e628d4826e43ca5ce6fd32f215b2dbba9c27d3c1cf6e32a4d80c349e83f21
6
+ metadata.gz: 557a9333803ac3b0ab1e533f48b432ae057dfc84284b0df46cac8edec5e2da5f9c4f4096cf4e9cdfe76c621d9cf16314c7ca7823c90346431c43fe28ce09b873
7
+ data.tar.gz: 7bbc1b9b2e6dc4a22a479a20e89157ff86b95210402d1224092cb3192dc02244cb83be0e6e8db8156e6c3bbec81502409e61f75fb71f46fb9c17256fd182e535
@@ -55,7 +55,7 @@ module CommandRunner
55
55
  # all process start, stop, and timeouts here. To enable debug logging for all commands call
56
56
  # CommandRunner.set_debug_log!($stderr) (or with some other object responding to :puts).
57
57
  #
58
- def self.run(*args, timeout: nil, environment: {}, debug_log: nil, split_stderr: false, options: DEFAULT_OPTIONS)
58
+ def self.run(*args, timeout: nil, environment: {}, debug_log: nil, split_stderr: false, encoding: nil, options: DEFAULT_OPTIONS)
59
59
  if debug_log.nil?
60
60
  debug_log = @@global_debug_log
61
61
  end
@@ -99,7 +99,7 @@ module CommandRunner
99
99
 
100
100
  # Spawn child, merging stderr into stdout
101
101
  io = IO.popen(environment, *args, options)
102
- debug_log.puts("CommandRunnerNG spawn: args=#{args}, timeout=#{timeout}, options: #{options}, PID: #{io.pid}") if debug_log.respond_to?(:puts)
102
+ debug_log.puts("CommandRunnerNG spawn: args=#{args}, timeout=#{timeout}, encoding=#{encoding}, options: #{options}, PID: #{io.pid}") if debug_log.respond_to?(:puts)
103
103
  data = ""
104
104
 
105
105
  err_w.close if split_stderr
@@ -113,9 +113,9 @@ module CommandRunner
113
113
  if Process.wait(io.pid, Process::WNOHANG)
114
114
  read_nonblock_safe!(io, data, tick)
115
115
  read_nonblock_safe!(err_r, errbuf, 0) if split_stderr
116
- result = {:out => data, :status => $?, pid: io.pid}
116
+ result = {:out => ensure_command_output_encoded(data, args.join(" "), encoding), :status => $?, pid: io.pid}
117
117
  if split_stderr
118
- result[:err] = errbuf
118
+ result[:err] = ensure_command_output_encoded(errbuf, args.join(" "), encoding)
119
119
  err_r.close
120
120
  end
121
121
  debug_log.puts("CommandRunnerNG exit: PID: #{io.pid}, code: #{result[:status].exitstatus}") if debug_log.respond_to?(:puts)
@@ -160,9 +160,9 @@ module CommandRunner
160
160
  end
161
161
  Process.wait(io.pid)
162
162
 
163
- result = {:out => data, :status => $?, pid: io.pid}
163
+ result = {:out => ensure_command_output_encoded(data, args.join(" "), encoding), :status => $?, pid: io.pid}
164
164
  if split_stderr
165
- result[:err] = errbuf
165
+ result[:err] = ensure_command_output_encoded(errbuf, args.join(" "), encoding)
166
166
  err_r.close
167
167
  end
168
168
 
@@ -173,6 +173,34 @@ module CommandRunner
173
173
  result
174
174
  end
175
175
 
176
+ # Forces encoding in the specified character set
177
+ # Defaults to no change (outputs in original encoding)
178
+ def self.ensure_command_output_encoded(string, command, encoding = nil)
179
+ return '' if !string
180
+
181
+ return string if encoding.nil? || encoding != :safe
182
+
183
+ encodingName = 'UTF-8'
184
+
185
+ firstPass = string.force_encoding(encodingName)
186
+
187
+ return firstPass if firstPass.valid_encoding?
188
+
189
+ encoded = firstPass.encode(encodingName, encodingName,
190
+ invalid: :replace,
191
+ undef: :replace,
192
+ replace: "")
193
+
194
+ return encoded if encoded.valid_encoding?
195
+
196
+ raise EncodingError, %Q{
197
+ Could not force #{encodingName} encoding on this string:
198
+ #{string}
199
+ which is the output of this command:
200
+ #{command}
201
+ }
202
+ end
203
+
176
204
  # Create a helper instance to launch a command with a given configuration.
177
205
  # Invoke the command with the run() method. The configuration given to create()
178
206
  # can be overriden on each invocation of run().
@@ -184,8 +212,8 @@ module CommandRunner
184
212
  # git.run(:pull, 'origin', 'master')
185
213
  # git.run(:pull, 'origin', 'master', timeout: 2) # override default timeout of 10
186
214
  # git.run(:status) # will raise an error because :status is not in list of allowed commands
187
- def self.create(*args, timeout: nil, environment: {}, allowed_sub_commands: [], debug_log: nil, split_stderr: false, options: DEFAULT_OPTIONS)
188
- CommandInstance.new(args, timeout, environment, allowed_sub_commands, debug_log, split_stderr, options)
215
+ def self.create(*args, timeout: nil, environment: {}, allowed_sub_commands: [], debug_log: nil, split_stderr: false, encoding: nil, options: DEFAULT_OPTIONS)
216
+ CommandInstance.new(args, timeout, environment, allowed_sub_commands, debug_log, split_stderr, encoding, options)
189
217
  end
190
218
 
191
219
  # Log all command line invocations to a logger object responding to :puts. Set to nil to disable.
@@ -205,7 +233,7 @@ module CommandRunner
205
233
 
206
234
  class CommandInstance
207
235
 
208
- def initialize(default_args, default_timeout, default_environment, allowed_sub_commands, debug_log, split_stderr, options)
236
+ def initialize(default_args, default_timeout, default_environment, allowed_sub_commands, debug_log, split_stderr, encoding, options)
209
237
  unless default_args.first.is_a? Array
210
238
  raise "First argument must be an array of command line args. Found #{default_args}"
211
239
  end
@@ -216,6 +244,7 @@ module CommandRunner
216
244
  @allowed_sub_commands = allowed_sub_commands
217
245
  @debug_log = debug_log
218
246
  @split_stderr = split_stderr
247
+ @encoding = encoding
219
248
  @options = options
220
249
  end
221
250
 
@@ -243,6 +272,7 @@ module CommandRunner
243
272
  environment: @default_environment.merge(environment),
244
273
  debug_log: @debug_log,
245
274
  split_stderr: @split_stderr,
275
+ encoding: @encoding,
246
276
  options: @options)
247
277
  end
248
278
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_runner_ng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikkel Kamstrup Erlandsen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-12 00:00:00.000000000 Z
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Helper APIs for advanced interactions with subprocesses and shell commands
14
14
  email: kamikkel@microsoft.com
@@ -37,7 +37,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  version: '0'
38
38
  requirements: []
39
39
  rubyforge_project:
40
- rubygems_version: 2.5.2
40
+ rubygems_version: 2.7.6
41
41
  signing_key:
42
42
  specification_version: 4
43
43
  summary: Command Runner NG