Module: Kdep::State

Defined in:
lib/kdep/state.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

FILENAME =
"state.yml".freeze
INITIAL_TAG =
"0.0".freeze
HEADER =
<<~HEADER
  # Last tag deployed by kdep bump -- do not edit by hand.
  # This file is safe to commit. See: https://github.com/repleadfy/kdep
HEADER

Class Method Summary collapse

Class Method Details

.exists?(deploy_dir) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/kdep/state.rb', line 71

def self.exists?(deploy_dir)
  File.exist?(File.join(deploy_dir, FILENAME))
end

.load(deploy_dir) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/kdep/state.rb', line 15

def self.load(deploy_dir)
  path = File.join(deploy_dir, FILENAME)
  return nil unless File.exist?(path)
  data = YAML.safe_load(File.read(path)) || {}
  unless data.is_a?(Hash) && data["tag"]
    raise Error, "#{path}: missing required key 'tag'"
  end
  data
end

.parseable_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/kdep/state.rb', line 75

def self.parseable_tag?(tag)
  require "kdep/version_tagger"
  !!Kdep::VersionTagger.parse(tag)
end

.tag(deploy_dir) ⇒ Object



25
26
27
28
# File 'lib/kdep/state.rb', line 25

def self.tag(deploy_dir)
  loaded = load(deploy_dir)
  loaded && loaded["tag"]
end

.write(deploy_dir, tag:) ⇒ Object

Rewrites only the tag: line, keeping everything else byte-for-byte. The header says "do not edit by hand", but state.yml is the one place a warning sits next to the data it guards -- e.g. a target that shares an image repo with another and must never be re-seeded with --init-state. Rebuilding the file from scratch silently ate those comments on every bump, and a YAML round-trip would too (the parser doesn't carry them).



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kdep/state.rb', line 36

def self.write(deploy_dir, tag:)
  path = File.join(deploy_dir, FILENAME)
  tmp  = "#{path}.tmp"

  content = if File.exist?(path)
              with_header(replace_tag_line(File.read(path), tag))
            else
              "#{HEADER}tag: \"#{tag}\"\n"
            end

  File.write(tmp, content)
  File.rename(tmp, path)
  path
end