Class: Belt::CLI::RoutesCommand

Inherits:
Object
  • Object
show all
Includes:
RequestModelInference, RouteInference, SchemaLoader
Defined in:
lib/belt/cli/routes_command.rb,
lib/belt/cli/routes_command/schema_loader.rb,
lib/belt/cli/routes_command/route_inference.rb,
lib/belt/cli/routes_command/request_model_inference.rb

Defined Under Namespace

Modules: RequestModelInference, RouteInference, SchemaLoader

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ RoutesCommand

Returns a new instance of RoutesCommand.



23
24
25
26
# File 'lib/belt/cli/routes_command.rb', line 23

def initialize(args)
  @options = {}
  parse_options(args)
end

Class Method Details

.run(args) ⇒ Object



19
20
21
# File 'lib/belt/cli/routes_command.rb', line 19

def self.run(args)
  new(args).run
end

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/belt/cli/routes_command.rb', line 28

def run
  routes_file = find_routes_file
  unless routes_file
    abort 'Error: No routes file found. ' \
          'Expected config/routes.rb (or config/routes.tf.rb, infrastructure/routes.tf.rb)'
  end

  dsl = load_routes(routes_file)
  @table_inference = TableInference.new(@options[:tables_file])
  routes = collect_routes(dsl)

  # Convention-based request_model inference from contracts
  contracts_file = resolve_contracts_file(routes_file)
  infer_request_models!(routes, contracts_file) if contracts_file

  routes = apply_grep(routes) if @options[:grep]

  warn 'Warning: --output-dir has no effect without --namespace' if @options[:output_dir] && !@options[:namespace]

  if @options[:namespace]
    output_ruby(routes, @options[:namespace], routes_file)
  elsif @options[:format] == 'json'
    output = { routes: routes }
    models = load_schema_models(routes_file)
    output[:models] = models if models.any?
    puts JSON.pretty_generate(output)
  else
    output_concise(routes)
  end
end