Module: Squared::Common::Shell
- Defined in:
- lib/squared/common/shell.rb
Instance Method Summary collapse
- #double_quote(val) ⇒ Object
- #fill_option(val) ⇒ Object
- #sanitize_args(*opts) ⇒ Object
- #shell_escape(val, quote: false) ⇒ Object
- #shell_quote(val, force: true) ⇒ Object
- #single_quote(val) ⇒ Object
- #split_escape(val, char: ',') ⇒ Object
- #trailing_slash(val) ⇒ Object
Instance Method Details
#double_quote(val) ⇒ Object
31 32 33 |
# File 'lib/squared/common/shell.rb', line 31 def double_quote(val) val.gsub(/(?<!\\)"/, '\\"') end |
#fill_option(val) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/squared/common/shell.rb', line 20 def fill_option(val) return "-#{val}" if val.size == 1 || val.match?(/^[a-z]\d+$/i) val = "--#{val}" unless val.start_with?('-') shell_escape(val).sub('\\=', '=') end |
#sanitize_args(*opts) ⇒ Object
43 44 45 |
# File 'lib/squared/common/shell.rb', line 43 def sanitize_args(*opts) opts.map { |val| val.include?(' ') ? shell_quote(val) : shell_escape(val) }.join(' ') end |
#shell_escape(val, quote: false) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/squared/common/shell.rb', line 6 def shell_escape(val, quote: false) return (quote ? shell_quote(val, force: false) : val) if ::Rake::Win32.windows? require 'shellwords' Shellwords.escape(val) end |
#shell_quote(val, force: true) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/squared/common/shell.rb', line 13 def shell_quote(val, force: true) ret = val.to_s.strip return ret if (!force && !ret.include?(' ')) || ret.match?(/(?:^|=)(["']).+\1$/m) ::Rake::Win32.windows? ? "\"#{double_quote(ret)}\"" : "'#{single_quote(ret)}'" end |
#single_quote(val) ⇒ Object
27 28 29 |
# File 'lib/squared/common/shell.rb', line 27 def single_quote(val) val.gsub("'", "'\\\\''") end |
#split_escape(val, char: ',') ⇒ Object
35 36 37 |
# File 'lib/squared/common/shell.rb', line 35 def split_escape(val, char: ',') val.split(/\s*(?<!\\)#{char}\s*/) end |
#trailing_slash(val) ⇒ Object
39 40 41 |
# File 'lib/squared/common/shell.rb', line 39 def trailing_slash(val) val.to_s.chomp(::File::SEPARATOR) + ::File::SEPARATOR end |