Class: TransformedShellCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/transformed_shell_command.rb

Overview

TransformedShellCommand executes a shell command and provides access to a transformed version of the output.

The class accepts:

  • The same arguments as ExecutedShellCommand (command, chdir, env)

  • A regex pattern with named capture groups (supports multi-line output)

  • A format that can be either:

    • A Symbol: calls that method on the output (e.g., :strip, :upcase)

    • A String: format string with named placeholders like ‘%group_name’

The command is executed automatically during initialization, and the entire output (including multi-line output) is transformed in a single transformation operation using the format.

The regex pattern can match across multiple lines. Use the multiline flag (m) or construct patterns that handle newlines appropriately.

Basic usage with format string:

regex = /(?<name>\w+):(?<value>\d+)/
format_str = "Name: %{name}, Value: %{value}"
cmd = TransformedShellCommand.new("echo 'user:123'", regex: regex, format: format_str)
cmd.transformed_output  # => "Name: user, Value: 123"
cmd.result              # => original ExecutedShellCommand::Result

Basic usage with Symbol:

cmd = TransformedShellCommand.new("echo '  hello  '", regex: /.*/, format: :strip)
cmd.transformed_output  # => "hello"

Multi-line output example:

regex = /(?<first>\w+)\n(?<second>\w+)\n(?<third>\w+)/m
format_str = "%{first}-%{second}-%{third}"
cmd = TransformedShellCommand.new("echo -e 'one\ntwo\nthree'", regex: regex, format: format_str)
cmd.transformed_output  # => "one-two-three"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, regex:, format:, chdir: nil, env: {}) ⇒ TransformedShellCommand

Returns a new instance of TransformedShellCommand.



49
50
51
52
53
54
55
56
57
58
# File 'lib/transformed_shell_command.rb', line 49

def initialize(command, regex:, format:, chdir: nil, env: {})
  @command = command
  @chdir = chdir
  @env = env
  @regex = regex && (regex.is_a?(Regexp) ? regex : Regexp.new(regex))
  @format = format
  @result = nil
  @transformed_output = nil
  execute_and_transform
end

Instance Attribute Details

#chdirObject (readonly)

Returns the value of attribute chdir.



47
48
49
# File 'lib/transformed_shell_command.rb', line 47

def chdir
  @chdir
end

#commandObject (readonly)

Returns the value of attribute command.



47
48
49
# File 'lib/transformed_shell_command.rb', line 47

def command
  @command
end

#envObject (readonly)

Returns the value of attribute env.



47
48
49
# File 'lib/transformed_shell_command.rb', line 47

def env
  @env
end

#formatObject (readonly)

Returns the value of attribute format.



47
48
49
# File 'lib/transformed_shell_command.rb', line 47

def format
  @format
end

#regexObject (readonly)

Returns the value of attribute regex.



47
48
49
# File 'lib/transformed_shell_command.rb', line 47

def regex
  @regex
end

#transformed_outputObject (readonly)

Returns the transformed output string. The transformation is performed once during initialization and memoized.



64
65
66
# File 'lib/transformed_shell_command.rb', line 64

def transformed_output
  @transformed_output
end

Instance Method Details

#durationObject



95
96
97
# File 'lib/transformed_shell_command.rb', line 95

def duration
  result.duration
end

#exit_codeObject



83
84
85
# File 'lib/transformed_shell_command.rb', line 83

def exit_code
  result.exit_code
end

#failure?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/transformed_shell_command.rb', line 91

def failure?
  !result.success?
end

#finished_atObject



103
104
105
# File 'lib/transformed_shell_command.rb', line 103

def finished_at
  result.finished_at
end

#pidObject



107
108
109
# File 'lib/transformed_shell_command.rb', line 107

def pid
  result.pid
end

#resultObject

Returns the original ExecutedShellCommand result.



69
70
71
# File 'lib/transformed_shell_command.rb', line 69

def result
  @executed_command.result
end

#started_atObject



99
100
101
# File 'lib/transformed_shell_command.rb', line 99

def started_at
  result.started_at
end

#stderrObject



79
80
81
# File 'lib/transformed_shell_command.rb', line 79

def stderr
  result.stderr
end

#stdoutObject

Convenience delegators to the original result:



75
76
77
# File 'lib/transformed_shell_command.rb', line 75

def stdout
  result.stdout
end

#success?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/transformed_shell_command.rb', line 87

def success?
  result.success?
end