Class: Ukiryu::CliCommands::BaseCommand Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ukiryu/cli_commands/base_command.rb

Overview

This class is abstract.

Subclasses must implement the ‘run` method

Base class for CLI commands

Provides shared functionality for all CLI commands including register setup, output formatting, and error handling.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BaseCommand

Initialize a new command

Parameters:

  • options (Hash) (defaults to: {})

    command options from Thor



17
18
19
20
21
# File 'lib/ukiryu/cli_commands/base_command.rb', line 17

def initialize(options = {})
  @options = options
  @config = Ukiryu::Config.instance
  apply_cli_options_to_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/ukiryu/cli_commands/base_command.rb', line 12

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/ukiryu/cli_commands/base_command.rb', line 12

def options
  @options
end

Instance Method Details

#apply_cli_options_to_configObject

Apply CLI options to the Config instance CLI options have the highest priority in the configuration chain



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
# File 'lib/ukiryu/cli_commands/base_command.rb', line 46

def apply_cli_options_to_config
  # Thor option defaults that should not override ENV or programmatic config
  # Only apply CLI option if it's not the default value
  cli_mappings = {
    format: 'yaml', # default format in Thor
    output: nil,
    register: nil,
    timeout: nil,
    shell: nil
  }

  cli_mappings.each do |cli_key, default_value|
    next unless options.key?(cli_key)

    # Only set CLI option if it's not the default value
    # This allows ENV and programmatic config to take precedence when user doesn't specify
    option_value = options[cli_key]
    should_set = if default_value.nil?
                   # No default, always set
                   !option_value.nil? && !option_value.empty?
                 else
                   # Only set if different from default (user explicitly specified)
                   option_value != default_value
                 end

    config.set_cli_option(cli_key, option_value) if should_set
  end

  # Handle boolean options from Thor
  config.set_cli_option(:debug, options[:verbose]) if options.key?(:verbose)

  # Handle dry_run option
  return unless options.key?(:dry_run)

  config.set_cli_option(:dry_run, options[:dry_run])
end

#default_register_pathString?

Get the default register path

Returns:

  • (String, nil)

    the default register path



86
87
88
89
90
# File 'lib/ukiryu/cli_commands/base_command.rb', line 86

def default_register_path
  Ukiryu::Register.default.path
rescue Ukiryu::Register::Error
  nil
end

#runObject

Execute the command

Subclasses must implement this method

Raises:

  • (NotImplementedError)

    if not implemented in subclass



28
29
30
# File 'lib/ukiryu/cli_commands/base_command.rb', line 28

def run
  raise NotImplementedError, "#{self.class} must implement #run"
end

#setup_register(custom_path = nil) ⇒ Object

Setup the register path

Parameters:

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

    custom register path



35
36
37
38
39
40
41
42
# File 'lib/ukiryu/cli_commands/base_command.rb', line 35

def setup_register(custom_path = nil)
  register_path = custom_path || config.register || default_register_path
  return unless register_path && Dir.exist?(register_path)

  # Set UKIRYU_REGISTER env and reset the default register
  ENV['UKIRYU_REGISTER'] = register_path
  Ukiryu::Register.reset_default
end

#stringify_keys(hash) ⇒ Hash

Convert string keys to symbols

Parameters:

  • hash (Hash)

    hash with string keys

Returns:

  • (Hash)

    hash with symbol keys



96
97
98
# File 'lib/ukiryu/cli_commands/base_command.rb', line 96

def stringify_keys(hash)
  hash.transform_keys(&:to_sym)
end