class WavefrontCli::Config

Create and manage a local configuration file. This class doesn't fit many of the assumptions made by the Base class. (Primarily, that it will consume the SDK.) Rather than split everything up, we're going to do some bad programming and override a couple of methods in the parent class to force different behaviour.

Constants

CONFIGURABLES
RX

Attributes

config_file[R]
profile[R]

Public Class Methods

new(options) click to toggle source

rubocop:disable Lint/MissingSuper

# File lib/wavefront-cli/config.rb, line 40
def initialize(options)
  @options = options
  @config_file = _config_file
  @profile = options[:'<profile>'] || 'default'
end

Public Instance Methods

_config_file() click to toggle source

@return [Pathname] path to config file, from options, or from

a constant if not supplied.
# File lib/wavefront-cli/config.rb, line 183
def _config_file
  Pathname.new(options[:config] || DEFAULT_CONFIG)
end
base_config() click to toggle source
# File lib/wavefront-cli/config.rb, line 74
def base_config
  return read_config if config_file.exist?

  puts "Creating new configuration file at #{config_file}."
  IniFile.new
end
create_profile(profile) click to toggle source
# File lib/wavefront-cli/config.rb, line 94
def create_profile(profile)
  puts "Creating profile '#{profile}'."

  prof_arr = ["[#{profile}]"]

  CONFIGURABLES.each do |c|
    prof_arr.<< format('%<key>s=%<value>s',
                       key: c[:key],
                       value: read_thing(c))
  end

  IniFile.new(content: prof_arr.join("\n"))
end
delete_section(profile, file) click to toggle source
# File lib/wavefront-cli/config.rb, line 112
def delete_section(profile, file)
  raw = read_config

  unless raw.has_section?(profile)
    raise(WavefrontCli::Exception::ProfileNotFound, profile)
  end

  raw.delete_section(profile)
  raw.write(filename: file)
end
display(_data, _method) click to toggle source
# File lib/wavefront-cli/config.rb, line 133
def display(_data, _method); end
do_about() click to toggle source
# File lib/wavefront-cli/config.rb, line 60
def do_about
  require 'wavefront-sdk/defs/version'
  require_relative 'display/base'

  info = { 'wf version': WF_CLI_VERSION,
           'wf path': CMD_PATH.realpath.to_s,
           'SDK version': WF_SDK_VERSION,
           'SDK location': WF_SDK_LOCATION.to_s,
           'Ruby version': RUBY_VERSION,
           'Ruby platform': Gem::Platform.local.os.capitalize }

  WavefrontDisplay::Base.new(info).long_output
end
do_delete() click to toggle source
# File lib/wavefront-cli/config.rb, line 108
def do_delete
  delete_section(profile, config_file)
end
do_envvars() click to toggle source
# File lib/wavefront-cli/config.rb, line 123
def do_envvars
  %w[WAVEFRONT_ENDPOINT WAVEFRONT_TOKEN WAVEFRONT_PROXY].each do |v|
    puts format('%-20<var>s %<value>s',
                var: v,
                value: ENV[v] || 'unset')
  end
end
do_location() click to toggle source

rubocop:enable Lint/MissingSuper

# File lib/wavefront-cli/config.rb, line 47
def do_location
  puts config_file
end
do_profiles() click to toggle source
# File lib/wavefront-cli/config.rb, line 51
def do_profiles
  read_config.sections.each { |s| puts s }
end
do_setup() click to toggle source
# File lib/wavefront-cli/config.rb, line 81
def do_setup
  config = base_config

  if config.has_section?(profile)
    raise(WavefrontCli::Exception::ProfileExists, profile)
  end

  new_section = create_profile(profile)

  config = config.merge(new_section)
  config.write(filename: config_file)
end
do_show() click to toggle source
# File lib/wavefront-cli/config.rb, line 55
def do_show
  present?
  puts IO.read(config_file)
end
input_prompt(label, default) click to toggle source
# File lib/wavefront-cli/config.rb, line 139
def input_prompt(label, default)
  ret = format('  %<label>s', label: label)
  ret.<< format(' [%<value>s]', value: default) unless default.nil?
  ret + ':> '
end
present?() click to toggle source
# File lib/wavefront-cli/config.rb, line 174
def present?
  return true if config_file.exist?

  raise WavefrontCli::Exception::ConfigFileNotFound, config_file
end
read_config(_nocheck = false) click to toggle source
# File lib/wavefront-cli/config.rb, line 187
def read_config(_nocheck = false)
  present?
  IniFile.load(config_file)
end
read_input() click to toggle source

Read STDIN and strip the whitespace. The rescue is there to catch a ctrl-d

# File lib/wavefront-cli/config.rb, line 148
def read_input
  $stdin.gets.strip
rescue NoMethodError
  abort "\nInput aborted at user request."
end
read_thing(thing) click to toggle source

Read something, and return its checked, sanitized value @return [String]

# File lib/wavefront-cli/config.rb, line 157
def read_thing(thing)
  print input_prompt(thing[:text], thing[:default])
  validate_input(read_input, thing[:default], thing[:test])
end
run() click to toggle source
# File lib/wavefront-cli/config.rb, line 135
def run
  dispatch
end
validate_input(input, default, test) click to toggle source
# File lib/wavefront-cli/config.rb, line 162
def validate_input(input, default, test)
  if input.empty?
    raise WavefrontCli::Exception::MandatoryValue if default.nil?

    input = default
  end

  return input if test.call(input)

  raise WavefrontCli::Exception::InvalidValue
end
validate_opts() click to toggle source
# File lib/wavefront-cli/config.rb, line 131
def validate_opts; end