Class: Kdep::Commands::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/kdep/commands/init.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_options:, command_options:, args:) ⇒ Init

Returns a new instance of Init.



18
19
20
21
22
23
# File 'lib/kdep/commands/init.rb', line 18

def initialize(global_options:, command_options:, args:)
  @global_options = global_options
  @command_options = command_options
  @args = args
  @ui = Kdep::UI.new
end

Class Method Details

.option_parserObject



8
9
10
11
12
13
14
15
16
# File 'lib/kdep/commands/init.rb', line 8

def self.option_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: kdep init [preset] [name]"
    opts.separator ""
    opts.separator "Scaffolds a new kdep deploy target."
    opts.on("--namespace=NAMESPACE", "Kubernetes namespace")
    opts.on("--registry=REGISTRY", "Container registry URL")
  end
end

Instance Method Details

#executeObject



25
26
27
28
29
30
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
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
92
93
94
95
96
97
98
# File 'lib/kdep/commands/init.rb', line 25

def execute
  preset = @args[0]
  deploy_name = @args[1]

  unless preset
    @ui.error("Missing preset and name arguments")
    @ui.info("Usage: kdep init [preset] [name]")
    @ui.info("Available presets: #{Kdep::Preset::BUILT_IN.join(', ')}")
    exit 1
  end

  unless deploy_name
    @ui.error("Missing name argument")
    @ui.info("Usage: kdep init [preset] [name]")
    exit 1
  end

  unless Kdep::Preset::BUILT_IN.include?(preset)
    @ui.error("Unknown preset: #{preset}")
    @ui.info("Available presets: #{Kdep::Preset::BUILT_IN.join(', ')}")
    exit 1
  end

  target_dir = File.join(Dir.pwd, "kdep", deploy_name)

  if File.exist?(target_dir)
    @ui.error("kdep/#{deploy_name} already exists")
    exit 1
  end

  FileUtils.mkdir_p(target_dir)

  # Render app.yml from ERB template
  namespace = @command_options[:namespace]
  registry = @command_options[:registry]

  template_path = File.join(Kdep.templates_dir, "init", "app.yml.erb")
  template = File.read(template_path)
  erb = if RUBY_VERSION >= "2.6"
          ERB.new(template, trim_mode: "-")
        else
          ERB.new(template, nil, "-")
        end
  binding_ctx = erb_binding(preset: preset, deploy_name: deploy_name,
                            namespace: namespace, registry: registry)
  app_yml_content = erb.result(binding_ctx)

  File.write(File.join(target_dir, "app.yml"), app_yml_content)
  @ui.file_written("kdep/#{deploy_name}/app.yml")

  # Copy secrets.yml
  FileUtils.cp(
    File.join(Kdep.templates_dir, "init", "secrets.yml"),
    File.join(target_dir, "secrets.yml")
  )
  @ui.file_written("kdep/#{deploy_name}/secrets.yml")

  # Copy .gitignore
  FileUtils.cp(
    File.join(Kdep.templates_dir, "init", "gitignore"),
    File.join(target_dir, ".gitignore")
  )
  @ui.file_written("kdep/#{deploy_name}/.gitignore")

  if preset == "custom"
    FileUtils.mkdir_p(File.join(target_dir, "presets"))
    FileUtils.mkdir_p(File.join(target_dir, "resources"))
    @ui.info("Created presets/ and resources/ directories for custom preset")
    @ui.info("Add resource names to kdep/#{deploy_name}/presets/custom (one per line)")
    @ui.info("Add matching .yml.erb templates to kdep/#{deploy_name}/resources/")
  end

  @ui.success("Initialized kdep/#{deploy_name} with #{preset} preset")
end