Class: KubeDeployTools::Templater::Optparser::Options
- Defined in:
- lib/kube_deploy_tools/templater/options.rb
Instance Attribute Summary collapse
-
#output ⇒ Object
Returns the value of attribute output.
-
#template ⇒ Object
Returns the value of attribute template.
-
#values ⇒ Object
Returns the value of attribute values.
-
#values_from_flags ⇒ Object
Returns the value of attribute values_from_flags.
-
#values_from_yaml ⇒ Object
Returns the value of attribute values_from_yaml.
Instance Method Summary collapse
- #define_options(parser) ⇒ Object
-
#initialize ⇒ Options
constructor
A new instance of Options.
- #merge_values ⇒ Object
- #require_options ⇒ Object
Constructor Details
#initialize ⇒ Options
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
#output ⇒ Object
Returns the value of attribute output.
8 9 10 |
# File 'lib/kube_deploy_tools/templater/options.rb', line 8 def output @output end |
#template ⇒ Object
Returns the value of attribute template.
8 9 10 |
# File 'lib/kube_deploy_tools/templater/options.rb', line 8 def template @template end |
#values ⇒ Object
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_flags ⇒ Object
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_yaml ⇒ Object
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 (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_values ⇒ Object
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_options ⇒ Object
44 45 46 47 48 49 |
# File 'lib/kube_deploy_tools/templater/options.rb', line 44 def 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 |