Module: Licensed::Shell

Defined in:
lib/licensed/shell.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.execute(cmd, *args, allow_failure: false, env: {}) ⇒ Object

Executes a command, returning its standard output on success. On failure it raises an exception that contains the error output, unless ‘allow_failure` is true.



9
10
11
12
13
14
15
16
17
18
# File 'lib/licensed/shell.rb', line 9

def self.execute(cmd, *args, allow_failure: false, env: {})
  stdout, stderr, status = Open3.capture3(env, cmd, *args)

  if !status.success? && !allow_failure
    raise Error.new([cmd, *args], status.exitstatus, encode_content(stderr))
  end

  # ensure that returned data is properly encoded
  encode_content(stdout.strip)
end

.success?(cmd, *args) ⇒ Boolean

Executes a command and returns a boolean value indicating if the command was succesful

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/licensed/shell.rb', line 22

def self.success?(cmd, *args)
  _, _, status = Open3.capture3(cmd, *args)
  status.success?
end

.tool_available?(tool) ⇒ Boolean

Returns a boolean indicating whether a CLI tool is available in the current environment

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/licensed/shell.rb', line 29

def self.tool_available?(tool)
  output, err, status = Open3.capture3("which", tool)
  status.success? && !output.strip.empty? && err.strip.empty?
end