Class: Loom::Shell::CmdWrapper
- Inherits:
-
Object
- Object
- Loom::Shell::CmdWrapper
- Defined in:
- lib/loom/shell/cmd_wrapper.rb
Instance Attribute Summary collapse
-
#cmd_parts ⇒ Object
readonly
Returns the value of attribute cmd_parts.
Class Method Summary collapse
-
.escape(cmd) ⇒ String
Escapes a shell command.
-
.wrap_cmd(*cmd_parts, should_quote: false) ⇒ Object
Wraps a command in another command.
Instance Method Summary collapse
-
#escape_cmd ⇒ String
(also: #to_s)
Shell escapes each part of ‘@cmd_parts` and joins them with spaces.
-
#initialize(*cmd, should_quote: false, is_wrapped: false, redirect: []) ⇒ CmdWrapper
constructor
in quotes.
-
#wrap(*wrapped_cmd) ⇒ Array<#to_s>
The ‘wrapped_cmd` wrapped by `#escape_cmd`.
Constructor Details
#initialize(*cmd, should_quote: false, is_wrapped: false, redirect: []) ⇒ CmdWrapper
in quotes.
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_parts ⇒ Object (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.
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’
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_cmd ⇒ String Also known as: to_s
Shell escapes each part of ‘@cmd_parts` and joins them with spaces.
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`.
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 |