Class: Ukiryu::CliCommands::BaseCommand Abstract
- Inherits:
-
Object
- Object
- Ukiryu::CliCommands::BaseCommand
- Defined in:
- lib/ukiryu/cli_commands/base_command.rb
Overview
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.
Direct Known Subclasses
CommandsCommand, ConfigCommand, DescribeCommand, ExtractCommand, InfoCommand, ListCommand, OptsCommand, RegisterCommand, ResolveCommand, RunCommand, RunFileCommand, SystemCommand, VersionCommand, WhichCommand
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#apply_cli_options_to_config ⇒ Object
Apply CLI options to the Config instance CLI options have the highest priority in the configuration chain.
-
#default_register_path ⇒ String?
Get the default register path.
-
#initialize(options = {}) ⇒ BaseCommand
constructor
Initialize a new command.
-
#run ⇒ Object
Execute the command.
-
#setup_register(custom_path = nil) ⇒ Object
Setup the register path.
-
#stringify_keys(hash) ⇒ Hash
Convert string keys to symbols.
Constructor Details
#initialize(options = {}) ⇒ BaseCommand
Initialize a new command
17 18 19 20 21 |
# File 'lib/ukiryu/cli_commands/base_command.rb', line 17 def initialize( = {}) @options = @config = Ukiryu::Config.instance end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/ukiryu/cli_commands/base_command.rb', line 12 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/ukiryu/cli_commands/base_command.rb', line 12 def @options end |
Instance Method Details
#apply_cli_options_to_config ⇒ Object
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 # 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 .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 = [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, [:verbose]) if .key?(:verbose) # Handle dry_run option return unless .key?(:dry_run) config.set_cli_option(:dry_run, [:dry_run]) end |
#default_register_path ⇒ String?
Get 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 |
#run ⇒ Object
Execute the command
Subclasses must implement this method
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
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
96 97 98 |
# File 'lib/ukiryu/cli_commands/base_command.rb', line 96 def stringify_keys(hash) hash.transform_keys(&:to_sym) end |