Class: Kontena::Cli::Master::Config::ImportCommand

Inherits:
Kontena::Command
  • Object
show all
Includes:
Common
Defined in:
lib/kontena/cli/master/config/import_command.rb

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Methods inherited from Kontena::Command

banner, callback_matcher, #help_requested?, inherited, #instance, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#convert(data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# 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

#executeObject



62
63
64
65
# File 'lib/kontena/cli/master/config/import_command.rb', line 62

def execute
  set_default_format
  upload(convert(input_as_hash))
end

#http_methodObject



49
50
51
# File 'lib/kontena/cli/master/config/import_command.rb', line 49

def http_method
  self.full? ? :patch : :put
end

#input_as_hashObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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_formatObject



58
59
60
# 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) ⇒ Object



53
54
55
56
# 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