Class: Kitsune::Kit::Commands::SwitchEnv

Inherits:
Thor
  • Object
show all
Defined in:
lib/kitsune/kit/commands/switch_env.rb

Instance Method Summary collapse

Instance Method Details

#to(env_name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kitsune/kit/commands/switch_env.rb', line 11

def to(env_name)
  kit_env_path = ".kitsune/kit.env"
  infra_env_path = ".kitsune/infra.#{env_name}.env"
  blueprint_path = File.expand_path("../../blueprints/.env.template", __dir__)

  unless File.exist?(kit_env_path)
    say "❌ No .kitsune/kit.env found. Did you run `kitsune kit init`?", :red
    exit(1)
  end

  content = File.read(kit_env_path)

  if content.match?(/^KIT_ENV=/)
    new_content = content.gsub(/^KIT_ENV=.*/, "KIT_ENV=#{env_name}")
  else
    new_content = "KIT_ENV=#{env_name}\n" + content
  end

  File.write(kit_env_path, new_content)
  say "🎯 Environment switched to '#{env_name}' in .kitsune/kit.env", :green

  unless File.exist?(infra_env_path)
    FileUtils.cp(blueprint_path, infra_env_path)
    say "📝 Created new infra environment file: #{infra_env_path}", :cyan
  else
    say "📄 Infra environment file already exists: #{infra_env_path}", :green
  end
end