Class: Kdep::Commands::Eject

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Eject.



15
16
17
18
19
20
# File 'lib/kdep/commands/eject.rb', line 15

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



7
8
9
10
11
12
13
# File 'lib/kdep/commands/eject.rb', line 7

def self.option_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: kdep eject [resource_name]"
    opts.separator ""
    opts.separator "Copies a built-in template to your deploy target for customization."
  end
end

Instance Method Details

#executeObject



22
23
24
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
# File 'lib/kdep/commands/eject.rb', line 22

def execute
  resource_name = @args[0]

  if resource_name.nil?
    list_available_resources
    return
  end

  # Find kdep dir
  discovery = Kdep::Discovery.new
  kdep_dir = discovery.find_kdep_dir

  unless kdep_dir
    @ui.error("No kdep/ directory found (run kdep init first)")
    exit 1
  end

  # Find deploy directory
  deploys = discovery.find_deploys
  if deploys.empty?
    @ui.error("No deploy targets found in #{kdep_dir}/")
    exit 1
  end

  deploy_name = deploys.first
  deploy_dir = File.join(kdep_dir, deploy_name)

  # Check built-in template exists
  source = File.join(Kdep.templates_dir, "resources", "#{resource_name}.yml.erb")
  unless File.exist?(source)
    @ui.error("Unknown resource: #{resource_name}")
    @ui.info("Available resources: #{available_resources.join(', ')}")
    exit 1
  end

  # Target path
  resources_dir = File.join(deploy_dir, "resources")
  target = File.join(resources_dir, "#{resource_name}.yml.erb")

  if File.exist?(target)
    @ui.warn("#{resource_name}.yml.erb already exists in resources/ -- skipping (delete it first to re-eject)")
    return
  end

  # Create resources dir and copy
  FileUtils.mkdir_p(resources_dir)
  FileUtils.cp(source, target)

  @ui.file_written(target)
  @ui.info("Edit #{target} to customize the #{resource_name} template")
end