Module: RFacter::DSL::Facter::Core::Execution

Defined in:
lib/rfacter/dsl.rb

Overview

TODO:

Implement execution options

Shims for Facter::Core::Exection methods

Since:

  • 0.1.0

Defined Under Namespace

Classes: ExecutionFailure

Class Method Summary collapse

Class Method Details

.exec(command) ⇒ String

Deprecated.

Use #execute instead

Try to execute a command and return the output.

Parameters:

  • command (String)

    the program to run

Returns:

  • (String)

    the output of the program, or nil if the command does not exist or could not be executed.

Since:

  • 0.1.0



255
256
257
# File 'lib/rfacter/dsl.rb', line 255

def self.exec(command)
  execute(command, on_fail: nil)
end

.execute(command, on_fail: :raise, **options) ⇒ String, Object

Execute a command and return the output of that program.

Parameters:

  • command (String)

    the program to run

  • options (Hash)

Options Hash (**options):

  • :on_fail (Object)

    How to behave when the command could not be run. Specifying ‘:raise` will raise an error, anything else will return that object on failure. Default is `:raise`.

Returns:

  • (String)

    The stdout of the program.

  • (Object)

    The value of ‘:on_fail` if command execution failed and `:on_fail` was specified.

Raises:

  • (RFacter::Util::DSL::Facter::Core::Execution::ExecutionFailure)

    If the command does not exist or could not be executed.

Since:

  • 0.1.0



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/rfacter/dsl.rb', line 275

def self.execute(command, on_fail: :raise, **options)
  begin
    output = NODE.value.execute(command).stdout.chomp
  rescue => detail
    if on_fail == :raise
      raise ::RFacter::DSL::Facter::Core::Execution::ExecutionFailure.new,
        "Failed while executing '#{command}': #{detail.message}"
    else
      return on_fail
    end
  end

  output
end

.which(bin) ⇒ String?

Determines the full path to a binary.

Returns nil if no matching executable can be found otherwise returns the expanded pathname.

Parameters:

  • bin (String)

    the executable to locate

Returns:

  • (String, nil)

    the full path to the executable or nil if not found

Since:

  • 0.1.0



299
300
301
# File 'lib/rfacter/dsl.rb', line 299

def self.which(bin)
  NODE.value.which(bin)
end