class KubeManifest::CLI::Exec
Attributes
cwd[RW]
filenames[RW]
options[RW]
values[RW]
Public Class Methods
new(options: {}, values: {}, filenames: [])
click to toggle source
# File lib/kube_manifest/cli.rb, line 83 def initialize(options: {}, values: {}, filenames: []) @options = options @values = values @filenames = filenames @cwd = nil end
run(filenames, values, mixin: nil, cwd: nil, verbose: false)
click to toggle source
# File lib/kube_manifest/cli.rb, line 159 def self.run(filenames, values, mixin: nil, cwd: nil, verbose: false) if mixin.is_a? String mixin = if Pathname.new(mixin).absolute? mixin else Pathname.new(File.join(Dir.pwd, mixin)).expand_path.to_s end end if verbose STDERR.write "# Loading #{mixin}\n" STDERR.flush end KubeManifest::Runner.load_mixin!(mixin) collected = filenames.inject([]) do |result, filename| if filename == '-' result << filename else filename = if Pathname.new(filename).absolute? filename else Pathname.new(File.join(Dir.pwd, filename)).expand_path.to_s end if File.directory? filename result << Dir["#{filename}/*.rb"] elsif File.exists? filename result << filename end end result end.flatten.uniq.reject{ |f| f == mixin } if verbose STDERR.write "# Collected #{collected.join(',')}\n" STDERR.flush end collected.inject([]) do |result, filename| if filename == '-' ctx = KubeManifest::Runner.new(STDIN.read, values: values).ctx result << ctx else file = File.open(filename) STDERR.write "# Processing #{filename}\n" if verbose ctx = KubeManifest::Runner.new(file.read, values: values, cwd: [cwd, File.dirname(filename)]).ctx if ctx.is_a? Array ctx.each do |m| result << m end elsif ctx result << ctx end end result end end
Public Instance Methods
parse_options!()
click to toggle source
# File lib/kube_manifest/cli.rb, line 90 def parse_options! parser = OptionParser.new do |opts| opts.banner = "Usage: rkube-manifest template [options] filename" opts.on('--set KEY=VALUE', String, 'Set values') do |v| @options[:values] ||= [] @options[:values] << v end opts.on('-f VALUE_FILE', '--values VALUE_FILE', String, 'Read values from a YAML file and override') do |v| @options[:values_file] = v end opts.on('-v', '--[no-]verbose', 'Run verbosely. The log would be be write into stderr') do |v| @options[:verbose] = v end opts.on('-m METHODS_FILE', '--methods METHODS_FILE', String, 'Import methods from a given file') do |v| @options[:method_file] = v end opts.on("-h", "--help", "Prints this help") do puts opts exit end end parser.parse! command = ARGV.shift if command != 'template' STDERR.write "Error: unknown command: #{command}\n" STDERR.write parser.banner STDERR.write "\n" exit 1 end @filenames = ARGV || [] if @filenames.empty? STDERR.write "Error: File or directory not given\n" STDERR.write parser.banner STDERR.write "\n" exit 1 end end
run()
click to toggle source
# File lib/kube_manifest/cli.rb, line 135 def run self.load_values! mixin = if @options[:method_file] && File.exist?(@options[:method_file]) @options[:method_file] else nil end if @options[:verbose] STDERR.write "# Processing manifests within #{@cwd} with values:\n" STDERR.write "#{@values.pretty_inspect.gsub(/^/, '# ')}" STDERR.flush end self.class.run(@filenames, @values, cwd: @cwd, mixin: mixin, verbose: @options[:verbose]) end
run!()
click to toggle source
# File lib/kube_manifest/cli.rb, line 151 def run! manifests = run STDOUT.write manifests.map{|m|m.as_yaml}.join("\n") rescue STDERR.write "Error: #{$!.message}\n" exit 2 end
Protected Instance Methods
load_values!()
click to toggle source
# File lib/kube_manifest/cli.rb, line 220 def load_values! if @filenames.size == 1 @cwd = expand_dir(@filenames.first) end @cwd ||= Dir.pwd values = {} %w(values.yml values.yaml).each do |f| filename = File.join(@cwd, f) if File.exists? filename begin values = YAML.load(File.open(filename).read) STDERR.write "# Reading values from #{filename}\n" if @options[:verbose] rescue values = {} end end end if @options[:values_file] file = File.open(@options[:values_file]) @cwd = expand_dir(file) || Dir.pwd begin yaml_values = YAML.load(file.read) STDERR.write "# Reading values from #{Pathname.new(file).expand_path.to_s}\n" if @options[:verbose] rescue yaml_values = {} end values = merge_hash_recursive(values, yaml_values) end opt_values = transform_to_hash(@options[:values]) values = merge_hash_recursive(values, opt_values) @values = symbolize_keys(values) end