Class: Loom::Shell::CmdWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/loom/shell/cmd_wrapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*cmd, should_quote: false, is_wrapped: false, redirect: []) ⇒ CmdWrapper

in quotes.

Parameters:

  • cmd (Array<[#to_s]>)

    Command parts that will be shell escaped.

  • :should_quote (Boolean)

    Whether wrapped commands should be quoted.

  • :redirc (Array<CmdRedirect>)

    STDIO redirection for the command



41
42
43
44
45
46
47
48
49
50
# File 'lib/loom/shell/cmd_wrapper.rb', line 41

def initialize(*cmd, should_quote: false, is_wrapped: false, redirect: [])
  if cmd.last.is_a?(Hash)
    raise ArgumentError.new "kwargs mixed into cmd"
  end
  @cmd_parts = cmd.flatten
  @should_quote = should_quote
  @is_wrapped = is_wrapped
  @redirects = [redirect].flatten.compact
  Loom.log.debug2(self) { "CmdWrapper.new {#{cmd}} => #{self.escape_cmd}" }
end

Instance Attribute Details

#cmd_partsObject (readonly)

Returns the value of attribute cmd_parts.



52
53
54
# File 'lib/loom/shell/cmd_wrapper.rb', line 52

def cmd_parts
  @cmd_parts
end

Class Method Details

.escape(cmd) ⇒ String

Escapes a shell command.

Parameters:

Returns:

  • (String)


11
12
13
14
15
16
17
# File 'lib/loom/shell/cmd_wrapper.rb', line 11

def escape(cmd)
  if cmd.is_a? CmdWrapper
    cmd.escape_cmd
  else
    Shellwords.escape(cmd)
  end
end

.wrap_cmd(*cmd_parts, should_quote: false) ⇒ Object

Wraps a command in another command. See ‘CmdWrapper.new’

Parameters:

  • cmd_parts (CmdWrapper|String|Symbol)
  • should_quote (Boolean) (defaults to: false)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/loom/shell/cmd_wrapper.rb', line 22

def wrap_cmd(*cmd_parts, should_quote: false)
  cmd_parts = cmd_parts.map do |parts|
    if parts.respond_to? :cmd_parts
      parts.cmd_parts
    else
      parts
    end
  end
  CmdWrapper.new *cmd_parts.flatten, **{
    :should_quote => should_quote,
    :is_wrapped => true
  }
end

Instance Method Details

#escape_cmdString Also known as: to_s

Shell escapes each part of ‘@cmd_parts` and joins them with spaces.

Returns:

  • (String)


56
57
58
59
60
61
# File 'lib/loom/shell/cmd_wrapper.rb', line 56

def escape_cmd
  escaped_cmd = escape_inner

  cmd_with_redirects = [escaped_cmd].concat @redirects.map(&:to_s)
  cmd_with_redirects.join " "
end

#wrap(*wrapped_cmd) ⇒ Array<#to_s>

Returns The ‘wrapped_cmd` wrapped by `#escape_cmd`.

Parameters:

  • wrapped_cmd (String)

Returns:

  • (Array<#to_s>)

    The ‘wrapped_cmd` wrapped by `#escape_cmd`



66
67
68
69
70
# File 'lib/loom/shell/cmd_wrapper.rb', line 66

def wrap(*wrapped_cmd)
  wrapped_cmd =
    CmdWrapper.wrap_cmd(*wrapped_cmd, should_quote: @should_quote)
  CmdWrapper.new(self, wrapped_cmd)
end