Class: Ukiryu::CliCommands::RunCommand

Inherits:
BaseCommand show all
Includes:
ResponseFormatter
Defined in:
lib/ukiryu/cli_commands/run_command.rb

Overview

Execute a tool command inline (shorthand for run)

Constant Summary

Constants included from ResponseFormatter

Ukiryu::CliCommands::ResponseFormatter::OUTPUT_FORMATS

Instance Attribute Summary

Attributes inherited from BaseCommand

#config, #options

Instance Method Summary collapse

Methods included from ResponseFormatter

#output_response

Methods inherited from BaseCommand

#apply_cli_options_to_config, #default_register_path, #initialize, #setup_register, #stringify_keys

Constructor Details

This class inherits a constructor from Ukiryu::CliCommands::BaseCommand

Instance Method Details

#run(tool_name, command_name = nil, *params) ⇒ Object

Execute the command

Parameters:

  • tool_name (String)

    the tool name

  • command_name (String, nil) (defaults to: nil)

    the command name (optional, uses default if nil)

  • params (Array<String>)

    key=value parameter pairs



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ukiryu/cli_commands/run_command.rb', line 16

def run(tool_name, command_name = nil, *params)
  setup_register

  # Handle the case where command_name is omitted and first param is a key=value pair
  # When user types: ukiryu exec-inline ping host=127.0.0.1
  # Thor interprets it as: tool_name="ping", command_name="host=127.0.0.1", params=["count=1"]
  # We need to detect if command_name looks like a parameter
  if command_name&.include?('=')
    # command_name is actually a parameter, shift it back to params
    params.unshift(command_name)
    command_name = nil
  end

  # Special handling for "help" command
  if command_name&.to_s == 'help'
    show_tool_help(tool_name, params)
    return
  end

  # Resolve command name if not provided
  command_name ||= resolve_default_command(tool_name)

  # Parse key=value pairs into arguments hash
  arguments = parse_inline_params(params)

  # Handle stdin from CLI flag (--stdin) or special parameter (stdin=-)
  if options[:stdin] || arguments[:stdin] == '-'
    # Read from actual stdin
    stdin_data = $stdin.read
    arguments[:stdin] = stdin_data
  elsif arguments[:stdin]
    # stdin parameter contains data (string or file path)
    # If it starts with @, treat as file path
    if arguments[:stdin].is_a?(String) && arguments[:stdin].start_with?('@')
      file_path = arguments[:stdin][1..]
      begin
        arguments[:stdin] = File.read(file_path)
      rescue Errno::ENOENT
        error! "File not found: #{file_path}"
      end
    end
    # Otherwise, use the value as-is (already a string or IO object)
  end

  # Build execution request
  request = {
    'tool' => tool_name,
    'command' => command_name,
    'arguments' => arguments
  }

  # Output debug: Ukiryu CLI Options
  if config.debug
    logger = Ukiryu::Logger.new
    ukiryu_options = {
      format: config.format,
      debug: config.debug,
      dry_run: config.dry_run,
      output: config.output,
      register: config.register,
      stdin: !arguments[:stdin].nil?
    }
    logger.debug_section_ukiryu_options(ukiryu_options)
  end

  # Get format from Config (priority: CLI > ENV > programmatic > default)
  # --raw flag overrides the format setting
  format = if options[:raw]
             :raw
           else
             config.format.to_sym
           end
  error! "Invalid format: #{format}. Must be one of: #{OUTPUT_FORMATS.join(', ')}" unless OUTPUT_FORMATS.include?(format)

  if config.dry_run
    # Show dry run output
    say_dry_run(request)
    return
  end

  # Execute the request
  response = execute_request(request, tool_name, command_name)

  # Output response
  output_file = config.output
  output_response(response, format, output_file, config)

  # Don't exit here - let Thor handle the result
end