Module: EcoRake::Shell::Command

Includes:
Rake::DSL
Included in:
Gpg
Defined in:
lib/eco-rake/shell/command.rb

Instance Method Summary collapse

Instance Method Details

#array_cmd(base, *opts) ⇒ String+

Note:

it excludes nil values.

Helper to build command line

Returns:

  • (String, Array<String>)


16
17
18
19
20
21
22
# File 'lib/eco-rake/shell/command.rb', line 16

def array_cmd(base, *opts)
  base = [base] unless base.is_a?(Array)
  base.tap do |out|
    opts.each {|opt| out << opt unless opt.nil?}
    yield(out) if block_given?
  end
end

#double_quote(str) ⇒ String

It double quotes a string (escapes the double quotes)

Parameters:

  • str (String)

Returns:

  • (String)


9
10
11
# File 'lib/eco-rake/shell/command.rb', line 9

def double_quote(str)
  "\"#{str}\"" if str
end

#middlewared_callback(original) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/eco-rake/shell/command.rb', line 74

def middlewared_callback(original)
  proc do |*args, **kargs|
    yield(*args, **kargs)
  ensure
    original.call(*args, **kargs)
  end
end

#sh_chain(cmds, continue: false, &block) ⇒ Object

Parameters:

  • continue (Boolean) (defaults to: false)

    whether it should continue when one failed.

  • arr (Array<String>)

    array of commands to be run



32
33
34
35
36
37
38
# File 'lib/eco-rake/shell/command.rb', line 32

def sh_chain(cmds, continue: false, &block)
  cmds.each do |cmd|
    next sh(cmd, &block) unless continue

    ch_continue(cmd, &block)
  end
end

#sh_continue(comm, &block) ⇒ Object

It continues even if there was an error or exit(1) during the execution

Parameters:

  • comm (String)

    the command line



42
43
44
# File 'lib/eco-rake/shell/command.rb', line 42

def sh_continue(comm, &block)
  sh(comm, &sh_default_block(comm, &block))
end

#sh_default_block(comm) ⇒ Proc

Note:

it wraps block if given

Returns the default block for sh native method.

Returns:

  • (Proc)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/eco-rake/shell/command.rb', line 61

def sh_default_block(comm)
  proc do |ok, res|
    yield(ok, res) if block_given?

    unless ok
      msg = "Command failed (status = #{res.exitstatus})"
      puts "#{msg}\n  • #{comm}"
    end

    res.exitstatus
  end
end

#sh_exit_on_fail(comm) ⇒ Object

Note:

if doesn't raise (prevents) a RuntimeError

It exits if there is an error

Parameters:

  • comm (String)

    the command line



49
50
51
52
53
54
55
56
# File 'lib/eco-rake/shell/command.rb', line 49

def sh_exit_on_fail(comm)
  callback = middlewared_callback(sh_default_block(comm)) do |ok, res|
    next if ok

    exit(1)
  end
  sh(comm, &callback)
end

#string_cmd(base, *options, join: ' ') ⇒ String

Returns:

  • (String)

See Also:



26
27
28
# File 'lib/eco-rake/shell/command.rb', line 26

def string_cmd(base, *options, join: ' ')
  array_cmd(base, *options).join(join)
end