Class: KubeDeployTools::Templater::Optparser::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/kube_deploy_tools/templater/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



14
15
16
17
18
19
20
21
# File 'lib/kube_deploy_tools/templater/options.rb', line 14

def initialize
  # Values available in the ERB template to be merged.
  # Avoid errors by forcing explicit checks for keys. With this, config[]
  # indirection requires the key to exist.
  self.values = {}
  self.values_from_yaml = {}
  self.values_from_flags = {}
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/kube_deploy_tools/templater/options.rb', line 8

def output
  @output
end

#templateObject

Returns the value of attribute template.



8
9
10
# File 'lib/kube_deploy_tools/templater/options.rb', line 8

def template
  @template
end

#valuesObject

Returns the value of attribute values.



8
9
10
# File 'lib/kube_deploy_tools/templater/options.rb', line 8

def values
  @values
end

#values_from_flagsObject

Returns the value of attribute values_from_flags.



8
9
10
# File 'lib/kube_deploy_tools/templater/options.rb', line 8

def values_from_flags
  @values_from_flags
end

#values_from_yamlObject

Returns the value of attribute values_from_yaml.



8
9
10
# File 'lib/kube_deploy_tools/templater/options.rb', line 8

def values_from_yaml
  @values_from_yaml
end

Instance Method Details

#define_options(parser) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kube_deploy_tools/templater/options.rb', line 23

def define_options(parser)
  parser.on('-tFILEPATH', '--template FILEPATH', 'The template file FILEPATH') do |f|
    self.template = f
  end

  parser.on('-vFILENAME', '--values FILENAME', 'Set template variables from the values in a YAML file, FILENAME') do |f|
    raise "Cannot find --values FILENAME '#{f}'" unless File.file?(f)
    self.values_from_yaml = YAML::load(File.read(f))
  end

  parser.on('-sKEY=VALUE', '--set KEY=VALUE', 'Set a template variable with KEY=VALUE') do |kv|
    raise "Cannot parse --set KEY=VALUE '#{kv}'" unless kv.include? '='
    k, v = *kv.split("=")
    self.values_from_flags[k] = v
  end

  parser.on('-oFILEPATH', '--output FILEPATH', 'Set the output file FILEPATH. Default output is to stdout.') do |f|
    self.output = f
  end
end

#merge_valuesObject



51
52
53
54
55
56
57
58
59
# File 'lib/kube_deploy_tools/templater/options.rb', line 51

def merge_values
  # merge values from yaml
  self.values = self.values.merge(self.values_from_yaml)

  # merge values from flags
  self.values = self.values.merge(self.values_from_flags)

  warn 'Warning: No values provided from --values, --set' unless ! self.values.empty?
end

#require_optionsObject



44
45
46
47
48
49
# File 'lib/kube_deploy_tools/templater/options.rb', line 44

def require_options
  raise 'Must provide --template' unless template.present?
  raise "Cannot find --template FILEPATH '#{template}'" unless File.file?(template)
  raise "Expected --template FILEPATH '#{template}' to end with .yaml.erb" unless template.end_with?(".yaml.erb")
  raise "Expected --output FILEPATH to be a new file location" unless output.blank? || !File.file?(output) || File.directory?(output)
end