Module: Belt::CLI::AppDetection

Instance Method Summary collapse

Instance Method Details

#detect_app_nameObject

Detects the application name. Priority: terraform.tfvars app_name > variables.tf default > directory name.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/belt/cli/app_detection.rb', line 21

def detect_app_name
  # Check any environment tfvars for the app_name
  Dir.glob('infrastructure/*/terraform.tfvars').each do |f|
    content = File.read(f)
    match = content.match(/app_name\s*=\s*"([^"]+)"/)
    return match[1] if match
  end

  # Check variables.tf for a default app_name value
  name = detect_app_name_from_variables_tf
  return name if name

  # Fall back to directory name
  File.basename(Dir.pwd)
end

#detect_environmentsObject

Detects existing environments by scanning infrastructure/ directories. Each subdirectory with a main.tf is considered an environment.



39
40
41
# File 'lib/belt/cli/app_detection.rb', line 39

def detect_environments
  Dir.glob('infrastructure/*/main.tf').map { |f| File.basename(File.dirname(f)) }.sort
end

#detect_namespaceObject

Detects the primary gateway from the route definitions. Used by generators to determine controller directory and route file naming.



8
9
10
11
12
13
14
15
16
17
# File 'lib/belt/cli/app_detection.rb', line 8

def detect_namespace
  routes_file = find_routes_file_path
  if routes_file && File.exist?(routes_file)
    content = File.read(routes_file)
    # Prefer `gateway :name` but fall back to legacy `namespace :name` at top-level
    match = content.match(/^\s*gateway :(\w+)/) || content.match(/^\s*namespace :(\w+)/)
    return match[1] if match
  end
  File.basename(Dir.pwd)
end

#find_contracts_file_pathObject

Finds contracts file checking config/contracts.rb first, then legacy paths.



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

def find_contracts_file_path
  candidates = [
    'config/contracts.rb',
    'config/contracts.tf.rb',
    'config/schema.tf.rb',
    'infrastructure/schema.tf.rb'
  ]
  candidates.find { |f| File.exist?(f) }
end

#find_routes_file_pathObject

Finds routes file checking config/routes.rb first, then legacy paths.



50
51
52
53
54
55
56
57
# File 'lib/belt/cli/app_detection.rb', line 50

def find_routes_file_path
  candidates = [
    'config/routes.rb',
    'config/routes.tf.rb',
    'infrastructure/routes.tf.rb'
  ]
  candidates.find { |f| File.exist?(f) }
end

#find_schema_file_pathObject

Legacy alias for backward compatibility



71
72
73
# File 'lib/belt/cli/app_detection.rb', line 71

def find_schema_file_path
  find_contracts_file_path
end

#s3_safe_name(name) ⇒ Object

S3 bucket names follow DNS rules — underscores are not allowed. App names often use underscores (Ruby namespaces); convert for S3.



45
46
47
# File 'lib/belt/cli/app_detection.rb', line 45

def s3_safe_name(name)
  name.to_s.downcase.tr('_', '-')
end