Class: Synthra::CLI::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/cli/commands/base.rb

Overview

Base class for all CLI commands

Provides common functionality for command execution including option parsing, error handling, and registry management.

Direct Known Subclasses

Diff, Export, Generate, Lint, Validate

Constant Summary collapse

EXIT_SUCCESS =

Exit codes

0
EXIT_PARSE_ERROR =
1
EXIT_RUNTIME_ERROR =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options) ⇒ Base

Create a new command instance

Parameters:

  • args (Array<String>)

    command arguments

  • options (Hash)

    parsed options



24
25
26
27
# File 'lib/synthra/cli/commands/base.rb', line 24

def initialize(args, options)
  @args = args
  @options = options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



17
18
19
# File 'lib/synthra/cli/commands/base.rb', line 17

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/synthra/cli/commands/base.rb', line 17

def options
  @options
end

Instance Method Details

#callInteger

Execute the command

Returns:

  • (Integer)

    exit code

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/synthra/cli/commands/base.rb', line 33

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

#error(message) ⇒ Object (protected)

Print error message

Parameters:

  • message (String)

    message to print



91
92
93
# File 'lib/synthra/cli/commands/base.rb', line 91

def error(message)
  $stderr.puts "#{message}"
end

#info(message) ⇒ Object (protected)

Print info message

Parameters:

  • message (String)

    message to print



99
100
101
# File 'lib/synthra/cli/commands/base.rb', line 99

def info(message)
  puts message
end

#load_path(registry, path, validate: false) ⇒ Object (protected)

Load from path (file or directory)

Parameters:

  • registry (Registry)

    registry to load into

  • path (String)

    file or directory path

  • validate (Boolean) (defaults to: false)

    whether to validate



71
72
73
74
75
76
77
# File 'lib/synthra/cli/commands/base.rb', line 71

def load_path(registry, path, validate: false)
  if File.directory?(path)
    load_schemas_from_dir(registry, path, validate: validate)
  else
    registry.load_file(path, validate: validate)
  end
end

#load_schemas_from_dir(registry, dir, validate: true) ⇒ void (protected)

This method returns an undefined value.

Load schemas from a directory

Parameters:

  • registry (Registry)

    registry to load schemas into

  • dir (String)

    directory path

  • validate (Boolean) (defaults to: true)

    whether to validate on load

Raises:

  • (Errno::ENOENT)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/synthra/cli/commands/base.rb', line 46

def load_schemas_from_dir(registry, dir, validate: true)
  dir = File.expand_path(dir)
  raise Errno::ENOENT, "Directory not found: #{dir}" unless Dir.exist?(dir)

  pattern = File.join(dir, "*.dsl")
  files = Dir.glob(pattern)

  if files.empty?
    $stderr.puts "No .dsl files found in #{dir}"
    return
  end

  files.each do |f|
    file_path = File.expand_path(f)
    raise ArgumentError, "Invalid file path: #{f}" unless file_path.start_with?(dir)
    registry.load_file(f, validate: validate)
  end
end

#success(message) ⇒ Object (protected)

Print success message

Parameters:

  • message (String)

    message to print



83
84
85
# File 'lib/synthra/cli/commands/base.rb', line 83

def success(message)
  puts "#{message}"
end