class Kontena::Cli::Master::Config::ImportCommand

Public Instance Methods

convert(data) click to toggle source
# File lib/kontena/cli/master/config/import_command.rb, line 37
def convert(data)
  case self.format.downcase
  when 'json'
    require 'json'
    JSON.parse(data)
  when 'yaml', 'yml'
    ::YAML.safe_load(data, [], [], true)
  else
    exit_with_error "Unknown input format '#{self.format}'"
  end
end
execute() click to toggle source
# File lib/kontena/cli/master/config/import_command.rb, line 62
def execute
  set_default_format
  upload(convert(input_as_hash))
end
http_method() click to toggle source
# File lib/kontena/cli/master/config/import_command.rb, line 49
def http_method
  self.full? ? :patch : :put
end
input_as_hash() click to toggle source
# File lib/kontena/cli/master/config/import_command.rb, line 20
def input_as_hash
  if self.path && self.preset
    exit_with_error "Options --preset and PATH can not be used together"
  elsif self.path
    unless File.exist?(self.path) && File.readable?(self.path)
      exit_with_error "Can not read '#{self.path}'"
    end
    File.read(self.path)
  elsif self.preset
    self.format = 'yaml'
    path = File.join(Kontena.root, 'lib/kontena/presets', "#{self.preset}.yml")
    File.read(path)
  else
    stdin_input("Enter master configuration as #{format.upcase}", :multiline)
  end
end
set_default_format() click to toggle source
# File lib/kontena/cli/master/config/import_command.rb, line 58
def set_default_format
  self.format ||= self.path.to_s.end_with?('.yml') ? 'yaml' : 'json'
end
upload(data) click to toggle source
# File lib/kontena/cli/master/config/import_command.rb, line 53
def upload(data)
  confirm unless self.force?
  client.send(http_method, "config", data)
end