class KongSchema::CLI

Public Instance Methods

run(argv) click to toggle source
Calls superclass method
# File lib/kong_schema/cli.rb, line 12
def run(argv)
  program_desc 'Configure Kong from file.'

  version KongSchema::VERSION

  sort_help :manually

  flag([ 'c', 'config' ], {
    desc: 'Path to the configuration file (in place of the first argument.)',
    arg_name: 'FILE'
  })

  desc 'Apply configuration from a .yml or .json file.'
  arg(:config_file)
  command :up do |c|
    c.flag([ 'c', 'config' ], {
      desc: 'Path to the configuration file (in place of the first argument.)',
      arg_name: 'FILE'
    })

    c.flag([ 'k', 'key' ], {
      default_value: 'kong',
      desc: 'The root configuration property key.',
      arg_name: 'NAME'
    })

    c.flag([ 'f', 'format' ], {
      default_value: 'json',
      desc: 'Format to use for reporting objects. Either "json" or "yaml".',
      long_desc: 'Available formats: "json" or "yaml".',
      arg_name: 'FORMAT',
      must_match: %w(json yaml)
    })

    c.switch([ 'confirm' ], {
      default_value: true,
      desc: 'Prompt for confirmation before applying changes.'
    })

    c.action do |globals, options, args|
      filepath = resolve_config_file!(args: args, globals: globals, options: options)

      up(filepath: filepath, options: options)
    end
  end

  desc 'Reset Kong configuration completely.'
  arg(:config_file)
  command :down do |c|
    c.flag([ 'k', 'key' ], {
      default_value: 'kong',
      desc: 'The root configuration property key.',
      arg_name: 'NAME'
    })

    c.switch([ 'confirm' ], {
      default_value: true,
      desc: 'Prompt for confirmation before applying changes.'
    })

    c.action do |globals, options, args|
      filepath = resolve_config_file!(args: args, globals: globals, options: options)

      down(filepath: filepath, options: options)
    end
  end

  super(argv)
end

Private Instance Methods

bail!(reason) click to toggle source
# File lib/kong_schema/cli.rb, line 158
def bail!(reason)
  help_now! red("✘ #{reason}")
end
down(filepath:, options:) click to toggle source
# File lib/kong_schema/cli.rb, line 104
def down(filepath:, options:)
  pastel = Pastel.new
  schema = KongSchema::Schema
  config = read_property(load_file(filepath), options[:key])

  if !options[:confirm] || yes?("You are about to completely reset Kong's database. Proceed?")
    KongSchema::Client.purge(config)

    puts "#{green('✓')} Kong reset."
  end
end
green(text) click to toggle source
# File lib/kong_schema/cli.rb, line 150
def green(text)
  pastel.green(text)
end
load_file(filepath) click to toggle source
# File lib/kong_schema/cli.rb, line 126
def load_file(filepath)
  if filepath.end_with?('.json')
    JSON.parse(File.read(filepath))
  else
    YAML.load_file(filepath)
  end
end
pastel() click to toggle source
# File lib/kong_schema/cli.rb, line 154
def pastel
  @pastel ||= Pastel.new
end
read_property(config, key) click to toggle source
# File lib/kong_schema/cli.rb, line 134
def read_property(config, key)
  if key.to_s.empty?
    config
  else
    config.fetch(key.to_s)
  end
end
red(text) click to toggle source
# File lib/kong_schema/cli.rb, line 146
def red(text)
  pastel.red(text)
end
resolve_config_file!(args:, globals:, options:) click to toggle source
# File lib/kong_schema/cli.rb, line 116
def resolve_config_file!(args:, globals:, options:)
  filepath = args.first || options[:config] || globals[:config]

  if filepath.nil?
    bail! "Missing path to .yml or .json config file"
  else
    filepath
  end
end
up(filepath:, options:) click to toggle source
# File lib/kong_schema/cli.rb, line 84
def up(filepath:, options:)
  pastel = Pastel.new
  schema = KongSchema::Schema
  config = read_property(load_file(filepath), options[:key])

  schema.scan(config).tap do |changes|
    if changes.empty?
      puts "#{green('✓')} Nothing to update."
    else
      puts KongSchema::Reporter.report(changes, object_format: options[:format].to_sym)

      if !options[:confirm] || yes?('Commit the changes to Kong?')
        schema.commit(config, changes)

        puts "#{green('✓')} Kong has been reconfigured!"
      end
    end
  end
end
yes?(message, default: false) click to toggle source
# File lib/kong_schema/cli.rb, line 142
def yes?(message, default: false)
  TTY::Prompt.new.yes?(message, default: default, color: false)
end