Module: Sevgi::Function::Shell

Included in:
Sevgi::Function
Defined in:
lib/sevgi/function/shell.rb

Overview

Shell execution helpers and executable lookup utilities.

Defined Under Namespace

Classes: Result, Runner

Instance Method Summary collapse

Instance Method Details

#executable!(*args) ⇒ nil

Requires the first command argument to name an executable program.

Parameters:

  • args (Array<Object>)

    command arguments

Returns:

  • (nil)

Raises:

  • (Sevgi::Error)

    when the program cannot be found in PATH



27
28
29
30
# File 'lib/sevgi/function/shell.rb', line 27

def executable!(*args)
  program = args.first.to_s.split.first
  Error.("Missing executable: #{program}") unless executable?(program)
end

#executable?(program) ⇒ Boolean

Checks whether a program exists in PATH.

Parameters:

  • program (Object)

    program name

Returns:

  • (Boolean)

    true when an executable with this name is found



12
13
14
15
16
17
18
19
20
21
# File 'lib/sevgi/function/shell.rb', line 12

def executable?(program)
  program = program.to_s
  return false if program.empty?

  executable_cache.fetch(program) do
    executable_cache[program] = ENV.fetch("PATH", "").split(::File::PATH_SEPARATOR).any? do |dir|
      ::File.executable?(::File.join(dir, program))
    end
  end
end

#sh(*args) { ... } ⇒ Sevgi::Function::Shell::Result

Runs a command and captures stdout, stderr, and exit status.

Parameters:

  • args (Array<String>)

    command and arguments

Yields:

  • optional content writer for stdin

Yield Returns:

Returns:

Raises:

  • (Errno::ENOENT)

    when the executable cannot be spawned



142
# File 'lib/sevgi/function/shell.rb', line 142

def sh(...) = Runner.new.(...)

#sh!(*args) { ... } ⇒ Sevgi::Function::Shell::Result

Runs a command, requiring both executable lookup and successful exit status.

Parameters:

  • args (Array<String>)

    command and arguments

Yields:

  • optional content writer for stdin

Yield Returns:

Returns:

Raises:

  • (Sevgi::Error)

    when the executable is missing or the command fails

  • (Errno::ENOENT)

    when the executable cannot be spawned



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sevgi/function/shell.rb', line 151

def sh!(*args, &block)
  executable!(*args) unless args.empty?

  sh(*args, &block).tap do |result|
    unless result.ok?
      warn(result.err)
      warn("")

      Error.("Command failed: #{result.cmd}")
    end
  end
end