Class: Makit::Commands::Request
- Inherits:
-
Object
- Object
- Makit::Commands::Request
- 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.
Instance Attribute Summary collapse
-
#arguments ⇒ Array<String>
readonly
Command line arguments.
-
#command ⇒ String
readonly
The command to execute.
-
#directory ⇒ String
readonly
Working directory for execution.
-
#environment ⇒ Hash<String,String>
readonly
Environment variables.
- #exit_on_failure(exit_on_failure) ⇒ Object
-
#metadata ⇒ Object
readonly
Command execution request with validation and metadata.
- #show_stderr(show_stderr) ⇒ Object
- #show_stdout(show_stdout) ⇒ Object
-
#timeout ⇒ Integer
readonly
Timeout in seconds.
Class Method Summary collapse
-
.from_hash(command_hash) ⇒ Request
Create a request from hash representation.
-
.from_json(json_string) ⇒ Request
Create a request from JSON string.
-
.from_string(command_string, **options) ⇒ Request
Create a request from a shell command string.
Instance Method Summary collapse
-
#cache_key ⇒ String
Generate a cache key for this request.
-
#equivalent_to?(other) ⇒ Boolean
Check if this request represents the same command.
- #exit_on_failure? ⇒ Boolean
-
#initialize(command:, arguments: [], **options) ⇒ Request
constructor
Initialize a new command request.
-
#run ⇒ Result
Execute the command.
- #show_stderr? ⇒ Boolean
- #show_stdout? ⇒ Boolean
-
#to_h ⇒ Hash
Convert request to hash representation.
-
#to_json(*args) ⇒ String
Convert request to JSON representation.
- #to_json_pretty(*args) ⇒ Object
-
#to_shell_command ⇒ String
Convert request to shell-executable command string.
- #try ⇒ Object
Constructor Details
#initialize(command:, arguments: [], **options) ⇒ Request
Initialize a new command request.
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: [], **) @command = validate_command(command) @arguments = validate_arguments(arguments) @environment = [:environment] || {} @directory = [:directory] || Dir.pwd @timeout = [:timeout] || Makit::Configuration::Timeout.global_default @metadata = [:metadata] || {} @exit_on_failure = [:exit_on_failure] || true @show_stderr = [:show_stderr] || true @show_stdout = [:show_stdout] || false validate_directory(@directory) validate_timeout(@timeout) end |
Instance Attribute Details
#arguments ⇒ Array<String> (readonly)
Returns command line arguments.
41 |
# File 'lib/makit/commands/request.rb', line 41 attr_reader :command, :arguments, :environment, :directory, :timeout, :metadata |
#command ⇒ String (readonly)
Returns the command to execute.
41 42 43 |
# File 'lib/makit/commands/request.rb', line 41 def command @command end |
#directory ⇒ String (readonly)
Returns working directory for execution.
41 |
# File 'lib/makit/commands/request.rb', line 41 attr_reader :command, :arguments, :environment, :directory, :timeout, :metadata |
#environment ⇒ Hash<String,String> (readonly)
Returns environment variables.
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 |
#metadata ⇒ Object (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 |
#timeout ⇒ Integer (readonly)
Returns 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.
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.
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.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/makit/commands/request.rb', line 113 def self.from_string(command_string, **) 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, **) end |
Instance Method Details
#cache_key ⇒ String
Generate a cache key for this request.
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.
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
186 187 188 |
# File 'lib/makit/commands/request.rb', line 186 def exit_on_failure? @exit_on_failure end |
#run ⇒ Result
Execute the command.
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
195 196 197 |
# File 'lib/makit/commands/request.rb', line 195 def show_stderr? @show_stderr end |
#show_stdout? ⇒ Boolean
204 205 206 |
# File 'lib/makit/commands/request.rb', line 204 def show_stdout? @show_stdout end |
#to_h ⇒ Hash
Convert request to hash representation.
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.
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_command ⇒ String
Convert request to shell-executable 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 |
#try ⇒ Object
231 232 233 |
# File 'lib/makit/commands/request.rb', line 231 def try Makit::Commands::Runner.default.execute(exit_on_failure(false)) end |