module PoiseLanguages::Utils

Constants

SHELLJOIN_WHITELIST

Default whitelist for {#shelljoin}.

Public Instance Methods

absolute_command(cmd, path: nil) click to toggle source

Convert the executable in a string or array command to an absolute path.

@param cmd [String, Array<String>] Command to fix up. @param path [String, nil] Replacement $PATH for executable lookup. @return [String, Array<String>]

# File lib/poise_languages/utils.rb, line 51
def absolute_command(cmd, path: nil)
  was_array = cmd.is_a?(Array)
  cmd = if was_array
    cmd.dup
  else
    Shellwords.split(cmd)
  end
  # Don't try to touch anything if the first value looks like a flag or a path.
  if cmd.first && !cmd.first.start_with?('-') && !cmd.first.include?(::File::SEPARATOR)
    # If which returns false, just leave it I guess.
    cmd[0] = which(cmd.first, path: path) || cmd.first
  end
  cmd = shelljoin(cmd) unless was_array
  cmd
end
shelljoin(cmd, whitelist: SHELLJOIN_WHITELIST) click to toggle source

An improved version of Shellwords.shelljoin that doesn't escape a few things.

@param cmd [Array<String>] Command array to join. @param whitelist [Array<Regexp>] Array of patterns to whitelist. @return [String]

# File lib/poise_languages/utils.rb, line 36
def shelljoin(cmd, whitelist: SHELLJOIN_WHITELIST)
  cmd.map do |str|
    if whitelist.any? {|pat| str =~ pat }
      str
    else
      Shellwords.shellescape(str)
    end
  end.join(' ')
end