cmd-utils 1.1.2 → 1.2.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: 1f047ffb87574fcc165f9651ff733e2f42f0c2e3
4
- data.tar.gz: b64622069ed70aca2474362658474f88c9f26542
3
+ metadata.gz: 19937d5ab0376abd84254c084edaec772b501d42
4
+ data.tar.gz: 130e9b5ec4997a36db8c4e46503650ca55f53288
5
5
  SHA512:
6
- metadata.gz: d74547e65fb7d59e99aae4cc87d6bb358c3b3a0a784e2894b90e37db53e58f0be35fbb10cf107c3c0c8f6b4cc74a17fada172071c5f658d4498232a7ac7580fd
7
- data.tar.gz: e56c5364aed15bb4cc340924bf0e64c434007081e600eaa3ddd65e7377a22858fae8671b8244707f905d4e93754442857ce693371470e13424a7d83ba141b137
6
+ metadata.gz: a1fe074ab79e0f4e541f3ab7c3325ad60e836e40ff8895168591131ea1391433347eb5f3dbb1e3d9a25eb053643105c48ccd2f66e7303fbd91e75da28c87c9ac
7
+ data.tar.gz: 9f359fed8970713e8a5ce436ce031a9b5d67d0ac80383691a89daa195d275c001f84f573e039664f776a8f2dcfe7e4ec539e0e9a897c60291ab4268bd14d7d0b
data/Gemfile CHANGED
@@ -6,10 +6,8 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "minitest", ">= 5.3.0"
10
- #gem "shoulda"
11
- gem "rdoc"
12
- gem "bundler"
13
- gem "jeweler"
14
- gem "simplecov"
9
+ gem "minitest", '~> 5.3'
10
+ gem "rdoc", "~> 4.1"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 2.0"
15
13
  end
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- addressable (2.3.5)
4
+ addressable (2.3.6)
5
5
  builder (3.2.2)
6
- descendants_tracker (0.0.3)
7
- docile (1.1.3)
6
+ descendants_tracker (0.0.4)
7
+ thread_safe (~> 0.3, >= 0.3.1)
8
8
  faraday (0.9.0)
9
9
  multipart-post (>= 1.2, < 3)
10
- git (1.2.6)
10
+ git (1.2.7)
11
11
  github_api (0.11.3)
12
12
  addressable (~> 2.3)
13
13
  descendants_tracker (~> 0.0.1)
@@ -16,7 +16,7 @@ GEM
16
16
  multi_json (>= 1.7.5, < 2.0)
17
17
  nokogiri (~> 1.6.0)
18
18
  oauth2
19
- hashie (2.0.5)
19
+ hashie (3.0.0)
20
20
  highline (1.6.21)
21
21
  jeweler (2.0.1)
22
22
  builder
@@ -28,37 +28,31 @@ GEM
28
28
  rake
29
29
  rdoc
30
30
  json (1.8.1)
31
- jwt (0.1.11)
32
- multi_json (>= 1.5)
33
- mini_portile (0.5.2)
34
- minitest (5.3.0)
35
- multi_json (1.8.4)
31
+ jwt (1.0.0)
32
+ mini_portile (0.6.0)
33
+ minitest (5.3.5)
34
+ multi_json (1.10.1)
36
35
  multi_xml (0.5.5)
37
36
  multipart-post (2.0.0)
38
- nokogiri (1.6.1)
39
- mini_portile (~> 0.5.0)
40
- oauth2 (0.9.3)
37
+ nokogiri (1.6.2.1)
38
+ mini_portile (= 0.6.0)
39
+ oauth2 (0.9.4)
41
40
  faraday (>= 0.8, < 0.10)
42
- jwt (~> 0.1.8)
41
+ jwt (~> 1.0)
43
42
  multi_json (~> 1.3)
44
43
  multi_xml (~> 0.5)
45
44
  rack (~> 1.2)
46
45
  rack (1.5.2)
47
- rake (10.1.1)
46
+ rake (10.3.2)
48
47
  rdoc (4.1.1)
49
48
  json (~> 1.4)
50
- simplecov (0.8.2)
51
- docile (~> 1.1.0)
52
- multi_json
53
- simplecov-html (~> 0.8.0)
54
- simplecov-html (0.8.0)
49
+ thread_safe (0.3.4)
55
50
 
56
51
  PLATFORMS
57
52
  ruby
58
53
 
59
54
  DEPENDENCIES
60
- bundler
61
- jeweler
62
- minitest (>= 5.3.0)
63
- rdoc
64
- simplecov
55
+ bundler (~> 1.0)
56
+ jeweler (~> 2.0)
57
+ minitest (~> 5.3)
58
+ rdoc (~> 4.1)
data/README.md CHANGED
@@ -1,5 +1,4 @@
1
- cmd-utils
2
- =========
1
+ # cmd-utils
3
2
 
4
3
  Utilities for writing command line tools in ruby.
5
4
 
@@ -9,27 +8,35 @@ Installation:
9
8
 
10
9
  Usage:
11
10
 
12
- require 'cmd-utils'
13
- require 'ssh-utils'
11
+ require 'cmd-utils' # include all libraries in this repo
12
+
13
+ or
14
+
15
+ require 'arg-utils'
16
+ require 'error-utils'
14
17
  require 'lookup'
18
+ require 'run-utils'
19
+ require 'ssh-utils'
20
+ require 'talk-utils'
15
21
 
16
22
  This gem provides:
17
23
 
18
- * routines for output on `$stderr`, controlled by several global variables
19
- * error-reporting-and-exit
20
- * system call handling, with verbose or debug output, error and "ok" message handling
21
- * remote system command invocation (based on ssh)
22
- * ambiguous, case-insensitive string lookups in arrays or hashs, with error handling
24
+ * `arg-utils`: help manage variable argumentlists.
25
+ * `talk-utils`: routines for output on `$stderr`, controlled by several global variables.
26
+ * `error-utils`: error-reporting-and-exit.
27
+ * `run-utils`: system call handling, with verbose or debug output, error and "ok" message handling.
28
+ * `ssh-utils`: remote system command invocation (based on ssh).
29
+ * `lookup`: ambiguous, case-insensitive string lookups in arrays or hashs, with error handling.
30
+ * `cmd-utils`: includes all the above libraries.
23
31
 
24
- talk, dtalk, qtalk, vtalk, nrtalk, nvtalk
25
- =========================================
32
+ ## talk-utils: talk, dtalk, qtalk, vtalk, nrtalk, nvtalk
26
33
 
27
34
  These utilities provide simple utilities that rely on the following global variables:
28
35
 
29
- `$verbose` -- enables vtalk(f) function output
30
- `$norun` -- enables nrtalk(f) output, and disables the "run" command execution
31
- `$quiet` -- disables talk(f) output, and enables qtalk(f) function output
32
- `$debug` -- enables dtalk(f) function output
36
+ $verbose -- enables vtalk(f) function output
37
+ $norun -- enables nrtalk(f) output, and disables the run command execution
38
+ $quiet -- disables talk(f) output, and enables qtalk(f) function output
39
+ $debug -- enables dtalk(f) function output
33
40
 
34
41
  These routines provide option-controlled output. The arguments can be given as
35
42
  part of the the function calls, or, can be provided as the return value of a
@@ -94,8 +101,26 @@ Error output:
94
101
  The `error` routine take an optional numeric first argument which is used to
95
102
  set the exit code. If not given, the exit code is 1.
96
103
 
97
- `cmd_run`
98
- --------
104
+ ## error-utils: `error`, `errorf`
105
+
106
+ The `error` and `errorf` family of methods make it easy display an error message
107
+ and then exit, possibly with a specific error code.
108
+
109
+ The arguments may be given as a parameters on the method, or as return values
110
+ from a yield block.
111
+
112
+ error MSG
113
+ error {MSG}
114
+ error CODE, MSG
115
+ error( CODE) { MSG }
116
+ error{ CODE, MSG }
117
+
118
+ errorf FMT, *ARGS
119
+ errorf( FMT){ ARGS }
120
+ errorf{ FMT, ARGS }
121
+
122
+
123
+ ## run-utils: `cmd_run`, `safe_run`
99
124
 
100
125
  cmd_run cmd
101
126
  cmd_run { cmd }
@@ -134,11 +159,10 @@ command evaluation with the `$verbose` treatment.
134
159
  safe_run { cmd }
135
160
  safe_run {[cmd, errmsg, okmsg]}
136
161
 
137
- ssh-utils
138
- =========
162
+ ## ssh-utils
139
163
 
140
164
  A module to define some routines for running commands across many systems using
141
- ssh. Environment variables can be specified (PATH is used by default).
165
+ ssh. Environment variables can be specified (`PATH` is used by default).
142
166
 
143
167
  Usage:
144
168
 
@@ -146,14 +170,13 @@ Usage:
146
170
 
147
171
  on serverlist, :debug = true do |server|
148
172
  as user do
149
- with PATH
173
+ with PATH do
150
174
  remote_run :whoami
151
175
  end
152
176
  end
153
177
  end
154
178
 
155
- Method Descriptions
156
- -------------------
179
+ ### `on`
157
180
 
158
181
  on SERVERLIST, OPTIONSHASH |server|
159
182
  BLOCK-TO-EXECUTE-FOR-EACH-SERVER
@@ -193,6 +216,7 @@ is globally available, but the `:debug` option is a block-local specification.
193
216
  Sets the `:verbose` flag for use within the associated block. The `$verbose` flag
194
217
  is globally available, but the `:verbose` option is a block-local specification.
195
218
 
219
+ ### `as`
196
220
 
197
221
  as USERLIST, OPTIONSHASH
198
222
  BLOCK-TO-EXECUTE-FOR-EACH-USER
@@ -215,10 +239,18 @@ option. For example, the following two sections are equivalent:
215
239
  remote_run :whoami
216
240
  end
217
241
 
242
+ ### `with`
243
+
244
+ as USERLIST, OPTIONSHASH do
245
+ with SOMEENVAR do
246
+ BLOCK-TO-EXECUTE-WITH-SOMEENVAR
247
+ end
248
+ end
218
249
 
250
+ The `with` method is used to set additional environment variables, or to
251
+ reset them.
219
252
 
220
- lookup
221
- ======
253
+ ## lookup-utils:
222
254
 
223
255
  usage:
224
256
 
@@ -249,7 +281,6 @@ nil.
249
281
  If `err_ambigmsg` is nil, do not raise a `LookupAmbigError`, and return the list
250
282
  of possible results.
251
283
 
252
- Author
253
- ------
284
+ # Author
254
285
 
255
286
  Alan K. Stebbens <aks@stebbens.org>
data/Rakefile CHANGED
@@ -32,11 +32,11 @@ Rake::TestTask.new(:test) do |test|
32
32
  test.verbose = true
33
33
  end
34
34
 
35
- desc "Code coverage detail"
36
- task :simplecov do
37
- ENV['COVERAGE'] = "true"
38
- Rake::Task['test'].execute
39
- end
35
+ #desc "Code coverage detail"
36
+ #task :simplecov do
37
+ # ENV['COVERAGE'] = "true"
38
+ # Rake::Task['test'].execute
39
+ #end
40
40
 
41
41
  task :default => :test
42
42
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.2.0
data/cmd-utils.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: cmd-utils 1.1.2 ruby lib
5
+ # stub: cmd-utils 1.2.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "cmd-utils"
9
- s.version = "1.1.2"
9
+ s.version = "1.2.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Alan K. Stebbens"]
14
- s.date = "2014-03-03"
14
+ s.date = "2014-06-25"
15
15
  s.description = "Several ruby libraries for building command-line utilities."
16
16
  s.email = "aks@stebbens.org"
17
17
  s.extra_rdoc_files = [
@@ -27,13 +27,17 @@ Gem::Specification.new do |s|
27
27
  "Rakefile",
28
28
  "VERSION",
29
29
  "cmd-utils.gemspec",
30
+ "lib/arg-utils.rb",
30
31
  "lib/cmd-utils.rb",
32
+ "lib/error-utils.rb",
31
33
  "lib/lookup.rb",
34
+ "lib/run-utils.rb",
32
35
  "lib/ssh-utils.rb",
36
+ "lib/talk-utils.rb",
33
37
  "test/helper.rb",
34
- "test/test-cmd-utils.rb",
35
38
  "test/test-lookup.rb",
36
- "test/test_cmd-utils.rb.off"
39
+ "test/test-run-utils.rb",
40
+ "test/test-talk-utils.rb"
37
41
  ]
38
42
  s.homepage = "http://bitbucket.org/aks_/cmd-utils"
39
43
  s.licenses = ["MIT"]
@@ -44,24 +48,21 @@ Gem::Specification.new do |s|
44
48
  s.specification_version = 4
45
49
 
46
50
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
- s.add_development_dependency(%q<minitest>, [">= 5.3.0"])
48
- s.add_development_dependency(%q<rdoc>, [">= 0"])
49
- s.add_development_dependency(%q<bundler>, [">= 0"])
50
- s.add_development_dependency(%q<jeweler>, [">= 0"])
51
- s.add_development_dependency(%q<simplecov>, [">= 0"])
51
+ s.add_development_dependency(%q<minitest>, ["~> 5.3"])
52
+ s.add_development_dependency(%q<rdoc>, ["~> 4.1"])
53
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
54
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0"])
52
55
  else
53
- s.add_dependency(%q<minitest>, [">= 5.3.0"])
54
- s.add_dependency(%q<rdoc>, [">= 0"])
55
- s.add_dependency(%q<bundler>, [">= 0"])
56
- s.add_dependency(%q<jeweler>, [">= 0"])
57
- s.add_dependency(%q<simplecov>, [">= 0"])
56
+ s.add_dependency(%q<minitest>, ["~> 5.3"])
57
+ s.add_dependency(%q<rdoc>, ["~> 4.1"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 2.0"])
58
60
  end
59
61
  else
60
- s.add_dependency(%q<minitest>, [">= 5.3.0"])
61
- s.add_dependency(%q<rdoc>, [">= 0"])
62
- s.add_dependency(%q<bundler>, [">= 0"])
63
- s.add_dependency(%q<jeweler>, [">= 0"])
64
- s.add_dependency(%q<simplecov>, [">= 0"])
62
+ s.add_dependency(%q<minitest>, ["~> 5.3"])
63
+ s.add_dependency(%q<rdoc>, ["~> 4.1"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 2.0"])
65
66
  end
66
67
  end
67
68
 
data/lib/arg-utils.rb ADDED
@@ -0,0 +1,52 @@
1
+ # arg-utils.rb -- simple methods for managing variable argument lists
2
+ #
3
+ # Alan K. Stebbens <aks@stebbens.org>
4
+ #
5
+ #
6
+ # require 'arg-utils'
7
+ #
8
+ # defines two methods:
9
+ #
10
+ # _msgargs(args, block_given?) { yield }
11
+ # _fmtargs(args, block_given?) { yield }
12
+ #
13
+ # These two functions can be used to pass a variable list of arguments
14
+ # to a function accepting such, including possibly using a BLOCK to
15
+ # produce some (or all) of the values.
16
+ #
17
+ # Example:
18
+ #
19
+ # def dtalkf(*args) {
20
+ # if $debug
21
+ # $stderr.printf(*_fmtargs(args, block_given?) { yield })
22
+ # end
23
+ # }
24
+ #
25
+ # The arguments pass to `printf` will come from either the arguments
26
+ # passed to `dtalkf` or the BLOCK given on the call to `dtalkf`.
27
+ #
28
+
29
+ ####
30
+ # _msgargs(args, block_given?) { yield }
31
+ #
32
+ # merge args with any block results
33
+
34
+ def _msgargs args, flag
35
+ args.concat([yield].flatten) if flag
36
+ args
37
+ end
38
+
39
+ ####
40
+ # _fmtargs(args, block_given?) { yield }
41
+ #
42
+ # merge args with any block results
43
+ # provide default format string if only one argument
44
+
45
+ def _fmtargs args, flag
46
+ args.concat([yield].flatten) if flag
47
+ args.unshift('%s') if args.size < 2
48
+ args
49
+ end
50
+
51
+ # end of arg-utils.sh
52
+ # vim: set ai sw=2
data/lib/cmd-utils.rb CHANGED
@@ -1,279 +1,17 @@
1
- # cmd-utils.rb -- simple utilities for ruby command line tools
1
+ # cmd-utils.rb -- a collection of utilities for ruby command line tools
2
2
  #
3
3
  # Alan K. Stebbens <aks@stebbens.org>
4
4
  #
5
- #
6
5
  # require 'cmd-utils'
7
6
  #
8
- # Utilities for option-controlled output, and running commands.
9
- #
10
- # The output and run methods rely on some external variables:
11
- #
12
- # $verbose -- enables vtalk(f) output
13
- # $norun -- enables nrtalk(f) output and controls the "run" command
14
- # $quiet -- enables qtalk(f) output, and disables talk(f) output
15
- # $debug -- enables dtalk(f) output
16
- #
17
- # These routines provide conditional output. The arguments can be given as
18
- # part of the the function calls, or, can be provided as the return value of a
19
- # block. The advantage of using a block is that the block is not evaluated
20
- # unless the conditions requiring output are met. So, if the expression to
21
- # compute a value that _might_ be printed is expensive, do the computation
22
- # inside a block.
23
- #
24
- ##
25
- # talk - Print msg on STDERR unless `$quiet` is set
26
- #
27
- # :call-seq:
28
- # talk msg ..
29
- # talk { msg .. }
30
- # talkf fmt, args ...
31
- # talkf fmt { [ args ... ] }
32
-
33
- def talk *args
34
- if !$quiet && (args.size > 0 || block_given?)
35
- $stderr.puts(*_msgargs(args, block_given?) { yield })
36
- end
37
- end
38
-
39
- def talkf *args
40
- if !$quiet && (args.size > 0 || block_given?)
41
- $stderr.printf(*_fmtargs(args, block_given?) { yield } )
42
- end
43
- end
44
-
45
- # _msgargs(args, block_given?) { yield }
46
- #
47
- # merge args with any block results
48
-
49
- def _msgargs args, flag
50
- args.concat([yield].flatten) if flag
51
- args
52
- end
53
-
54
- # _fmtargs(args, block_given?) { yield }
55
- #
56
- # merge args with any block results
57
- # provide default format string if only one argument
58
-
59
- def _fmtargs args, flag
60
- args.concat([yield].flatten) if flag
61
- args.unshift('%s') if args.size < 2
62
- args
63
- end
64
-
65
- ##
66
- # dtalk - "debug talk"
67
- # Print msg on STDERR only if `$debug` is set
68
- #
69
- # :call-seq:
70
- # dtalk msg
71
- # dtalk { msg }
72
- # dtalkf fmt, args ..
73
- # dtalkf fmt { [ args .. ] }
74
-
75
- def dtalk *args
76
- if $debug && (args.size > 0 || block_given?)
77
- $stderr.puts(*_msgargs(args, block_given?) { yield })
78
- end
79
- end
80
-
81
- def dtalkf *args
82
- if $debug && (args.size> 0 || block_given?)
83
- $stderr.printf(*_fmtargs(args, block_given?) { yield })
84
- end
85
- end
86
-
87
- ##
88
- # qtalk - "quiet talk"
89
- # print msg on STDERR only if `$quiet` is set
90
- #
91
- # :call-seq:
92
- # qtalk msg
93
- # qtalk { msg }
94
- # qtalkf fmt, args ..
95
- # qtalkf fmt { [ args .. ] }
96
-
97
- def qtalk *args
98
- if $quiet && (args.size > 0 || block_given?)
99
- $stderr.puts(*_msgargs(args, block_given?) { yield })
100
- end
101
- end
102
-
103
- def qtalkf *args
104
- if $quiet && (args.size > 0 || block_given?)
105
- $stderr.printf(*_fmtargs(args, block_given?) { yield } )
106
- end
107
- end
108
-
109
- ##
110
- # vtalk - "verbose talk"
111
- # Print msg on STDERR if `$verbose` is set
112
- #
113
- # :call-seq:
114
- # vtalk msg
115
- # vtalk { msg }
116
- # vtalkf fmt, args ..
117
- # vtalkf fmt { args .. }
118
-
119
- def vtalk *args
120
- if $verbose && (args.size > 0 || block_given?)
121
- $stderr.puts(*_msgargs(args, block_given?) { yield })
122
- end
123
- end
124
-
125
- def vtalkf *args
126
- if $verbose && (args.size > 0 || block_given?)
127
- $stderr.printf(*_fmtargs(args, block_given?) { yield } )
128
- end
129
- end
130
-
131
- ##
132
- # nvtalk -- "non-verbose" talk
133
- # Print msg on STDERR unless `$verbose` is set
134
- #
135
- # :call-seq:
136
- # nvtalk msg
137
- # nvtalk { msg }
138
-
139
- def nvtalk *args
140
- unless $verbose && (args.size > 0 || block_given?)
141
- $stderr.puts(*_msgargs(args, block_given?) { yield })
142
- end
143
- end
144
-
145
- def nvtalkf *args
146
- unless $verbose && (args.size > 0 || block_given?)
147
- $stderr.printf(*_fmtargs(args, block_given?) { yield } )
148
- end
149
- end
150
-
151
- ##
152
- # nrtalk -- "no run" talk
153
- # Print msg, prefixed with "(norun) ", on STDERR only if `$norun` is set
154
- #
155
- # :call-seq:
156
- # nrtalk msg
157
- # nrtalk { msg }
158
- # nrtalkf fmt, msg
159
- # nrtalkf fmt { msg }
160
-
161
- def nrtalk *args
162
- if $norun && (args.size > 0 || block_given?)
163
- newargs = _msgargs(args, block_given?) { yield }
164
- newargs[0] = '(norun) ' + newargs[0] unless newargs.size == 0 || newargs[0].nil? || newargs[0].include?('(norun)')
165
- $stderr.puts(*newargs)
166
- end
167
- end
168
-
169
- def nrtalkf *args
170
- if $norun && (args.size > 0 || block_given?)
171
- newargs = _fmtargs(args, block_given?) { yield }
172
- newargs[0] = '(norun) ' + newargs[0] unless newargs.size == 0 || newargs[0].nil? || newargs[0].include?('(norun)')
173
- $stderr.printf(*newargs)
174
- end
175
- end
176
-
177
- ##
178
- # error -- print an error message on STDERR, and then exit.
179
- # :call-seq:
180
- # error [code], msg
181
- # error( [code]) { msg }
182
- # error {[[code], msg ] }
183
- #
184
- # Code defaults to 1 if not given.
185
-
186
- def error *args
187
- args = _msgargs(args, block_given?) { yield }
188
- code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1
189
- $stderr.puts(*args)
190
- $stderr.flush
191
- exit code
192
- end
193
-
194
- ##
195
- # errorf -- print a formatted message on STDERR, and then exit
196
- #
197
- # :call-seq:
198
- # errorf [code], fmt, args ..
199
- # errorf( [code], fmt) { args .. }
200
- # errorf( [code]) { fmt, args .. }
201
- # errorf {[[code], fmt, args .. ] }
202
-
203
- def errorf *args
204
- args = _fmtargs(args, block_given?) { yield }
205
- # default the error code to 1 unless the first argument is a Fixnum
206
- code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1
207
- $stderr.printf(*args)
208
- $stderr.flush
209
- exit code
210
- end
211
-
212
- ##
213
- # run -- run a command with support for testing, diagnostics and verbosity
214
- # safe_run -- run a command with support for diagnostics and verbosity
215
- #
216
- # Both may be given optional `errmsg` and `okmsg`, which are printed if given
217
- # for the corresponding condition.
218
- #
219
- # :call-seq:
220
- # run cmd
221
- # run { cmd }
222
- # run cmd, errmsg
223
- # run { [cmd, errmsg] }
224
- # run { [cmd, errmsg, okmg] }
225
- # run cmd, errmsg, okmsg
226
- #
227
- # safe_run cmd
228
- # safe_run cmd, errmsg
229
- # safe_run cmd, errmsg, okmsg
230
- # safe_run { cmd }
231
- # safe_run { [cmd, errmsg] }
232
- # safe_run { [cmd, errmsg, okmsg] }
233
- #
234
- # if `$norun` is set, print `(norun) ` followed by `cmd` on `STDERR`, and
235
- # return.
236
- #
237
- # if `$verbose` is set, print `>> ` followed by `cmd` on `STDERR`.
238
- #
239
- # Invoke the `cmd` with the `system()` call.
240
- #
241
- # If there is an error, show the command (preceded by `>> `) if `$verbose` is
242
- # not set, then show the error code, followed by the given `errmsg` or the
243
- # default error message.
244
- #
245
- # The `cmd` can be given either as an argument, or as the returned value from a
246
- # block. Important: the block should return a string value to be passed to
247
- # the system call.
248
-
249
- def cmd_run *args
250
- args = _msgargs(args, block_given?) { yield }
251
- if $norun
252
- nrtalk(args.first)
253
- elsif args.size > 0
254
- safe_run *args
255
- end
256
- end
257
-
258
- alias run cmd_run
7
+ # This brings in several related utilities.
259
8
 
260
- def safe_run *args
261
- args = _msgargs(args, block_given?) { yield }
262
- cmd, errmsg, okmsg = args
263
- vtalkf ">> %s\n", cmd
264
- if cmd
265
- if system cmd # invoke the command
266
- $stderr.puts okmsg if okmsg
267
- return true
268
- else # an error occured
269
- qtalkf ">> %s\n", cmd
270
- erm = sprintf(errmsg ? errmsg : "Command failed with code %d", $?>>8)
271
- $stderr.puts erm
272
- $stderr.flush
273
- raise SystemCallError, erm # instead of exit, use raise
274
- end
275
- end
276
- end
9
+ require 'arg-utils'
10
+ require 'talk-utils'
11
+ require 'error-utils'
12
+ require 'lookup'
13
+ require 'run-utils'
14
+ require 'ssh-utils'
277
15
 
278
16
  # end of cmd-utils.sh
279
17
  # vim: set ai sw=2