Sha256: 6a7d8607563cb2262eca35138b61997771a6436320e455082ab2215f574fb99c

Contents?: true

Size: 1.82 KB

Versions: 30

Compression:

Stored size: 1.82 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.read
      end
    end

    def convert(data)
      case self.format.downcase
      when 'json'
        require 'json'
        JSON.parse(data)
      when 'yaml', 'yml'
        require 'yaml'
        YAML.safe_load(data)
      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

30 entries across 30 versions & 1 rubygems

Version Path
kontena-cli-1.2.0.pre3 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.2.0.pre2 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.6 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.2.0.pre1 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.5 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.5.rc3 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.5.rc2 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.5.rc1 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.4 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.2.0.dev1 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.3 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.2 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.2.rc2 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.2.rc1 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.1 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.1.rc1 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.0 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.0.rc2 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.0.rc1 lib/kontena/cli/master/config/import_command.rb
kontena-cli-1.1.0.pre1 lib/kontena/cli/master/config/import_command.rb