Class: Cmds::Cmd
- Inherits:
-
Object
- Object
- Cmds::Cmd
- Defined in:
- lib/cmds/cmd.rb
Overview
a command consisting of a template, base/common parameters and input, and options.
Instance Attribute Summary collapse
-
#args ⇒ Array<Object>
readonly
base/common positional parameters to render into the command template.
-
#assert ⇒ Boolean
readonly
if
true, will execution will raise an error on non-zero exit code. -
#env ⇒ Hash{String | Symbol => String}
readonly
environment variables to set for command execution.
-
#format ⇒ :squish | :pretty
readonly
format specifier symbol:.
-
#input ⇒ String | #read
readonly
string or readable IO-like object to use as default input to the command.
-
#kwds ⇒ Hash{Symbol => Object}
readonly
base/common keyword parameters to render into the command template.
-
#template ⇒ String
readonly
ERB stirng template (with Cmds-specific extensions) for the command.
Instance Method Summary collapse
-
#capture(*args, **kwds, &input_block) ⇒ Cmds::Result
(also: #call)
executes the command and returns a Result with the captured outputs.
-
#chomp(*args, **kwds, &input_block) ⇒ String
captures and chomps stdout (sugar for
#out(*subs, &input_block).chomp). -
#chomp!(*args, **kwds, &input) ⇒ String
captures and chomps stdout, raising an error if the command failed.
-
#curry(*args, **kwds, &input_block) ⇒ Object
returns a new Cmd with the parameters and input merged in.
-
#err(*args, **kwds, &input_block) ⇒ String
captures and returns stdout (sugar for
#capture(*subs, &input_block).err). -
#error?(*args, **kwds, &io_block) ⇒ Boolean
execute command and return
trueif it failed. -
#initialize(template, **opts) ⇒ Cmd
constructor
construct a Cmd.
-
#ok?(*args, **kwds, &io_block) ⇒ Boolean
execute command and return
trueif it exited successfully. -
#out(*args, **kwds, &input_block) ⇒ String
captures and returns stdout (sugar for
#capture(*args, **kwds, &input_block).out). -
#out!(*args, **kwds, &input) ⇒ String
captures and returns stdout (sugar for
#capture(*args, **kwds, &input_block).out). -
#prepare(*args, **kwds) ⇒ String
prepare a shell-safe command string for execution.
- #proxy ⇒ Object
-
#render(*args, **kwds) ⇒ String
render parameters into
@template. - #stream(*args, **kwds, &io_block) ⇒ Object
Constructor Details
#initialize(template, **opts) ⇒ Cmd
construct a Cmd.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cmds/cmd.rb', line 104 def initialize template, **opts Cmds.debug "Cmd constructing...", template: template, opts: opts @template = template @args = opts[:args] || [] @kwds = opts[:kwds] || {} @input = opts[:input] || nil @assert = opts[:assert] || false @env = opts[:env] || {} @format = opts[:format] || :squish @env_mode = opts[:env_mode] || :inline end |
Instance Attribute Details
#args ⇒ Array<Object> (readonly)
32 33 34 |
# File 'lib/cmds/cmd.rb', line 32 def args @args end |
#assert ⇒ Boolean (readonly)
if true, will execution will raise an error on non-zero exit code.
defaults to false.
60 61 62 |
# File 'lib/cmds/cmd.rb', line 60 def assert @assert end |
#env ⇒ Hash{String | Symbol => String} (readonly)
environment variables to set for command execution.
defaults to {}.
67 68 69 |
# File 'lib/cmds/cmd.rb', line 67 def env @env end |
#format ⇒ :squish | :pretty (readonly)
format specifier symbol:
-
:squish- collapse rendered command string to one line.
-
:pretty- clean up and backslash suffix line endings.
defaults to :squish.
79 80 81 |
# File 'lib/cmds/cmd.rb', line 79 def format @format end |
#input ⇒ String | #read (readonly)
53 54 55 |
# File 'lib/cmds/cmd.rb', line 53 def input @input end |
#kwds ⇒ Hash{Symbol => Object} (readonly)
43 44 45 |
# File 'lib/cmds/cmd.rb', line 43 def kwds @kwds end |
#template ⇒ String (readonly)
ERB stirng template (with Cmds-specific extensions) for the command.
20 21 22 |
# File 'lib/cmds/cmd.rb', line 20 def template @template end |
Instance Method Details
#capture(*args, **kwds, &input_block) ⇒ Cmds::Result Also known as: call
executes the command and returns a Result with the captured outputs.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/cmds/cmd.rb', line 208 def capture *args, **kwds, &input_block Cmds.debug "entering Cmds#capture", args: args, kwds: kwds, input: input # prepare the command string cmd = prepare *args, **kwds # extract input from block via `call` if one is provided, # otherwise default to instance variable (which may be `nil`) input = input_block.nil? ? @input : input_block.call Cmds.debug "prepared", cmd: cmd, input: input # strings output will be concatenated onto out = '' err = '' Cmds.debug "calling Cmds.spawn..." status = Cmds.spawn( cmd, # we deal with input in the block nil, # include env if mode is spawn argument (@env_mode == :spawn_arg ? @env : {}), ) do |io| # send the input to stream, which sends it to spawn io.in = input # and concat the output lines as they come in io.on_out do |line| out += line end io.on_err do |line| err += line end end Cmds.debug "Cmds.spawn completed", status: status # build a Result # result = Cmds::Result.new cmd, status, out_reader.value, err_reader.value result = Cmds::Result.new cmd, status, out, err # tell the Result to assert if the Cmds has been told to, which will # raise a SystemCallError with the exit status if it was non-zero result.assert if @assert return result end |
#chomp(*args, **kwds, &input_block) ⇒ String
captures and chomps stdout
(sugar for #out(*subs, &input_block).chomp).
357 358 359 |
# File 'lib/cmds/cmd.rb', line 357 def chomp *args, **kwds, &input_block out(*args, **kwds, &input_block).chomp end |
#chomp!(*args, **kwds, &input) ⇒ String
captures and chomps stdout, raising an error if the command failed.
(sugar for #out!(*subs, &input_block).chomp).
378 379 380 |
# File 'lib/cmds/cmd.rb', line 378 def chomp! *args, **kwds, &input out!(*args, **kwds, &input).chomp end |
#curry(*args, **kwds, &input_block) ⇒ Object
returns a new Cmds::Cmd with the parameters and input merged in
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/cmds/cmd.rb', line 121 def curry *args, **kwds, &input_block self.class.new @template, { args: (@args + args), kwds: (@kwds.merge kwds), input: (input ? input.call : @input), assert: @assert, env: @env, format: @format, } end |
#err(*args, **kwds, &input_block) ⇒ String
captures and returns stdout
(sugar for #capture(*subs, &input_block).err).
396 397 398 |
# File 'lib/cmds/cmd.rb', line 396 def err *args, **kwds, &input_block capture(*args, **kwds, &input_block).err end |
#error?(*args, **kwds, &io_block) ⇒ Boolean
execute command and return true if it failed.
292 293 294 |
# File 'lib/cmds/cmd.rb', line 292 def error? *args, **kwds, &io_block stream(*args, **kwds, &io_block) != 0 end |
#ok?(*args, **kwds, &io_block) ⇒ Boolean
execute command and return true if it exited successfully.
278 279 280 |
# File 'lib/cmds/cmd.rb', line 278 def ok? *args, **kwds, &io_block stream(*args, **kwds, &io_block) == 0 end |
#out(*args, **kwds, &input_block) ⇒ String
captures and returns stdout
(sugar for #capture(*args, **kwds, &input_block).out).
322 323 324 |
# File 'lib/cmds/cmd.rb', line 322 def out *args, **kwds, &input_block capture(*args, **kwds, &input_block).out end |
#out!(*args, **kwds, &input) ⇒ String
captures and returns stdout
(sugar for #capture(*args, **kwds, &input_block).out).
340 341 342 |
# File 'lib/cmds/cmd.rb', line 340 def out! *args, **kwds, &input capture(*args, **kwds, &input).assert.out end |
#prepare(*args, **kwds) ⇒ String
prepare a shell-safe command string for execution.
171 172 173 |
# File 'lib/cmds/cmd.rb', line 171 def prepare *args, **kwds Cmds.format render(*args, **kwds), @format end |
#proxy ⇒ Object
302 303 304 305 306 |
# File 'lib/cmds/cmd.rb', line 302 def proxy stream do |io| io.in = $stdin end end |
#render(*args, **kwds) ⇒ String
the returned string is not formatted for shell execution.
Cmds passes this string through Cmds.format before execution,
which addresses newlines in the rendered string through "squishing"
everything down to one line or adding \ to line ends.
render parameters into @template.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/cmds/cmd.rb', line 146 def render *args, **kwds context = Cmds::ERBContext.new((@args + args), @kwds.merge(kwds)) erb = Cmds::ShellEruby.new Cmds.replace_shortcuts(@template) rendered = NRSER.dedent erb.result(context.get_binding) if @env_mode == :inline && !@env.empty? rendered = @env.sort_by {|name, value| name }.map {|name, value| "#{ name }=#{ Cmds.esc value }" }.join("\n\n") + "\n\n" + rendered end rendered end |
#stream(*args, **kwds, &io_block) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/cmds/cmd.rb', line 176 def stream *args, **kwds, &io_block Cmds.debug "entering Cmd#stream", args: args, kwds: kwds, io_block: io_block Cmds.spawn prepare(*args, **kwds), @input, # include env if mode is spawn argument (@env_mode == :spawn_arg ? @env : {}), &io_block end |