Sha256: 3b34b36c419a44cb4cc6ef0d22d65c57f12ef3de986e94489a941a61fe06c7a9

Contents?: true

Size: 1.87 KB

Versions: 42

Compression:

Stored size: 1.87 KB

Contents

module Kontena::Cli::Master::Config
  class ImportCommand < Kontena::Command

    include Kontena::Cli::Common

    requires_current_master
    requires_current_master_token

    banner "Updates configuration from a file into Master"

    parameter '[PATH]', "Input from file in PATH (default: STDIN)", required: false

    option ['--preset'], '[NAME]', 'Load preset', hidden: true

    option ['--format'], '[FORMAT]', "Specify input format (json, yaml) (default: guess from PATH or json)"
    option ['--full'], :flag, "Perform full update, keys that are not present in the input are cleared"
    option ['-f', '--force'], :flag, "Don't ask for confirmation"


    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

    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

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

    def upload(data)
      confirm unless self.force?
      client.send(http_method, "config", data)
    end

    def set_default_format
      self.format ||= self.path.to_s.end_with?('.yml') ? 'yaml' : 'json'
    end

    def execute
      set_default_format
      upload(convert(input_as_hash))
    end
  end
end

Version data entries

42 entries across 42 versions & 2 rubygems

Version Path
krates-1.7.11 lib/kontena/cli/master/config/import_command.rb
krates-1.7.10 lib/kontena/cli/master/config/import_command.rb
krates-1.7.9 lib/kontena/cli/master/config/import_command.rb
krates-1.7.8 lib/kontena/cli/master/config/import_command.rb
krates-1.7.7 lib/kontena/cli/master/config/import_command.rb
krates-1.7.6 lib/kontena/cli/master/config/import_command.rb
krates-1.7.5 lib/kontena/cli/master/config/import_command.rb
krates-1.7.4 lib/kontena/cli/master/config/import_command.rb
krates-1.7.3 lib/kontena/cli/master/config/import_command.rb
krates-1.7.2 lib/kontena/cli/master/config/import_command.rb
krates-1.7.1 lib/kontena/cli/master/config/import_command.rb
krates-1.7.0 lib/kontena/cli/master/config/import_command.rb
krates-1.6.9 lib/kontena/cli/master/config/import_command.rb
krates-1.6.8 lib/kontena/cli/master/config/import_command.rb
krates-1.6.7 lib/kontena/cli/master/config/import_command.rb
krates-1.6.6 lib/kontena/cli/master/config/import_command.rb
krates-1.6.5 lib/kontena/cli/master/config/import_command.rb
krates-1.6.4 lib/kontena/cli/master/config/import_command.rb
krates-1.6.3 lib/kontena/cli/master/config/import_command.rb
krates-1.6.2 lib/kontena/cli/master/config/import_command.rb