Module: Belt::CLI

Defined in:
lib/belt/cli.rb,
lib/belt/cli/new_command.rb,
lib/belt/cli/env_resolver.rb,
lib/belt/cli/logs_command.rb,
lib/belt/cli/app_detection.rb,
lib/belt/cli/backup_config.rb,
lib/belt/cli/backup_runner.rb,
lib/belt/cli/setup_command.rb,
lib/belt/cli/tasks_command.rb,
lib/belt/cli/views_command.rb,
lib/belt/cli/deploy_command.rb,
lib/belt/cli/doctor_command.rb,
lib/belt/cli/plugin_command.rb,
lib/belt/cli/routes_command.rb,
lib/belt/cli/server_command.rb,
lib/belt/cli/tables_command.rb,
lib/belt/cli/bucket_security.rb,
lib/belt/cli/console_command.rb,
lib/belt/cli/destroy_command.rb,
lib/belt/cli/frontend_command.rb,
lib/belt/cli/frontend_env_map.rb,
lib/belt/cli/generate_command.rb,
lib/belt/cli/contracts_command.rb,
lib/belt/cli/terraform_command.rb,
lib/belt/cli/environment_config.rb,
lib/belt/cli/generator_registry.rb,
lib/belt/cli/environment_command.rb,
lib/belt/cli/frontend_env_command.rb,
lib/belt/cli/lambda_config_command.rb,
lib/belt/cli/path_gem_materializer.rb,
lib/belt/cli/frontend_setup_command.rb,
lib/belt/cli/frontend_deploy_command.rb,
lib/belt/cli/routes_command/schema_loader.rb,
lib/belt/cli/routes_command/route_inference.rb

Defined Under Namespace

Modules: AppDetection, BucketSecurity, EnvResolver, GeneratorRegistry Classes: BackupConfig, BackupRunner, ConsoleCommand, ContractsCommand, DeployCommand, DestroyCommand, DoctorCommand, EnvironmentCommand, EnvironmentConfig, FrontendCommand, FrontendDeployCommand, FrontendEnvCommand, FrontendEnvMap, FrontendSetupCommand, GenerateCommand, LambdaConfigCommand, LogsCommand, NewCommand, PathGemMaterializer, PluginCommand, RoutesCommand, ServerCommand, SetupCommand, TablesCommand, TasksCommand, TerraformCommand, ViewsCommand

Constant Summary collapse

COMMANDS_DEFINITION =
{
  'new' => Belt::CLI::NewCommand,
  %w[generate g] => Belt::CLI::GenerateCommand,
  %w[destroy d] => Belt::CLI::DestroyCommand,
  'routes' => Belt::CLI::RoutesCommand,
  'contracts' => Belt::CLI::ContractsCommand,
  'lambda-config' => Belt::CLI::LambdaConfigCommand,
  %w[console c] => Belt::CLI::ConsoleCommand,
  'logs' => Belt::CLI::LogsCommand,
  %w[tasks --tasks -T] => Belt::CLI::TasksCommand,
  'setup' => Belt::CLI::SetupCommand,
  'doctor' => Belt::CLI::DoctorCommand,
  'plugin' => Belt::CLI::PluginCommand,
  'deploy' => Belt::CLI::DeployCommand,
  'frontend' => Belt::CLI::FrontendEnvCommand,
  %w[server s] => Belt::CLI::ServerCommand,
  %w[version --version -v] => ->(_args) { puts "Belt #{Belt::VERSION}" }
}.freeze
COMMANDS =
COMMANDS_DEFINITION.each_with_object({}) do |(keys, handler), hash|
  Array(keys).each { |key| hash[key] = handler }
end.freeze
TERRAFORM_ACTIONS =
Belt::CLI::TerraformCommand::ACTIONS
STANDALONE_COMMANDS =

Commands that can run without being inside a Belt project

%w[new version --version -v doctor].freeze

Class Method Summary collapse

Class Method Details

.ensure_project_root!(command) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/belt/cli.rb', line 179

def self.ensure_project_root!(command)
  if Belt.root?
    Dir.chdir(Belt.root)
  else
    puts not_in_app_message(command)
    exit 1
  end
end

.not_in_app_message(command) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/belt/cli.rb', line 162

def self.not_in_app_message(command)
  <<~MSG
    Could not find a Belt application. Run `belt #{command}` from within a Belt project directory, or create a new one:

      belt new <app_name>                         Create a new Belt application
      belt new <app_name> --frontend react        With a React frontend
      belt new <app_name> --domain myapp.com      With a custom domain

    Examples:
      belt new blog
      belt new my-api --frontend react
      belt new shop --domain shop.example.com

    See `belt new --help` for all options.
  MSG
end

.route_destroy_command(args) ⇒ Object

Routes belt destroy to DestroyCommand when args indicate a generator. Returns true if handled, false to fall through to TerraformCommand.



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/belt/cli.rb', line 190

def self.route_destroy_command(args) # rubocop:disable Naming/PredicateMethod
  if args.empty? || args.first =~ /\A-/
    if args.include?('--help') || args.include?('-h')
      DestroyCommand.run(args)
      return true
    end
  elsif DestroyCommand::GENERATORS.include?(args.first) || GeneratorRegistry.generator_names.include?(args.first)
    DestroyCommand.run(args)
    return true
  end
  false
end

.start(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/belt/cli.rb', line 59

def self.start(args)
  command = args.shift

  if command.nil?
    puts usage
    exit 1
  end

  # For project-level commands, find and chdir to the project root
  ensure_project_root!(command) unless STANDALONE_COMMANDS.include?(command)

  # `belt destroy` is ambiguous: could be terraform destroy or belt destroy <generator>.
  return if command == 'destroy' && route_destroy_command(args)

  # Terraform shorthand: belt init wups, belt plan wups, belt apply wups, belt destroy wups
  return Belt::CLI::TerraformCommand.run(command, args) if TERRAFORM_ACTIONS.include?(command)

  handler = COMMANDS[command]

  # If no built-in command matched, try running it as a rake task
  if handler.nil?
    return Belt::CLI::TasksCommand.invoke(command, args) if Belt::CLI::TasksCommand.rake_task?(command)

    puts "Unknown command: #{command}\n\n#{usage}"
    exit 1
  end

  if handler.is_a?(Proc)
    handler.call(args)
  else
    handler.run(args)
  end
end

.usageObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/belt/cli.rb', line 93

def self.usage
  <<~USAGE
    Usage: belt <command> [options]

    Commands:
      new <app_name> [--frontend react]           Create a new Belt application
      generate <scaffold|model|controller> <name> Generate components
      generate frontend <react|vue|svelte>        Scaffold a frontend app
      generate views <resource> [fields...]       Generate React pages for REST actions
      generate environment <name>                 Create a new environment
      destroy <scaffold|model|controller> <name>  Remove generated components
      destroy frontend                            Remove the frontend/ directory
      destroy views <resource>                    Remove React pages for a resource
      destroy environment <name>                  Remove an environment directory
      server                                      Start local dev server (frontend)
      s                                           Alias for server
      deploy [environment]                        Deploy to AWS (init → plan → apply)
      deploy frontend <env>                       Build and deploy frontend to AWS
      frontend env <env>                          Write frontend/.env from terraform outputs
      routes [-g PATTERN] [-f json]               Show route definitions
      contracts [-g PATTERN] [-f json]            Show API request/response contracts
      lambda-config [-e ENV] [-f json|terraform]  Show merged lambda configuration

      console                                     Start an interactive console (IRB)
      c                                           Alias for console
      logs [lambda] [-f] [-s 5m] [-e env]         View Lambda function logs
      tasks [-g PATTERN] [-a]                     List available rake tasks
      -T [-g PATTERN] [-a]                        Alias for tasks
      setup state                                 Create/select S3 state bucket
      setup tables <env>                          Generate DynamoDB tables from schema
      setup frontend <env>                        Generate S3 + CloudFront infrastructure
      doctor                                      Check system dependencies and AWS config
      plugin new <name>                           Scaffold a new Belt plugin gem
      init [environment] <env>                    terraform init for environment
      plan [environment] <env>                    terraform plan for environment
      apply [environment] <env>                   terraform apply for environment
      destroy [environment] <env>                 terraform destroy for environment
      output [environment] <env>                  terraform output for environment
      --version                                   Show Belt version

    Rake Tasks:
      Any rake task from your Gemfile dependencies can be run directly:
        belt lambda:build_layer                   Run a rake task by name

    Environment:
      Set BELT_ENV to skip the <env> argument:
        export BELT_ENV=wups
        belt apply                  # uses BELT_ENV
        belt apply dev01            # explicit arg wins

    Examples:
      belt new blog --frontend react
      belt new blog --frontend react -v   # list every created file
      belt generate scaffold post title:string content:text status:string
      belt destroy scaffold post
      belt generate frontend react
      belt server                   # Start local frontend server
      belt deploy                   # Deploy dev to AWS
      belt deploy prod --auto       # Deploy prod without confirmation
      belt deploy frontend wups
      belt frontend env wups        # Smart-merge TF outputs into frontend/.env
      belt setup frontend wups
      belt apply wups
      belt tasks                    # list all rake tasks
      belt lambda:build_layer       # run a rake task directly
      belt plugin new messaging     # scaffold a belt-messaging style plugin gem
  USAGE
end