Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/makit/mp/string_mp.rb
Overview
monkey patch String class with command execution methods
Instance Method Summary collapse
- #cache_log(log_file) ⇒ Object
- #cache_run(timestamp = nil) ⇒ Object
- #cache_run_with_timestamp(timestamp) ⇒ Object
- #cache_try(timestamp = nil) ⇒ Object
- #cache_try_with_timestamp(timestamp) ⇒ Object
- #log ⇒ Object
- #run(args = nil) ⇒ Object
- #show ⇒ Object
- #show_command(cmd) ⇒ Object
- #to_markdown ⇒ Object
- #try ⇒ Object
Instance Method Details
#cache_log(log_file) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/makit/mp/string_mp.rb', line 179 def cache_log(log_file) command = self request = Makit::Commands::Request.from_string(command) result = Makit::Commands::Runner.default.execute(request) # Write output to log file FileUtils.mkdir_p(File.dirname(log_file)) File.write(log_file, result.stdout) if result.stdout && !result.stdout.empty? File.write(log_file, result.stderr, mode: "a") if result.stderr && !result.stderr.empty? result end |
#cache_run(timestamp = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/makit/mp/string_mp.rb', line 64 def cache_run( = nil) puts "cache_run: #{self}" command = self request = Makit::Commands::Request.from_string(command) if # Add timestamp to metadata for cache key generation request = Makit::Commands::Request.new( command: request.command, arguments: request.arguments, directory: request.directory, timeout: request.timeout, environment: request.environment || {}, metadata: (request. || {}).merge(timestamp: ), ) puts "timestamp: #{}" else puts "no timestamp" end Makit::Commands::Runner.default.execute(request) end |
#cache_run_with_timestamp(timestamp) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/makit/mp/string_mp.rb', line 151 def () command = self request = Makit::Commands::Request.from_string(command) request = Makit::Commands::Request.new( command: request.command, arguments: request.arguments, directory: request.directory, timeout: request.timeout, environment: request.environment || {}, metadata: (request. || {}).merge(timestamp: ), ) Makit::Commands::Runner.default.execute(request) end |
#cache_try(timestamp = nil) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/makit/mp/string_mp.rb', line 112 def cache_try( = nil) command = self request = Makit::Commands::Request.from_string(command) if # Add timestamp to metadata for cache key generation request = Makit::Commands::Request.new( command: request.command, arguments: request.arguments, directory: request.directory, timeout: request.timeout, environment: request.environment || {}, metadata: (request. || {}).merge(timestamp: ), ) end Makit::Commands::Runner.default.execute(request) end |
#cache_try_with_timestamp(timestamp) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/makit/mp/string_mp.rb', line 165 def () command = self request = Makit::Commands::Request.from_string(command) request = Makit::Commands::Request.new( command: request.command, arguments: request.arguments, directory: request.directory, timeout: request.timeout, environment: request.environment || {}, metadata: (request. || {}).merge(timestamp: ), ) Makit::Commands::Runner.default.execute(request) end |
#log ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/makit/mp/string_mp.rb', line 97 def log command = self request = Makit::Commands::Request.from_string(command) result = Makit::Commands::Runner.default.execute(request) # Log the command execution result if result.success? Makit::Logging.info("Command completed successfully: #{command}") else Makit::Logging.error("Command failed: #{command} (exit code: #{result.exit_code})") end result end |
#run(args = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/makit/mp/string_mp.rb', line 8 def run(args = nil) command = self if args.nil? request = Makit::Commands::Request.from_string(command) else # Parse initial request base_request = Makit::Commands::Request.from_string(command) if args.is_a?(Hash) # Create new request with combined options = { command: base_request.command, arguments: base_request.arguments || [], directory: base_request.directory, timeout: base_request.timeout, environment: base_request.environment || {}, metadata: base_request. || {}, } # Apply args to the request options args.each do |key, value| case key.to_s when "arguments" # Append additional arguments [:arguments] = ([:arguments] + Array(value)).flatten when "directory" [:directory] = value when "timeout" [:timeout] = value when "environment" [:environment] = ([:environment] || {}).merge(value) when "metadata" [:metadata] = ([:metadata] || {}).merge(value) else # Add unknown keys to metadata [:metadata] = ([:metadata] || {}).merge(key.to_s => value) end end # Create new request with combined options request = Makit::Commands::Request.new(**) else # args is an array of additional arguments request = Makit::Commands::Request.new( command: base_request.command, arguments: (base_request.arguments || []) + Array(args), directory: base_request.directory, timeout: base_request.timeout, environment: base_request.environment || {}, metadata: base_request. || {}, ) end end Makit::Commands::Runner.default.execute(request) end |
#show ⇒ Object
91 92 93 94 95 |
# File 'lib/makit/mp/string_mp.rb', line 91 def show command = self request = Makit::Commands::Request.from_string(command) Makit::Commands::Runner.default.execute(request) end |
#show_command(cmd) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/makit/mp/string_mp.rb', line 129 def show_command(cmd) if cmd.success? puts "✅ #{cmd.command} #{cmd.arguments&.join(" ")}".colorize(:green) else puts "❌ #{cmd.command} #{cmd.arguments&.join(" ")}".colorize(:red) puts " Exit code: #{cmd.exit_code}".colorize(:red) puts " Error: #{cmd.stderr}".colorize(:red) if cmd.stderr && !cmd.stderr.empty? end end |
#to_markdown ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/makit/mp/string_mp.rb', line 139 def to_markdown command = self request = Makit::Commands::Request.from_string(command) result = Makit::Commands::Runner.default.execute(request) if result.success? "```\n#{result.stdout}\n```" else "```\nError: #{result.stderr}\n```" end end |
#try ⇒ Object
85 86 87 88 89 |
# File 'lib/makit/mp/string_mp.rb', line 85 def try command = self request = Makit::Commands::Request.from_string(command).exit_on_failure(false) Makit::Commands::Runner.default.execute(request) end |