Class: KubeManifest::CLI::Exec

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/kube_manifest/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#expand_dir, #merge_hash_recursive, #symbolize_keys, #transform_to_hash

Constructor Details

#initialize(options: {}, values: {}, filenames: []) ⇒ Exec

Returns a new instance of Exec.



83
84
85
86
87
88
# File 'lib/kube_manifest/cli.rb', line 83

def initialize(options: {}, values: {}, filenames: [])
  @options = options
  @values = values
  @filenames = filenames
  @cwd = nil
end

Instance Attribute Details

#cwdObject

Returns the value of attribute cwd.



81
82
83
# File 'lib/kube_manifest/cli.rb', line 81

def cwd
  @cwd
end

#filenamesObject

Returns the value of attribute filenames.



81
82
83
# File 'lib/kube_manifest/cli.rb', line 81

def filenames
  @filenames
end

#optionsObject

Returns the value of attribute options.



81
82
83
# File 'lib/kube_manifest/cli.rb', line 81

def options
  @options
end

#valuesObject

Returns the value of attribute values.



81
82
83
# File 'lib/kube_manifest/cli.rb', line 81

def values
  @values
end

Class Method Details

.run(filenames, values, mixin: nil, cwd: nil, verbose: false) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# 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

Instance Method Details

#parse_options!Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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

#runObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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!Object



151
152
153
154
155
156
157
# 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