Module: KubeManifest::SpecUtils

Included in:
Spec
Defined in:
lib/kube_manifest/spec.rb

Class Method Summary collapse

Class Method Details

.child_node(field_name, field_type) ⇒ Object



58
59
60
61
62
63
# File 'lib/kube_manifest/spec.rb', line 58

def self.child_node(field_name, field_type)
  lambda do |*args, **kwargs, &blk|
    value = ::KubeManifest::Describe.new_value(self, field_type, *args, **kwargs, &blk)
    self.instance_variable_set("@#{field_name}", value)
  end
end

.child_value(field_name) ⇒ Object



92
93
94
95
96
# File 'lib/kube_manifest/spec.rb', line 92

def self.child_value(field_name)
  lambda do |value|
    instance_variable_set("@#{field_name}", value)
  end
end

.children_node(field_name, field_type) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kube_manifest/spec.rb', line 65

def self.children_node(field_name, field_type)
  lambda do |*args, **kwargs, &blk|
    if instance_variable_get("@#{field_name}").nil?
      instance_variable_set("@#{field_name}", [])
    end
    value = ::KubeManifest::Describe.new_value(self, field_type, *args, **kwargs, &blk)
    if value.is_a? Array
      instance_variable_get("@#{field_name}").concat(value)
    else
      instance_variable_get("@#{field_name}") << value
    end
  end
end

.children_value(field_name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kube_manifest/spec.rb', line 79

def self.children_value(field_name)
  lambda do |value|
    if instance_variable_get("@#{field_name}").nil?
      instance_variable_set("@#{field_name}", [])
    end
    if value.is_a? Array
      instance_variable_get("@#{field_name}").concat(value)
    else
      instance_variable_get("@#{field_name}") << value
    end
  end
end

.included(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/kube_manifest/spec.rb', line 9

def self.included(base)
  base.class_eval do
    protected

    def to_json(value, pretty: false)
      if pretty
        JSON.pretty_generate(value)
      else
        JSON.generate(value)
      end
    end

    def file(filename, rstrip: true)
      dir = []
      dir.concat(@_ctx.cwd || []) if @_ctx
      dir << '.'

      dir.each do |d|
        path = File.join(d, filename)
        next unless File.exists? path
        f = File.open(path).read
        if rstrip
          return f.rstrip
        end
        return f
      end

      nil
    end

    def b64encode(value)
      Base64.urlsafe_encode64(value)
    end

    def manifest(value)
      ctx = ::KubeManifest::Runner.new(file(value), values: self._values, cwd: self._ctx.cwd).ctx
      ctx.as_yaml
    end

    def sha256(value)
      Digest::SHA2.new(256).hexdigest(value)
    end

    def md5(value)
      Digest::MD5.hexdigest(value)
    end
  end
end