obfusk-util 0.4.3 → 0.4.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30f9d007dcb9e565a07d90b2294e5277238e3962
4
- data.tar.gz: 6a32009fc08b0c4ac6afa7941860405d3855453e
3
+ metadata.gz: ace37fd05ca1a55b9ed7197c9ef69ad8b25b5ebd
4
+ data.tar.gz: d1f1fd09ce7432c58810fa072358a2b114d138d6
5
5
  SHA512:
6
- metadata.gz: 2d0ec221df62096ced64e7ee2c35f0e0832c38e1448d09cd589c5a368aba15c8e112760068462c5132fecae47f3ed053b4fa4a2a41eb3f2dffbcf85249863274
7
- data.tar.gz: 159554003b57938c2ceb3f939e37a48b8a598be5a274527f698ff6cf4d228dc173ac77af7fc84c60e8a4a4d081abac019fed68c9735b9509d30702660728a093
6
+ metadata.gz: 48e4a35b37818666e5037dc416b912e2952cdc854eae275b7821a9f14274d921b99db71e47fe7212d47367be6f8e07e8565c57ea100ed4d88eb0217534016a9c
7
+ data.tar.gz: e0dfe81bb323b6222b408333184edfe287d56aada548352b23bfe6cd2032fd971625aadbb45970d809f56c7b7fe5237c0ef6f04f36b1505f2090d20646fb4b19
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Date : 2014-02-20
6
6
 
7
7
  Copyright : Copyright (C) 2014 Felix C. Stegerman
8
- Version : v0.4.3
8
+ Version : v0.4.4
9
9
 
10
10
  []: }}}1
11
11
 
@@ -210,6 +210,14 @@ Obfusk::Util::sh? 'false'
210
210
 
211
211
  Obfusk::Util::sh! 'false'
212
212
  # => RunError
213
+
214
+ Obfusk::Util::sys 'echo "$0" ">>$1<<" ">>$FOO<<"', '"one"', 'FOO' => 'foo'
215
+ # stdout: bash >>"one"<< >>foo<<
216
+
217
+ Obfusk::Util::sys! 'echo FOO; false'
218
+ # stdout: FOO
219
+ # => RunError
220
+
213
221
  ```
214
222
 
215
223
  []: }}}2
@@ -25,8 +25,7 @@ module Obfusk; module Util
25
25
  # @raise RunError when exitstatus non-zero
26
26
  # @return [Sh] self
27
27
  def ok!
28
- ::Obfusk::Util.chk_exitstatus cmd, status.exitstatus
29
- self
28
+ ::Obfusk::Util.chk_exitstatus cmd, status.exitstatus; self
30
29
  end
31
30
 
32
31
  # ... TODO ...
@@ -35,11 +34,12 @@ module Obfusk; module Util
35
34
 
36
35
  # --
37
36
 
38
- # run a command using bash (w/ arguments)
37
+ # run a command using bash (w/ arguments) and capture its output;
38
+ # see also `sys`; uses `capture{2e,3}`
39
39
  #
40
40
  # ```
41
- # sh 'echo "$0" ">>$1<<" ">>$FOO<<"', '"one"', 'FOO' => 'foo'
42
- # # => bash >>"one"<< >>foo<<
41
+ # sh('echo "$0" ">>$1<<" ">>$FOO<<"', '"one"', 'FOO' => 'foo').stdout
42
+ # # => %Q{bash >>"one"<< >>foo<<}
43
43
  # ```
44
44
  #
45
45
  # @param [Hash] args.last
@@ -52,24 +52,13 @@ module Obfusk; module Util
52
52
  # * any other `Symbol` key is passed as an option to `capture3`
53
53
  # @return [Sh]
54
54
  def self.sh(cmd, *args) # {{{1
55
- a, o = args.last && (l = args.last.dup).is_a?(Hash) ?
56
- [args[0..-2],l] : [args,{}]
57
- shell = o.delete(:shell) || 'bash'
58
- print = o.delete :print
59
- exit = o.delete :exit
60
- merge = o.delete :merge
61
- env = o.delete(:env) || {}
62
- syms = o.select { |k,v| Symbol === k }
63
- strs = o.select { |k,v| String === k }
64
- opts = syms.merge env: env.merge(strs)
65
- c = [shell] + (print ? %w{-x} : []) + (exit ? %w{-e} : []) +
66
- ['-c', cmd, shell] + a
67
- if merge
68
- stderr = nil; stdout, status = capture2e *c, opts
55
+ c = _sh cmd, args
56
+ if c[:merge]
57
+ stderr = nil; stdout, status = capture2e(*c[:cmd], c[:opts])
69
58
  else
70
- stdout, stderr, status = capture3 *c, opts
59
+ stdout, stderr, status = capture3(*c[:cmd], c[:opts])
71
60
  end
72
- Sh.new c, status, stdout, stderr
61
+ Sh.new c[:cmd], status, stdout, stderr
73
62
  end # }}}1
74
63
 
75
64
  # `sh(...).ok?`
@@ -84,6 +73,34 @@ module Obfusk; module Util
84
73
 
85
74
  # --
86
75
 
76
+ # run a command using bash (w/ arguments); takes the same arguments
77
+ # as `sh`; uses `spawn_w`
78
+ #
79
+ # ```
80
+ # sys 'echo "$0" ">>$1<<" ">>$FOO<<"', '"one"', 'FOO' => 'foo'
81
+ # # stdout: bash >>"one"<< >>foo<<
82
+ # ```
83
+ #
84
+ # @return [Process::Status]
85
+ def self.sys(cmd, *args)
86
+ c = _sh cmd, args; o = c[:opts]
87
+ o_ = c[:merge] ? o.merge(:err => o[:out] ? [:child, :out] : :out) : o
88
+ spawn_w(*c[:cmd], o_)
89
+ end
90
+
91
+ # `sys(...).exitstatus == 0`
92
+ def self.sys?(cmd, *args)
93
+ sys(cmd, *args).exitstatus == 0
94
+ end
95
+
96
+ # `sys(...)`
97
+ # @raise RunError when exitstatus non-zero
98
+ def self.sys!(cmd, *args)
99
+ sys(cmd, *args).tap { |s| chk_exitstatus cmd, s.exitstatus }
100
+ end
101
+
102
+ # --
103
+
87
104
  # ohai + sh; requires `obfusk/util/message`
88
105
  def self.osh(*args)
89
106
  ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; sh(*args)
@@ -99,6 +116,42 @@ module Obfusk; module Util
99
116
  ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; sh!(*args)
100
117
  end
101
118
 
119
+ # --
120
+
121
+ # ohai + sys; requires `obfusk/util/message`
122
+ def self.osys(*args)
123
+ ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; sys(*args)
124
+ end
125
+
126
+ # ohai + sys?; requires `obfusk/util/message`
127
+ def self.osys?(*args)
128
+ ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; sys?(*args)
129
+ end
130
+
131
+ # ohai + sys!; requires `obfusk/util/message`
132
+ def self.osys!(*args)
133
+ ::Obfusk::Util.ohai _spawn_rm_opts(args)*' '; sys!(*args)
134
+ end
135
+
136
+ # --
137
+
138
+ # helper
139
+ def self._sh(cmd, args) # {{{1
140
+ a, o = args.last && (l = args.last.dup).is_a?(Hash) ?
141
+ [args[0..-2],l] : [args,{}]
142
+ shell = o.delete(:shell) || 'bash'
143
+ print = o.delete :print
144
+ exit = o.delete :exit
145
+ merge = o.delete :merge
146
+ env = o.delete(:env) || {}
147
+ syms = o.select { |k,v| Symbol === k }
148
+ strs = o.select { |k,v| String === k }
149
+ opts = syms.merge env: env.merge(strs)
150
+ c = [shell] + (print ? %w{-x} : []) + (exit ? %w{-e} : []) +
151
+ ['-c', cmd, shell] + a
152
+ { cmd: c, opts: opts, merge: merge }
153
+ end # }}}1
154
+
102
155
  end; end
103
156
 
104
157
  # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -1,5 +1,5 @@
1
1
  # my namespace
2
2
  module Obfusk; module Util
3
- VERSION = '0.4.3'
3
+ VERSION = '0.4.4'
4
4
  DATE = '2014-02-20'
5
5
  end; end
@@ -120,6 +120,113 @@ describe 'obfusk/util/sh' do
120
120
  end
121
121
  end # }}}1
122
122
 
123
+ context 'sys (1)' do # {{{1
124
+ it 'echoes w/ args and env' do
125
+ r, w = IO.pipe
126
+ res = ou.sys 'echo "$0" ">>$1<<" ">>$FOO<<"', '"one"',
127
+ 'FOO' => 'foo', out: w
128
+ w.close; x = r.read; r.close
129
+ expect(res.exitstatus).to eq(0)
130
+ expect(x).to eq(%Q{bash >>"one"<< >>foo<<\n})
131
+ end
132
+ it 'works w/ print, exit, and merge' do
133
+ r, w = IO.pipe
134
+ res = ou.sys 'echo step1; false; echo step3', print: true, exit: true,
135
+ merge: true, out: w
136
+ w.close; x = r.read; r.close
137
+ expect(res.exitstatus).to_not eq(0)
138
+ expect(x).to eq("+ echo step1\nstep1\n+ false\n")
139
+ end
140
+ it 'outputs pwd w/ arg' do
141
+ r, w = IO.pipe
142
+ res = ou.sys 'cd "$1"; pwd', '/', out: w
143
+ w.close; x = r.read; r.close
144
+ expect(res.exitstatus).to eq(0)
145
+ expect(x).to eq("/\n")
146
+ end
147
+ it 'outputs pwd w/ :chdir' do
148
+ r, w = IO.pipe
149
+ res = ou.sys 'pwd', chdir: '/', out: w
150
+ w.close; x = r.read; r.close
151
+ expect(res.exitstatus).to eq(0)
152
+ expect(x).to eq("/\n")
153
+ end
154
+ end # }}}1
155
+
156
+ context 'sys (2)' do # {{{1
157
+ it 'prints to stderr w/ print/-x' do
158
+ ro, wo = IO.pipe; re, we = IO.pipe
159
+ res = ou.sys 'echo FOO; echo BAR', print: true, out: wo, err: we
160
+ wo.close; o = ro.read; ro.close
161
+ we.close; e = re.read; re.close
162
+ expect(res.exitstatus).to eq(0)
163
+ expect(o).to eq("FOO\nBAR\n")
164
+ expect(e).to eq("+ echo FOO\n+ echo BAR\n")
165
+ end
166
+ it 'ignores false w/o exit/-e' do
167
+ r, w = IO.pipe
168
+ res = ou.sys 'echo FOO; false; echo BAR', out: w
169
+ w.close; x = r.read; r.close
170
+ expect(res.exitstatus).to eq(0)
171
+ expect(x).to eq("FOO\nBAR\n")
172
+ end
173
+ it 'stops at false w/ exit/-e' do
174
+ r, w = IO.pipe
175
+ res = ou.sys 'echo FOO; false; echo BAR', exit: true, out: w
176
+ w.close; x = r.read; r.close
177
+ expect(res.exitstatus).to_not eq(0)
178
+ expect(x).to eq("FOO\n")
179
+ end
180
+ it 'merges stdout and stderr w/ merge' do
181
+ r, w = IO.pipe
182
+ res = ou.sys 'echo FOO; echo BAR >&2; echo BAZ', merge: true, out: w
183
+ w.close; x = r.read; r.close
184
+ expect(res.exitstatus).to eq(0)
185
+ expect(x).to eq("FOO\nBAR\nBAZ\n")
186
+ end
187
+ it 'merges env w/ string keys' do
188
+ r, w = IO.pipe
189
+ res = ou.sys 'echo $FOO; echo $BAR',
190
+ env: { 'FOO' => 'no', 'BAR' => 'bar' }, 'FOO' => 'yes', out: w
191
+ w.close; x = r.read; r.close
192
+ expect(res.exitstatus).to eq(0)
193
+ expect(x).to eq("yes\nbar\n")
194
+ end
195
+ end # }}}1
196
+
197
+ context 'sys (3)' do # {{{1
198
+ it 'fails w/ message on stderr w/ NONEXISTENT' do
199
+ r, w = IO.pipe
200
+ res = ou.sys 'NONEXISTENT', err: w
201
+ w.close; x = r.read; r.close
202
+ expect(res.exitstatus).to eq(127)
203
+ expect(x).to eq("bash: NONEXISTENT: command not found\n")
204
+ end
205
+ it 'fails w/ RunError w/ shell NONEXISTENT' do
206
+ expect { ou.sys 'irrelevant', shell: 'NONEXISTENT' } .to \
207
+ raise_error(ou::RunError, /failed to spawn command/)
208
+ end
209
+ end # }}}1
210
+
211
+ context 'sys?' do # {{{1
212
+ it 'true => true' do
213
+ expect(ou.sys? 'true').to eq(true)
214
+ end
215
+ it 'false => false' do
216
+ expect(ou.sys? 'false').to eq(false)
217
+ end
218
+ end # }}}1
219
+
220
+ context 'sys!' do # {{{1
221
+ it 'true => Process::Status' do
222
+ expect( ou.sys! 'true' ).to be_an_instance_of(Process::Status)
223
+ end
224
+ it 'false => RunError' do
225
+ expect { ou.sys! 'false' } .to \
226
+ raise_error(ou::RunError, /command returned non-zero/)
227
+ end
228
+ end # }}}1
229
+
123
230
  end
124
231
 
125
232
  # vim: set tw=70 sw=2 sts=2 et fdm=marker :
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obfusk-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix C. Stegerman
@@ -14,28 +14,28 @@ dependencies:
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: |
@@ -46,44 +46,44 @@ executables: []
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
- - .yardopts
49
+ - ".yardopts"
50
50
  - README.md
51
51
  - Rakefile
52
52
  - obfusk-util.gemspec
53
- - lib/obfusk/util/die.rb
54
- - lib/obfusk/util/sh.rb
55
- - lib/obfusk/util/misc.rb
56
- - lib/obfusk/util/term.rb
57
- - lib/obfusk/util/spec.rb
58
- - lib/obfusk/util/fs.rb
59
- - lib/obfusk/util/message.rb
60
- - lib/obfusk/util/process.rb
61
- - lib/obfusk/util/all.rb
62
- - lib/obfusk/util/os.rb
63
53
  - lib/obfusk/util/run.rb
54
+ - lib/obfusk/util/misc.rb
64
55
  - lib/obfusk/util/opt.rb
65
- - lib/obfusk/util/cmd.rb
66
- - lib/obfusk/util/module.rb
67
- - lib/obfusk/util/struct.rb
56
+ - lib/obfusk/util/os.rb
68
57
  - lib/obfusk/util/valid.rb
58
+ - lib/obfusk/util/module.rb
59
+ - lib/obfusk/util/all.rb
60
+ - lib/obfusk/util/process.rb
61
+ - lib/obfusk/util/fs.rb
62
+ - lib/obfusk/util/sh.rb
69
63
  - lib/obfusk/util/data.rb
64
+ - lib/obfusk/util/spec.rb
65
+ - lib/obfusk/util/message.rb
70
66
  - lib/obfusk/util/version.rb
67
+ - lib/obfusk/util/die.rb
68
+ - lib/obfusk/util/term.rb
69
+ - lib/obfusk/util/struct.rb
70
+ - lib/obfusk/util/cmd.rb
71
71
  - spec/obfusk/util/die_spec.rb
72
- - spec/obfusk/util/struct_spec.rb
73
- - spec/obfusk/util/process_spec.rb
74
- - spec/obfusk/util/fs_spec.rb
75
- - spec/obfusk/util/cmd_spec.rb
72
+ - spec/obfusk/util/valid_spec.rb
73
+ - spec/obfusk/util/sh_spec.rb
74
+ - spec/obfusk/util/message_spec.rb
75
+ - spec/obfusk/util/term_spec.rb
76
+ - spec/obfusk/util/os_spec.rb
76
77
  - spec/obfusk/util/spec_spec.rb
77
- - spec/obfusk/util/run_spec.rb
78
78
  - spec/obfusk/util/opt_spec.rb
79
- - spec/obfusk/util/misc_spec.rb
80
- - spec/obfusk/util/term_spec.rb
81
- - spec/obfusk/util/sh_spec.rb
79
+ - spec/obfusk/util/struct_spec.rb
82
80
  - spec/obfusk/util/module_spec.rb
83
- - spec/obfusk/util/os_spec.rb
84
- - spec/obfusk/util/valid_spec.rb
85
- - spec/obfusk/util/message_spec.rb
81
+ - spec/obfusk/util/fs_spec.rb
82
+ - spec/obfusk/util/misc_spec.rb
83
+ - spec/obfusk/util/cmd_spec.rb
86
84
  - spec/obfusk/util/data_spec.rb
85
+ - spec/obfusk/util/process_spec.rb
86
+ - spec/obfusk/util/run_spec.rb
87
87
  homepage: https://github.com/obfusk/rb-obfusk-util
88
88
  licenses:
89
89
  - LGPLv3+
@@ -94,12 +94,12 @@ require_paths:
94
94
  - lib
95
95
  required_ruby_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
- - - '>='
97
+ - - ">="
98
98
  - !ruby/object:Gem::Version
99
99
  version: 1.9.1
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - '>='
102
+ - - ">="
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
@@ -109,3 +109,4 @@ signing_key:
109
109
  specification_version: 4
110
110
  summary: miscellaneous utility library for ruby
111
111
  test_files: []
112
+ has_rdoc: