Module: Belt::CLI::AppDetection

Instance Method Summary collapse

Instance Method Details

#detect_app_nameObject

Detects the application name. Prefers terraform.tfvars app_name, then falls back to directory name.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/belt/cli/app_detection.rb', line 19

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

  # 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.



33
34
35
# File 'lib/belt/cli/app_detection.rb', line 33

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

#detect_namespaceObject

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



8
9
10
11
12
13
14
15
# 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)
    match = File.read(routes_file).match(/namespace :(\w+)/)
    return match[1] if match
  end
  File.basename(Dir.pwd)
end

#find_routes_file_pathObject

Finds routes.tf.rb checking config/ first, then infrastructure/ (legacy).



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

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

#find_schema_file_pathObject

Finds schema.tf.rb checking config/ first, then infrastructure/ (legacy).



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

def find_schema_file_path
  candidates = ['config/schema.tf.rb', 'infrastructure/schema.tf.rb']
  candidates.find { |f| File.exist?(f) }
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.



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

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