Top Level Namespace
- Includes:
- SSH_Utils
Defined Under Namespace
Modules: SSH_Utils Classes: Array, Hash, LookupAmbigError, LookupError, LookupNotFoundError
Instance Attribute Summary
Attributes included from SSH_Utils
Instance Method Summary collapse
-
#_fmtargs(args, flag) ⇒ Object
_fmtargs(args, block_given?) { yield }.
-
#_msgargs(args, flag) ⇒ Object
_msgargs(args, block_given?) { yield }.
-
#_prefix_arg0(args, prefix) ⇒ Object
_prefix_arg0( args, prefix ).
- #cmd_run(*args) ⇒ Object (also: #run)
-
#dtalk(*args) ⇒ Object
dtalk - “debug talk” Print msg on STDERR only if ‘$debug` is set.
- #dtalkf(*args) ⇒ Object
-
#error(*args) ⇒ Object
error – print an error message on STDERR, and then exit.
-
#errorf(*args) ⇒ Object
errorf – print a formatted message on STDERR, and then exit.
- #key_lookup(list, key, err_notfound = "%s not found\n", err_ambig = "%s is ambiguous\n") ⇒ Object (also: #lookup)
-
#nrtalk(*args) ⇒ Object
nrtalk – “no run” talk Print msg, prefixed with “(norun) ”, on STDERR only if ‘$norun` is set.
- #nrtalkf(*args) ⇒ Object
-
#nrvtalk(*args) ⇒ Object
nrvtalk – “no run” or verbose talk.
- #nrvtalkf(*args) ⇒ Object
-
#nvtalk(*args) ⇒ Object
nvtalk – “non-verbose” talk Print msg on STDERR unless ‘$verbose` is set.
- #nvtalkf(*args) ⇒ Object
-
#qtalk(*args) ⇒ Object
qtalk - “quiet talk” print msg on STDERR only if ‘$quiet` is set.
- #qtalkf(*args) ⇒ Object
-
#safe_run(*args) ⇒ Object
run – run a command with support for testing, diagnostics and verbosity safe_run – run a command with support for diagnostics and verbosity.
-
#talk(*args) ⇒ Object
talk - Print msg on STDERR unless ‘$quiet` is set.
- #talkf(*args) ⇒ Object
-
#vtalk(*args) ⇒ Object
vtalk - “verbose talk” Print msg on STDERR if ‘$verbose` is set.
- #vtalkf(*args) ⇒ Object
Methods included from SSH_Utils
#_show_cmd, #as, #merge_env, #merge_opts, #merge_opts_with_env, #on, #remote_run, #remote_run_with_output, #ssh_command, #with
Instance Method Details
#_fmtargs(args, flag) ⇒ Object
_fmtargs(args, block_given?) { yield }
merge args with any block results provide default format string if only one argument
45 46 47 48 49 |
# File 'lib/arg-utils.rb', line 45 def _fmtargs args, flag args.concat([yield].flatten) if flag args.unshift('%s') if args.size < 2 args end |
#_msgargs(args, flag) ⇒ Object
_msgargs(args, block_given?) { yield }
merge args with any block results
34 35 36 37 |
# File 'lib/arg-utils.rb', line 34 def _msgargs args, flag args.concat([yield].flatten) if flag args end |
#_prefix_arg0(args, prefix) ⇒ Object
194 195 196 197 198 199 |
# File 'lib/talk-utils.rb', line 194 def _prefix_arg0(args, prefix) if args.size > 0 && !args[0].nil? && !args[0].include?(prefix) args[0] = prefix + args[0] end args end |
#cmd_run(*args) ⇒ Object Also known as: run
67 68 69 70 71 72 73 74 |
# File 'lib/run-utils.rb', line 67 def cmd_run *args args = _msgargs(args, block_given?) { yield } if $norun nrvtalk(args.first) elsif args.size > 0 safe_run(*args) end end |
#dtalk(*args) ⇒ Object
dtalk - “debug talk” Print msg on STDERR only if ‘$debug` is set
:call-seq:
dtalk msg
dtalk { msg }
dtalkf fmt, args ..
dtalkf fmt { [ args .. ] }
dtalkf { [ fmt, args .. ] }
58 59 60 61 62 |
# File 'lib/talk-utils.rb', line 58 def dtalk *args if $debug && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end |
#dtalkf(*args) ⇒ Object
64 65 66 67 68 |
# File 'lib/talk-utils.rb', line 64 def dtalkf *args if $debug && (args.size> 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield }) end end |
#error(*args) ⇒ Object
error – print an error message on STDERR, and then exit. :call-seq:
error [code], msg
error( [code]) { msg }
error {[[code], msg ] }
Code defaults to 1 if not given.
29 30 31 32 33 34 35 |
# File 'lib/error-utils.rb', line 29 def error *args args = _msgargs(args, block_given?) { yield } code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1 $stderr.puts(*args) $stderr.flush exit code end |
#errorf(*args) ⇒ Object
errorf – print a formatted message on STDERR, and then exit
:call-seq:
errorf [code], fmt, args ..
errorf( [code], fmt) { args .. }
errorf( [code]) { fmt, args .. }
errorf {[[code], fmt, args .. ] }
46 47 48 49 50 51 52 53 |
# File 'lib/error-utils.rb', line 46 def errorf *args args = _fmtargs(args, block_given?) { yield } # default the error code to 1 unless the first argument is a Fixnum code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1 $stderr.printf(*args) $stderr.flush exit code end |
#key_lookup(list, key, err_notfound = "%s not found\n", err_ambig = "%s is ambiguous\n") ⇒ Object Also known as: lookup
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lookup.rb', line 37 def key_lookup list, key, err_notfound="%s not found\n", err_ambig="%s is ambiguous\n" keylist = list.is_a?(Hash) ? list.keys : list if exact = keylist.grep(/^#{key}$/i) # exact match? return exact.shift if exact && exact.size == 1 end keys = keylist.grep(/^#{key}/i) case keys.size when 0 unless err_notfound.nil? raise LookupNotFoundError, sprintf(err_notfound, key) end return nil when 1 return keys[0] else unless err_ambig.nil? raise LookupAmbigError, sprintf(err_ambig, key) end return keys end end |
#nrtalk(*args) ⇒ Object
147 148 149 150 151 |
# File 'lib/talk-utils.rb', line 147 def nrtalk *args if $norun && (args.size > 0 || block_given?) $stderr.puts(*_prefix_arg0(_msgargs(args, block_given?){yield},'(norun) ')) end end |
#nrtalkf(*args) ⇒ Object
153 154 155 156 157 |
# File 'lib/talk-utils.rb', line 153 def nrtalkf *args if $norun && (args.size > 0 || block_given?) $stderr.printf(*_prefix_arg0(_fmtargs(args, block_given?){yield},'(norun) ')) end end |
#nrvtalk(*args) ⇒ Object
nrvtalk – “no run” or verbose talk
Print msg, possibly prefixed with “(norun)” or “>>” (verbose), on STDERR only if $norun or $verbose are set.
:call-seq:
nrvtalk msg
nrvtalk { msg }
nrvtalkf fmt, args, ..
nrvtalkf fmt { [ args, ..] }
nrvtalkf { [ fmt, args .. ] }
171 172 173 174 175 176 177 178 |
# File 'lib/talk-utils.rb', line 171 def nrvtalk *args _args = _msgargs(args, block_given?) { yield } if $norun && _args.size > 0 nrtalk(*_args) elsif $verbose && _args.size > 0 vtalk(*_prefix_arg0(_args, '>> ')) end end |
#nrvtalkf(*args) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'lib/talk-utils.rb', line 180 def nrvtalkf *args _args = _fmtargs(args, block_given?) { yield } if $norun && _args.size > 0 nrtalkf(*_args) elsif $verbose && _args.size > 0 vtalkf(*_prefix_arg0(_args, '>> ')) end end |
#nvtalk(*args) ⇒ Object
nvtalk – “non-verbose” talk Print msg on STDERR unless ‘$verbose` is set
:call-seq:
nvtalk msg
nvtalk { msg }
124 125 126 127 128 |
# File 'lib/talk-utils.rb', line 124 def nvtalk *args unless $verbose && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end |
#nvtalkf(*args) ⇒ Object
130 131 132 133 134 |
# File 'lib/talk-utils.rb', line 130 def nvtalkf *args unless $verbose && (args.size > 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield } ) end end |
#qtalk(*args) ⇒ Object
qtalk - “quiet talk” print msg on STDERR only if ‘$quiet` is set
:call-seq:
qtalk msg
qtalk { msg }
qtalkf fmt, args ..
qtalkf fmt { [ args .. ] }
qtalkf { [ fmt, args, ... ] }
81 82 83 84 85 |
# File 'lib/talk-utils.rb', line 81 def qtalk *args if $quiet && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end |
#qtalkf(*args) ⇒ Object
87 88 89 90 91 |
# File 'lib/talk-utils.rb', line 87 def qtalkf *args if $quiet && (args.size > 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield } ) end end |
#safe_run(*args) ⇒ Object
run – run a command with support for testing, diagnostics and verbosity safe_run – run a command with support for diagnostics and verbosity
Both may be given optional ‘errmsg` and `okmsg`, which are printed if given for the corresponding condition.
:call-seq:
run cmd
run { cmd }
run cmd, errmsg
run { [cmd, errmsg] }
run { [cmd, errmsg, okmg] }
run cmd, errmsg, okmsg
safe_run cmd
safe_run cmd, errmsg
safe_run cmd, errmsg, okmsg
safe_run { cmd }
safe_run { [cmd, errmsg] }
safe_run { [cmd, errmsg, okmsg] }
if ‘$norun` is set, print `(norun) ` followed by `cmd` on `STDERR`, and return.
if ‘$verbose` is set, print `>> ` followed by `cmd` on `STDERR`.
Invoke the ‘cmd` with the `system()` call.
If there is an error, show the command (preceded by ‘>> `) if `$verbose` is not set, then show the error code, followed by the given `errmsg` or the default error message.
The ‘cmd` can be given either as an argument, or as the returned value from a block. Important: the block should return a string value to be passed to the system call.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/run-utils.rb', line 49 def safe_run *args args = _msgargs(args, block_given?) { yield } cmd, errmsg, okmsg = args vtalkf ">> %s\n", cmd if cmd if system cmd # invoke the command talk okmsg if okmsg return true else # an error occured qtalkf ">> %s\n", cmd erm = sprintf(errmsg ? errmsg : "Command failed with code %d", $?>>8) talk erm $stderr.flush raise SystemCallError, erm # instead of exit, use raise end end end |
#talk(*args) ⇒ Object
talk - Print msg on STDERR unless ‘$quiet` is set
:call-seq:
talk msg ..
talk { msg .. }
talkf fmt, args ...
talkf fmt { [ args ... ] }
35 36 37 38 39 |
# File 'lib/talk-utils.rb', line 35 def talk *args if !$quiet && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end |
#talkf(*args) ⇒ Object
41 42 43 44 45 |
# File 'lib/talk-utils.rb', line 41 def talkf *args if !$quiet && (args.size > 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield } ) end end |
#vtalk(*args) ⇒ Object
vtalk - “verbose talk” Print msg on STDERR if ‘$verbose` is set
:call-seq:
vtalk msg
vtalk { msg }
vtalkf fmt, args ..
vtalkf fmt { args .. }
vtalkf { [ fmt, args ... ] }
104 105 106 107 108 |
# File 'lib/talk-utils.rb', line 104 def vtalk *args if $verbose && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end |
#vtalkf(*args) ⇒ Object
110 111 112 113 114 |
# File 'lib/talk-utils.rb', line 110 def vtalkf *args if $verbose && (args.size > 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield } ) end end |