Module: PoiseLanguages::Utils

Extended by:
Utils
Includes:
Which
Included in:
Utils
Defined in:
lib/poise_languages/utils.rb,
lib/poise_languages/utils/which.rb

Defined Under Namespace

Modules: Which

Constant Summary collapse

SHELLJOIN_WHITELIST =

Default whitelist for #shelljoin.

[/^2?[><]/]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Which

#which

Class Method Details

.absolute_command(cmd, path: nil) ⇒ String+

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

Parameters:

  • cmd (String, Array<String>)

    Command to fix up.

  • path (String, nil) (defaults to: nil)

    Replacement $PATH for executable lookup.

Returns:

  • (String, Array<String>)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ String

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

Parameters:

  • cmd (Array<String>)

    Command array to join.

  • whitelist (Array<Regexp>) (defaults to: SHELLJOIN_WHITELIST)

    Array of patterns to whitelist.

Returns:

  • (String)


36
37
38
39
40
41
42
43
44
# 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

Instance Method Details

#absolute_command(cmd, path: nil) ⇒ String+

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

Parameters:

  • cmd (String, Array<String>)

    Command to fix up.

  • path (String, nil) (defaults to: nil)

    Replacement $PATH for executable lookup.

Returns:

  • (String, Array<String>)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ String

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

Parameters:

  • cmd (Array<String>)

    Command array to join.

  • whitelist (Array<Regexp>) (defaults to: SHELLJOIN_WHITELIST)

    Array of patterns to whitelist.

Returns:

  • (String)


36
37
38
39
40
41
42
43
44
# 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