Class: Belt::CLI::AuthCommand

Inherits:
Object
  • Object
show all
Includes:
AppDetection
Defined in:
lib/belt/cli/auth_command.rb

Constant Summary collapse

TEMPLATE_DIR =
File.expand_path('../../templates/generate/auth', __dir__)
MODULE_DIR =
'infrastructure/modules/app'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AppDetection

#detect_app_name, #detect_environments, #detect_namespace, #find_contracts_file_path, #find_routes_file_path, #find_schema_file_path, #s3_safe_name

Constructor Details

#initialize(pools:, force: false) ⇒ AuthCommand

Returns a new instance of AuthCommand.



75
76
77
78
79
80
# File 'lib/belt/cli/auth_command.rb', line 75

def initialize(pools:, force: false)
  @pool_names = pools
  @force = force
  @app_name = detect_app_name
  @pools = 
end

Class Method Details

.destroy(_args) ⇒ Object



27
28
29
# File 'lib/belt/cli/auth_command.rb', line 27

def self.destroy(_args)
  new(pools: [], force: false).remove
end

.parse_pools(args) ⇒ Object

Parse pool names from args. Default to ["main"] if none provided.



69
70
71
72
73
# File 'lib/belt/cli/auth_command.rb', line 69

def self.parse_pools(args)
  names = args.reject { |a| a.start_with?('-') }
  names = ['main'] if names.empty?
  names.map(&:downcase).map { |n| n.gsub(/[^a-z0-9_]/, '_') }
end


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
58
59
60
61
62
63
64
65
66
# File 'lib/belt/cli/auth_command.rb', line 31

def self.print_help
  puts <<~HELP
    Generate Cognito user pool infrastructure for authentication.

    Usage: belt generate auth [pool_names...] [options]

    Options:
      --force, -f     Overwrite existing cognito.tf (skip collision check)

    Arguments:
      pool_names      Optional pool names for multiple user pools.
                      If omitted, generates a single pool named "main".

    Examples:
      belt g auth                        # Single pool: "main"
      belt g auth web                    # Single pool: "web"
      belt g auth web mobile             # Two pools: "web" and "mobile"
      belt g auth web android ios        # Three pools: "web", "android", "ios"
      belt g auth --force                # Overwrite existing

    What this generates:
      infrastructure/modules/app/cognito.tf          User pool + client resources
      infrastructure/modules/app/cognito_outputs.tf  Pool ID, ARN, and client ID outputs

    It also patches:
      infrastructure/modules/app/main.tf             Adds cognito_user_pool_arns to conveyor_belt

    After running:
      1. Review the generated Cognito config in cognito.tf
      2. Customize password policy, MFA, triggers as needed
      3. Run `belt apply <env>` to deploy

    To remove:
      belt destroy auth
  HELP
end

.run(args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/belt/cli/auth_command.rb', line 15

def self.run(args)
  if args.include?('--help') || args.include?('-h')
    print_help
    exit 0
  end

  force = args.delete('--force') || args.delete('-f')
  pools = parse_pools(args)

  new(pools: pools, force: force).generate
end

Instance Method Details

#generateObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/belt/cli/auth_command.rb', line 82

def generate
  check_collision! unless @force
  ensure_module_dir!

  write_cognito_tf
  write_cognito_outputs_tf
  patch_main_tf

  puts "\nāœ“ Auth generated!"
  puts "\nNext steps:"
  puts '  1. Review infrastructure/modules/app/cognito.tf'
  puts '  2. Customize password policy, MFA, or Lambda triggers as needed'
  puts '  3. Run `belt apply <env>` to deploy'
end

#removeObject



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
# File 'lib/belt/cli/auth_command.rb', line 97

def remove
  removed = []

  cognito_tf = File.join(MODULE_DIR, 'cognito.tf')
  cognito_outputs_tf = File.join(MODULE_DIR, 'cognito_outputs.tf')

  if File.exist?(cognito_tf)
    FileUtils.rm(cognito_tf)
    removed << cognito_tf
    puts "  remove  #{cognito_tf}"
  end

  if File.exist?(cognito_outputs_tf)
    FileUtils.rm(cognito_outputs_tf)
    removed << cognito_outputs_tf
    puts "  remove  #{cognito_outputs_tf}"
  end

  unpatch_main_tf
  removed << File.join(MODULE_DIR, 'main.tf') if @main_tf_patched

  if removed.empty?
    puts '  Nothing to remove — auth was not generated.'
  else
    puts "\nāœ“ Auth destroyed!"
  end
end