Class: Cuprum::Cli::Dependencies::SystemCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/cli/dependencies/system_command.rb

Overview

Utility wrapper for running system command and capturing output.

Direct Known Subclasses

Mock

Defined Under Namespace

Classes: CapturedOutput, Mock

Instance Method Summary collapse

Instance Method Details

#capture(command, .arguments: [], environment: {}, options: {}) ⇒ Cuprum::Result<Cuprum::Cli::Dependencies::SystemCommand::CapturedOutput>

Executes the system command and returns the captured output.

Parameters:

  • command (String)

    the command to run.

  • arguments (Array<String>)

    command-line flags or arguments to pass to the command.

  • enviroment (Hash<String, Object>)

    environment variables to set for the command.

  • options (Hash<String, Object>) (defaults to: {})

    command line options and values to pass to the command.

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cuprum/cli/dependencies/system_command.rb', line 35

def capture(command, **)
  value = capture_command(command, **)

  return Cuprum::Result.new(value:) if value.success?

  error = Cuprum::Cli::Errors::SystemCommandFailure.new(
    command:,
    details:     value.error,
    exit_status: value.status.exitstatus
  )

  Cuprum::Result.new(error:, value:)
end

#spawn(command, arguments: [], enviroment:, options: {}) ⇒ Cuprum::Result<nil>

Spawns a process to run the system command.

Parameters:

  • command (String)

    the command to run.

  • arguments (Array<String>) (defaults to: [])

    command-line flags or arguments to pass to the command.

  • enviroment (Hash<String, Object>)

    environment variables to set for the command.

  • options (Hash<String, Object>) (defaults to: {})

    command line options and values to pass to the command.

Returns:

  • (Cuprum::Result<nil>)

    a result with status :success if the process ran successfully; otherwise, a result with status :failure and an Error.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cuprum/cli/dependencies/system_command.rb', line 63

def spawn(command, **)
  status = spawn_command(command, **)

  return Cuprum::Result.new if status.success?

  error = Cuprum::Cli::Errors::SystemCommandFailure.new(
    command:,
    exit_status: status.exitstatus
  )

  Cuprum::Result.new(error:)
end