Module: KubeManifest::CLI::Utils

Included in:
Exec
Defined in:
lib/kube_manifest/cli.rb

Instance Method Summary collapse

Instance Method Details

#expand_dir(filename) ⇒ Object



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

def expand_dir(filename)
  return nil unless filename
  return nil unless File.exist? filename

  if File.directory? filename
    return Pathname.new(filename).expand_path.to_s
  elsif File.file? filename
    return Pathname.new(File.dirname(filename)).expand_path.to_s
  end

  nil
end

#merge_hash_recursive(lh, rh) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kube_manifest/cli.rb', line 51

def merge_hash_recursive(lh, rh)
  result = lh.inject({}) { |init, (k, v)| init[k] = v; init; }
  rh.each_pair do |key, value|
    merged = if value.is_a?(Hash) && lh[key].is_a?(Hash) && rh[key].is_a?(Hash)
               merge_hash_recursive(lh[key], rh[key])
             else
               value
             end
    result[key] = merged
  end
  result
end

#symbolize_keys(hash) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kube_manifest/cli.rb', line 10

def symbolize_keys(hash)
  result = {}
  hash.each_pair do |key, value|
    v = if value.is_a? Hash
          symbolize_keys(value)
        elsif value.is_a? Array
          value.map { |v| symbolize_keys(v) }
        else
          value
        end
    result[key.to_sym] = v
  end
  result
end

#transform_to_hash(kvs) ⇒ Object



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
# File 'lib/kube_manifest/cli.rb', line 25

def transform_to_hash(kvs)
  result = {}
  (kvs || []).each do |v|
    pair = v.split('=')
    keys = pair.first.split('.')

    if keys.length == 1
      result[keys.first] = pair[1..-1].join('=')
    else
      child_keys = pair[0].split('.')
      converted = {}
      (child_keys.size - 1).downto(1) do |index|
        key = child_keys[index]
        if index == child_keys.size - 1
          converted = {key => pair[1..-1].join('=')}
        else
          converted = {key => converted}
        end
      end

      result[keys.first] = converted
    end
  end
  result
end