Class: Belt::CLI::ExplainCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/cli/explain_command.rb

Constant Summary collapse

DOCS_DIR =
File.expand_path('../docs', __dir__)
TOPICS =
Dir.glob(File.join(DOCS_DIR, '*.md')).map do |path|
  File.basename(path, '.md')
end.sort.freeze
ALIASES =
{
  'routes' => 'routing',
  'route' => 'routing',
  'router' => 'routing',
  'controller' => 'controllers',
  'model' => 'models',
  'activeitem' => 'models',
  'dynamodb' => 'models',
  'deploy' => 'deployment',
  'deploying' => 'deployment',
  'terraform' => 'deployment',
  'generate' => 'generators',
  'generator' => 'generators',
  'scaffold' => 'generators',
  'handler' => 'lambda_handler',
  'lambda' => 'lambda_handler',
  'entry_point' => 'lambda_handler',
  'entrypoint' => 'lambda_handler',
  'project' => 'structure',
  'layout' => 'structure',
  'directory' => 'structure',
  'logs' => 'observability',
  'logging' => 'observability',
  'metrics' => 'observability',
  'backup' => 'backups',
  'plugin' => 'plugins',
  'irb' => 'console',
  'repl' => 'console'
}.freeze

Class Method Summary collapse

Class Method Details

.display_topic(topic) ⇒ Object



71
72
73
74
75
# File 'lib/belt/cli/explain_command.rb', line 71

def self.display_topic(topic)
  path = File.join(DOCS_DIR, "#{topic}.md")
  content = File.read(path)
  puts content
end

.resolve_topic(input) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/belt/cli/explain_command.rb', line 61

def self.resolve_topic(input)
  normalized = input.downcase.gsub('-', '_')
  return normalized if TOPICS.include?(normalized)
  return ALIASES[normalized] if ALIASES[normalized]

  # Fuzzy match: find topics that start with or contain the input
  match = TOPICS.find { |t| t.start_with?(normalized) }
  match || TOPICS.find { |t| t.include?(normalized) }
end

.run(args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/belt/cli/explain_command.rb', line 42

def self.run(args)
  if args.empty? || args.include?('--help') || args.include?('-h')
    puts usage
    return
  end

  topic = resolve_topic(args.first)

  if topic.nil?
    puts "Unknown topic: #{args.first}\n\n"
    puts 'Available topics:'
    TOPICS.each { |t| puts "  #{t}" }
    puts "\nRun `belt explain <topic>` for details."
    exit 1
  end

  display_topic(topic)
end

.usageObject



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
105
106
107
# File 'lib/belt/cli/explain_command.rb', line 77

def self.usage
  topic_list = TOPICS.map { |t| "  #{t}" }.join("\n")

  <<~USAGE
    Usage: belt explain <topic>

    Display documentation for a Belt concept or feature.

    Available topics:
    #{topic_list}

    Aliases:
      routes, route, router     → routing
      controller                → controllers
      model, activeitem         → models
      deploy, terraform         → deployment
      generate, scaffold        → generators
      handler, lambda           → lambda_handler
      project, layout           → structure
      logs, logging, metrics    → observability
      backup                    → backups
      plugin                    → plugins
      irb, repl                 → console

    Examples:
      belt explain routing
      belt explain controllers
      belt explain deploy
      belt explain models
  USAGE
end