Module: Roast::Tools::Cmd

Extended by:
Cmd
Included in:
Cmd
Defined in:
lib/roast/tools/cmd.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Add this method to be included in other classes



27
28
29
# File 'lib/roast/tools/cmd.rb', line 27

def included(base)
  @base_class = base
end

.post_configuration_setup(base, config = {}) ⇒ Object

Called after configuration is loaded



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/roast/tools/cmd.rb', line 32

def post_configuration_setup(base, config = {})
  allowed_commands = config[CONFIG_ALLOWED_COMMANDS] || DEFAULT_ALLOWED_COMMANDS

  allowed_commands.each do |command_entry|
    case command_entry
    when String
      register_command_function(base, command_entry, nil)
    when Hash
      command_name = command_entry["name"] || command_entry[:name]
      description = command_entry["description"] || command_entry[:description]

      if command_name.nil?
        raise ArgumentError, "Command configuration must include 'name' field"
      end

      register_command_function(base, command_name, description)
    else
      raise ArgumentError, "Invalid command configuration format: #{command_entry.inspect}"
    end
  end
end

Instance Method Details

#call(command, config = {}, timeout: 30) ⇒ Object

Legacy method for backward compatibility



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/roast/tools/cmd.rb', line 101

def call(command, config = {}, timeout: 30)
  Roast::Helpers::Logger.info("🔧 Running command: #{command}\n")

  allowed_commands = config[CONFIG_ALLOWED_COMMANDS] || DEFAULT_ALLOWED_COMMANDS
  validation_result = validate_command(command, allowed_commands)
  return validation_result unless validation_result.nil?

  command_prefix = command.split(" ").first

  execute_command(command, command_prefix, timeout)
rescue StandardError => e
  handle_error(e)
end

#execute_allowed_command(full_command, command_prefix, timeout = 30) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/roast/tools/cmd.rb', line 92

def execute_allowed_command(full_command, command_prefix, timeout = 30)
  Roast::Helpers::Logger.info("🔧 Running command: #{full_command}\n")

  execute_command(full_command, command_prefix, timeout)
rescue StandardError => e
  handle_error(e)
end