Module: Facter::Core::Execution

Included in:
Util::Resolution
Defined in:
lib/facter/custom_facts/core/execution.rb,
lib/facter/custom_facts/core/execution/base.rb,
lib/facter/custom_facts/core/execution/posix.rb,
lib/facter/custom_facts/core/execution/popen3.rb,
lib/facter/custom_facts/core/execution/windows.rb

Overview

Since:

  • 2.0.0

Defined Under Namespace

Classes: Base, ExecutionFailure, Popen3, Posix, Windows

Constant Summary collapse

@@impl =

rubocop:disable Style/ClassVars

Since:

  • 2.0.0

if LegacyFacter::Util::Config.windows? # rubocop:disable Style/ClassVars
  Facter::Core::Execution::Windows.new
else
  Facter::Core::Execution::Posix.new
end

Class Method Summary collapse

Class Method Details

.absolute_path?(path, platform = nil) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • platform (:posix/:windows/nil) (defaults to: nil)

    The platform logic to use

Returns:

  • (Boolean)

Since:

  • 2.0.0



51
52
53
54
55
56
57
58
59
60
# File 'lib/facter/custom_facts/core/execution.rb', line 51

def absolute_path?(path, platform = nil)
  case platform
  when :posix
    Facter::Core::Execution::Posix.new.absolute_path?(path)
  when :windows
    Facter::Core::Execution::Windows.new.absolute_path?(path)
  else
    @@impl.absolute_path?(path)
  end
end

.exec(command) ⇒ String/nil

Deprecated.

Use #execute instead

Try to execute a command and return the output.

Parameters:

  • command (String)

    Command to run

Returns:

  • (String/nil)

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

Since:

  • 2.0.0



99
100
101
# File 'lib/facter/custom_facts/core/execution.rb', line 99

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

.execute(command, options = {}) ⇒ String

Execute a command and return the output of that program.

Parameters:

  • command (String)

    Command to run

  • options (Hash) (defaults to: {})

    Hash with options for the command

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.

  • :logger (Object)

    Optional logger used to log the command’s stderr.

  • :timeout (Object)

    Optional time out for the specified command. If no timeout is passed, a default of 300 seconds is used.

Returns:

  • (String)

    the output of the program, or the value of :on_fail (if it’s different than :raise) if command execution failed and :on_fail was specified.

Raises:

Since:

  • 2.0.0



124
125
126
# File 'lib/facter/custom_facts/core/execution.rb', line 124

def execute(command, options = {})
  @@impl.execute(command, options)
end

.execute_command(command, on_fail = nil, logger = nil, timeout = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute a command and return the stdout and stderr of that program.

Parameters:

  • command (String)

    Command to run

  • on_fail (Object) (defaults to: nil)

    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 to not raise.

  • logger (defaults to: nil)

    Optional logger used to log the command’s stderr.

  • timeout (defaults to: nil)

    Optional time out for the specified command. If no timeout is passed, a default of 300 seconds is used.

Returns:

  • (String, String)

    the stdout and stderr of the program, or the value of :on_fail if command execution failed and :on_fail was specified.

Raises:

Since:

  • 2.0.0



146
147
148
# File 'lib/facter/custom_facts/core/execution.rb', line 146

def execute_command(command, on_fail = nil, logger = nil, timeout = nil)
  @@impl.execute_command(command, on_fail, logger, timeout)
end

.expand_command(command) ⇒ String/nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Given a command line, this returns the command line with the executable written as an absolute path. If the executable contains spaces, it has to be put in double quotes to be properly recognized.

Parameters:

  • command (String)

    the command line

Returns:

  • (String/nil)

    The command line with the executable’s path expanded, or nil if the executable cannot be found.

Since:

  • 2.0.0



72
73
74
# File 'lib/facter/custom_facts/core/execution.rb', line 72

def expand_command(command)
  @@impl.expand_command(command)
end

.implObject

Since:

  • 2.0.0



12
13
14
# File 'lib/facter/custom_facts/core/execution.rb', line 12

def self.impl
  @@impl
end

.search_pathsArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the locations to be searched when looking for a binary. This is currently determined by the PATH environment variable plus ‘/sbin` and `/usr/sbin` when run on unix

Returns:

  • (Array<String>)

    The paths to be searched for binaries

Since:

  • 2.0.0



25
26
27
# File 'lib/facter/custom_facts/core/execution.rb', line 25

def search_paths
  @@impl.search_paths
end

.which(bin) ⇒ String/nil

Determines the full path to a binary. If the supplied filename does not already describe an absolute path then different locations (determined by search_paths) will be searched for a match.

Parameters:

  • bin (String)

    The executable to locate

Returns:

  • (String/nil)

    The full path to the executable or nil if not found

Since:

  • 2.0.0



39
40
41
# File 'lib/facter/custom_facts/core/execution.rb', line 39

def which(bin)
  @@impl.which(bin)
end

.with_env(values, &block) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Overrides environment variables within a block of code. The specified values will be set for the duration of the block, after which the original values (if any) will be restored.

Parameters:

  • values (Hash<String=>String>)

    A hash of the environment variables to override

Returns:

  • (String)

    The block’s return string

Since:

  • 2.0.0



86
87
88
# File 'lib/facter/custom_facts/core/execution.rb', line 86

def with_env(values, &block)
  @@impl.with_env(values, &block)
end