Class: Kdep::Commands::Doctor

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Doctor.



21
22
23
24
25
26
# File 'lib/kdep/commands/doctor.rb', line 21

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

Class Method Details

.option_parserObject



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

def self.option_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: kdep doctor [deploy]"
    opts.separator ""
    opts.separator "Runs health checks against the kdep/ directory."
    opts.separator "With a deploy name, checks only that deploy; otherwise, checks all."
    opts.separator ""
    opts.on("--fix", "Auto-fix safe issues (missing .gitignore only)")
    opts.on("--only=CHECKS", "Comma-separated check IDs to run")
    opts.on("--format=FORMAT", "Output format: text (default) or json")
  end
end

Instance Method Details

#executeObject



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
# File 'lib/kdep/commands/doctor.rb', line 28

def execute
  deploy_name = @args[0]
  discovery = Kdep::Discovery.new
  kdep_dir = discovery.find_kdep_dir

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

  deploys = deploy_name ? [deploy_name] : discovery.find_deploys
  only = (@command_options[:only] || "").split(",").map(&:strip).reject(&:empty?)
  format = @command_options[:format] || "text"

  all_results = []
  deploys.each do |name|
    deploy_dir = File.join(kdep_dir, name)
    unless File.directory?(deploy_dir)
      @ui.error("Deploy not found: #{name}")
      exit 1
    end
    Kdep::Doctor::CHECKS.each do |check_class|
      id = check_class.const_get(:ID)
      next if !only.empty? && !only.include?(id)
      result = check_class.new(deploy_dir).run
      all_results << { deploy: name, result: result }
      apply_fix(check_class, deploy_dir, result) if @command_options[:fix]
    end
  end

  if format == "json"
    puts JSON.pretty_generate(all_results.map { |r| r[:result].to_h.merge(deploy: r[:deploy]) })
  else
    render_text(all_results)
  end

  exit 1 if all_results.any? { |r| r[:result].severity == :error }
end