Class: Kamal::Lint::Checks::MissingServiceName

Inherits:
Kamal::Lint::Check show all
Defined in:
lib/kamal/lint/checks/missing_service_name.rb

Instance Attribute Summary

Attributes inherited from Kamal::Lint::Check

#context

Instance Method Summary collapse

Methods inherited from Kamal::Lint::Check

applies_to?, autofixable, id, #initialize, severity, since, title, until_version

Constructor Details

This class inherits a constructor from Kamal::Lint::Check

Instance Method Details

#apply_fix(ctx) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kamal/lint/checks/missing_service_name.rb', line 24

def apply_fix(ctx)
  file = ctx.file_for_finding
  text = File.read(file)
  parsed = YAML.safe_load(text, aliases: true) || {}
  return false if parsed["service"].is_a?(String) && !parsed["service"].empty?

  name = File.basename(ctx.working_dir).gsub(/[^A-Za-z0-9_-]/, "-")
  return false if name.empty?

  # Parse-and-dump so the fix composes safely with other autofixes
  # that may also rewrite the file. The trade-off is that comments
  # in the original YAML are lost — documented in the README.
  File.write(file, YAML.dump({ "service" => name }.merge(parsed)))
  true
rescue
  false
end

#callObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/kamal/lint/checks/missing_service_name.rb', line 13

def call
  service = parsed["service"]
  return [] if service.is_a?(String) && !service.strip.empty?

  [ finding(
    message: "`service:` is required; without it Kamal can't name the deployed container",
    line: 1,
    autofix: method(:apply_fix)
  ) ]
end