class Loom::Shell::CmdWrapper
Attributes
Public Class Methods
Escapes a shell command. @param cmd [CmdWrapper|String] @return [String]
# 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
@param cmd [Array<>] Command parts that will be shell escaped. @param :should_quote [Boolean] Whether wrapped commands should be quoted. @param :redirc [Array<CmdRedirect>] STDIO redirection for the command in quotes.
# File lib/loom/shell/cmd_wrapper.rb, line 41 def initialize(*cmd, should_quote: false, is_wrapped: false, redirect: []) @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
Wraps a command in another command. See `CmdWrapper.new' @param cmd_parts
[CmdWrapper|String|Symbol] @param should_quote [Boolean]
# 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
Public Instance Methods
Shell
escapes each part of `@cmd_parts` and joins them with spaces. @return [String]
# File lib/loom/shell/cmd_wrapper.rb, line 53 def escape_cmd escaped_cmd = escape_inner cmd_with_redirects = [escaped_cmd].concat @redirects.map(&:to_s) cmd_with_redirects.join " " end
@param wrapped_cmd [String] @return [Array<#to_s>] The `wrapped_cmd` wrapped by `#escape_cmd`
# File lib/loom/shell/cmd_wrapper.rb, line 63 def wrap(*wrapped_cmd) wrapped_cmd = CmdWrapper.wrap_cmd(*wrapped_cmd, should_quote: @should_quote) CmdWrapper.new(self, wrapped_cmd) end
Private Instance Methods
# File lib/loom/shell/cmd_wrapper.rb, line 70 def escape_inner escaped_parts = escape_parts(@cmd_parts) # Don't fuck with this unless you really want to fix it. if @should_quote && @is_wrapped double_escaped = escape_parts(escaped_parts).join " " # Shellwords escapes spaces, but I'm wrapping this string in another set # of quotes here, so it's unnecessary. double_escaped.gsub!(/\\(\s)/, "\\1") while double_escaped.match(/\\\s/) "\"#{double_escaped}\"" else escaped_parts.join " " end end
Maps each entry of #{cmd_parts} to the escaped form of itself, except if the part is frozen (like a Symbol) @param cmd_parts
[Array<String|Symbol|CmdWrapper>] @return [Array<String|Symbol>]
# File lib/loom/shell/cmd_wrapper.rb, line 91 def escape_parts(cmd_parts) cmd_parts.map do |part| part.cmd_parts rescue part end.flatten cmd_parts.map do |part| unless part.frozen? CmdWrapper.escape part else part end end end