Class: Makit::Commands::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/commands/request.rb

Overview

Enhanced request handling with validation and type safety. Uses plain Ruby objects - no protobuf dependency for better performance.

Examples:

Create a simple request

request = Request.new(command: "git", arguments: ["--version"])

Create request from string

request = Request.from_string("git clone https://github.com/user/repo.git")

Create request with full options

request = Request.new(
  command: "bundle",
  arguments: ["install"],
  directory: "/path/to/project",
  environment: { "BUNDLE_JOBS" => "4" },
  timeout: 300,
  metadata: { operation: "dependency_install" }
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, arguments: [], **options) ⇒ Request

Initialize a new command request.

Parameters:

  • command (String)

    the command to execute

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

    command arguments

  • environment (Hash<String,String>)

    environment variables

  • directory (String)

    working directory

  • timeout (Integer)

    timeout in seconds

  • metadata (Hash)

    additional metadata

Raises:

  • (ArgumentError)

    if command is invalid



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/makit/commands/request.rb', line 53

def initialize(command:, arguments: [], **options)
  @command = validate_command(command)
  @arguments = validate_arguments(arguments)
  @environment = options[:environment] || {}
  @directory = options[:directory] || Dir.pwd
  @timeout = options[:timeout] || Makit::Configuration::Timeout.global_default
  @metadata = options[:metadata] || {}
  @exit_on_failure = options[:exit_on_failure] || true
  @show_stderr = options[:show_stderr] || true
  @show_stdout = options[:show_stdout] || false
  validate_directory(@directory)
  validate_timeout(@timeout)
end

Instance Attribute Details

#argumentsArray<String> (readonly)

Returns command line arguments.

Returns:

  • (Array<String>)

    command line arguments



41
# File 'lib/makit/commands/request.rb', line 41

attr_reader :command, :arguments, :environment, :directory, :timeout, :metadata

#commandString (readonly)

Returns the command to execute.

Returns:

  • (String)

    the command to execute



41
42
43
# File 'lib/makit/commands/request.rb', line 41

def command
  @command
end

#directoryString (readonly)

Returns working directory for execution.

Returns:

  • (String)

    working directory for execution



41
# File 'lib/makit/commands/request.rb', line 41

attr_reader :command, :arguments, :environment, :directory, :timeout, :metadata

#environmentHash<String,String> (readonly)

Returns environment variables.

Returns:



41
# File 'lib/makit/commands/request.rb', line 41

attr_reader :command, :arguments, :environment, :directory, :timeout, :metadata

#exit_on_failure(exit_on_failure) ⇒ Object



190
191
192
193
# File 'lib/makit/commands/request.rb', line 190

def exit_on_failure(exit_on_failure)
  @exit_on_failure = exit_on_failure
  self
end

#metadataObject (readonly)

Command execution request with validation and metadata.



41
# File 'lib/makit/commands/request.rb', line 41

attr_reader :command, :arguments, :environment, :directory, :timeout, :metadata

#show_stderr(show_stderr) ⇒ Object



199
200
201
202
# File 'lib/makit/commands/request.rb', line 199

def show_stderr(show_stderr)
  @show_stderr = show_stderr
  self
end

#show_stdout(show_stdout) ⇒ Object



208
209
210
211
# File 'lib/makit/commands/request.rb', line 208

def show_stdout(show_stdout)
  @show_stdout = show_stdout
  self
end

#timeoutInteger (readonly)

Returns timeout in seconds.

Returns:

  • (Integer)

    timeout in seconds



41
# File 'lib/makit/commands/request.rb', line 41

attr_reader :command, :arguments, :environment, :directory, :timeout, :metadata

Class Method Details

.from_hash(command_hash) ⇒ Request

Create a request from hash representation.

Examples:

Request.from_hash({
  "command" => "git",
  "arguments" => ["clone", "https://github.com/user/repo.git"],
  "timeout" => 300
})

Parameters:

  • command_hash (Hash)

    hash containing command information

Returns:

Raises:

  • (ArgumentError)

    if hash is invalid



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/makit/commands/request.rb', line 135

def self.from_hash(command_hash)
  raise ArgumentError, "Command hash cannot be nil" if command_hash.nil?

  # Convert string keys to symbols for consistency
  hash = command_hash.is_a?(Hash) ? normalize_hash_keys(command_hash) : command_hash

  new(
    command: hash[:command] || hash["command"],
    arguments: hash[:arguments] || hash["arguments"] || [],
    environment: hash[:environment] || hash["environment"] || {},
    directory: hash[:directory] || hash["directory"],
    timeout: hash[:timeout] || hash["timeout"],
    metadata: hash[:metadata] || hash["metadata"] || {},
    exit_on_failure: hash[:exit_on_failure] || hash["exit_on_failure"],
    show_stderr: hash[:show_stderr] || hash["show_stderr"],
    show_stdout: hash[:show_stdout] || hash["show_stdout"],
  )
end

.from_json(json_string) ⇒ Request

Create a request from JSON string.

Parameters:

  • json_string (String)

    JSON string representation

Returns:

Raises:

  • (JSON::ParserError, ArgumentError)

    if JSON is invalid



159
160
161
162
# File 'lib/makit/commands/request.rb', line 159

def self.from_json(json_string)
  hash = JSON.parse(json_string)
  from_hash(hash)
end

.from_string(command_string, **options) ⇒ Request

Create a request from a shell command string.

Examples:

Request.from_string("git clone https://github.com/user/repo.git")
Request.from_string("bundle install --jobs 4")

Parameters:

  • command_string (String)

    shell command string to parse

  • options (Hash)

    additional options

Returns:

Raises:

  • (ArgumentError)

    if command string is invalid



113
114
115
116
117
118
119
120
121
122
# File 'lib/makit/commands/request.rb', line 113

def self.from_string(command_string, **options)
  raise ArgumentError, "Command string cannot be empty" if command_string.nil? || command_string.strip.empty?

  # Parse shell command string into command and arguments
  parts = Shellwords.split(command_string.strip)
  command = parts.shift
  arguments = parts

  new(command: command, arguments: arguments, **options)
end

Instance Method Details

#cache_keyString

Generate a cache key for this request.

Returns:

  • (String)

    cache key based on command, arguments, and context



180
181
182
183
184
# File 'lib/makit/commands/request.rb', line 180

def cache_key
  require "digest"
  content = "#{command}:#{arguments.join(":")}:#{directory}"
  Digest::SHA256.hexdigest(content)[0..16]
end

#equivalent_to?(other) ⇒ Boolean

Check if this request represents the same command.

Parameters:

  • other (Request)

    another request to compare

Returns:

  • (Boolean)

    true if commands are equivalent



168
169
170
171
172
173
174
175
# File 'lib/makit/commands/request.rb', line 168

def equivalent_to?(other)
  return false unless other.is_a?(Request)

  command == other.command &&
    arguments == other.arguments &&
    environment == other.environment &&
    directory == other.directory
end

#exit_on_failure?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/makit/commands/request.rb', line 186

def exit_on_failure?
  @exit_on_failure
end

#runResult

Execute the command.

Returns:

  • (Result)

    execution result

Raises:

  • (ArgumentError)

    if runner is not set



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/makit/commands/request.rb', line 217

def run
  # self.exit_on_failure = exit_on_failure
  # self.show_stderr = true
  result = Makit::Commands::Runner.default.execute(show_stderr(true))

  # Display stderr if the command failed and show_stderr is enabled
  puts result.stderr if result.failure? && show_stderr? && !result.stderr.empty?

  # Exit with the command's exit code if it failed and exit_on_failure is enabled
  exit result.exit_code if result.failure? && exit_on_failure?

  result
end

#show_stderr?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/makit/commands/request.rb', line 195

def show_stderr?
  @show_stderr
end

#show_stdout?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/makit/commands/request.rb', line 204

def show_stdout?
  @show_stdout
end

#to_hHash

Convert request to hash representation.

Returns:

  • (Hash)

    hash representation of the request



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/makit/commands/request.rb', line 78

def to_h
  {
    command: command,
    arguments: arguments,
    environment: environment,
    directory: directory,
    timeout: timeout,
    metadata: ,
    exit_on_failure: exit_on_failure?,
    show_stderr: show_stderr?,
    show_stdout: show_stdout?,
  }
end

#to_json(*args) ⇒ String

Convert request to JSON representation.

Parameters:

  • args (Array)

    arguments passed to JSON.generate

Returns:

  • (String)

    JSON representation



96
97
98
# File 'lib/makit/commands/request.rb', line 96

def to_json(*args)
  JSON.generate(to_h, *args)
end

#to_json_pretty(*args) ⇒ Object



100
101
102
# File 'lib/makit/commands/request.rb', line 100

def to_json_pretty(*args)
  JSON.pretty_generate(to_h, *args)
end

#to_shell_commandString

Convert request to shell-executable command string.

Returns:

  • (String)

    shell command string



70
71
72
73
# File 'lib/makit/commands/request.rb', line 70

def to_shell_command
  cmd_parts = [command] + arguments
  Shellwords.join(cmd_parts)
end

#tryObject



231
232
233
# File 'lib/makit/commands/request.rb', line 231

def try
  Makit::Commands::Runner.default.execute(exit_on_failure(false))
end