Module: Facter::Core::Execution

Included in:
Util::Resolution
Defined in:
lib/facter/core/execution.rb

Defined Under Namespace

Classes: Base, ExecutionFailure, Posix, Windows

Constant Summary collapse

@@impl =
if Facter::Util::Config.is_windows?
  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

Determine in a platform-specific way whether a path is absolute. This defaults to the local platform if none is specified.

Parameters:

  • path (String)

    the path to check

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

    the platform logic to use

Returns:

  • (Boolean)


54
55
56
# File 'lib/facter/core/execution.rb', line 54

def absolute_path?(path, platform = nil)
  @@impl.absolute_path?(path, platform)
end

.exec(command) ⇒ String

Deprecated.

Use #execute instead

Try to execute a command and return the output.

Parameters:

  • code (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.



95
96
97
# File 'lib/facter/core/execution.rb', line 95

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:

  • code (String)

    the program to run

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

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 output of the program, or the value of :on_fail if command execution failed and :on_fail was specified.

Raises:

Since:

  • 2.0.1



116
117
118
# File 'lib/facter/core/execution.rb', line 116

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

.expand_command(command) ⇒ String?

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

expanded, or nil if the executable cannot be found.

Parameters:

  • command (String)

    the command line

Returns:

  • (String, nil)

    the command line with the executable’s path



66
67
68
# File 'lib/facter/core/execution.rb', line 66

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

.implObject



17
18
19
# File 'lib/facter/core/execution.rb', line 17

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



29
30
31
# File 'lib/facter/core/execution.rb', line 29

def search_paths
  @@impl.search_paths
end

.which(bin) ⇒ String?

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.

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



45
46
47
# File 'lib/facter/core/execution.rb', line 45

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

.with_env(values, { || ... }) ⇒ void

This method returns an undefined value.

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



82
83
84
# File 'lib/facter/core/execution.rb', line 82

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