Class: WavefrontCli::Config

Inherits:
Base
  • Object
show all
Defined in:
lib/wavefront-cli/config.rb

Overview

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.

Constant Summary collapse

CONFIGURABLES =
{ key: :token,
    text: 'Wavefront API token',
    default: nil,
    test: proc { |v| v =~ RX } },
  { key: :endpoint,
    text: 'Wavefront API endpoint',
    default: 'metrics.wavefront.com',
    test: proc { |v| v.end_with?('.wavefront.com') } },
  { key: :proxy,
    text: 'Wavefront proxy endpoint',
    default: 'wavefront',
    test: proc { true } },
  { key: :format,
    text: 'default output format',
    default: 'human',
    test: proc { |v| %w[human json yaml].include?(v) } }
].freeze
RX =
/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/

Constants included from Constants

WavefrontCli::Constants::ALL_PAGE_SIZE, WavefrontCli::Constants::DEFAULT_CONFIG, WavefrontCli::Constants::DEFAULT_OPTS, WavefrontCli::Constants::HUMAN_TIME_FORMAT, WavefrontCli::Constants::HUMAN_TIME_FORMAT_MS

Instance Attribute Summary collapse

Attributes inherited from Base

#klass, #klass_word, #options, #wf

Instance Method Summary collapse

Methods inherited from Base

#_sdk_class, #cannot_noop!, #check_status, #conds_to_query, #dispatch, #display_api_error, #display_no_api_response, #do_describe, #do_import, #do_list, #do_search, #do_undelete, #do_update, #extract_values, #failed_validation_message, #format_var, #handle_error, #handle_response, #hcl_fields, #import_to_create, #load_display_class, #load_file, #load_from_stdin, #mk_creds, #mk_opts, #no_api_response, #ok_exit, #one_or_all, #options_and_exit, #parseable_output, #range_hash, #search_key, #smart_delete, #smart_delete_message, #validate_id, #validate_tags, #validator_exception, #validator_method

Constructor Details

#initialize(options) ⇒ Config

Returns a new instance of Config.



37
38
39
40
41
# File 'lib/wavefront-cli/config.rb', line 37

def initialize(options)
  @options = options
  @config_file = _config_file
  @profile = options[:'<profile>'] || 'default'
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



14
15
16
# File 'lib/wavefront-cli/config.rb', line 14

def config_file
  @config_file
end

#profileObject (readonly)

Returns the value of attribute profile.



14
15
16
# File 'lib/wavefront-cli/config.rb', line 14

def profile
  @profile
end

Instance Method Details

#_config_filePathname

Returns path to config file, from options, or from a constant if not supplied.

Returns:

  • path to config file, from options, or from a constant if not supplied.



170
171
172
# File 'lib/wavefront-cli/config.rb', line 170

def _config_file
  Pathname.new(options[:config] || DEFAULT_CONFIG)
end

#base_configObject



70
71
72
73
74
75
# File 'lib/wavefront-cli/config.rb', line 70

def base_config
  return read_config if config_file.exist?

  puts "Creating new configuration file at #{config_file}."
  IniFile.new
end

#create_profile(profile) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/wavefront-cli/config.rb', line 90

def create_profile(profile)
  puts "Creating profile '#{profile}'."

  str = CONFIGURABLES.each_with_object("[#{profile}]") do |t, a|
    a.<< format("\n%s=%s", t[:key], read_thing(t))
  end

  IniFile.new(content: str)
end

#delete_section(profile, file) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/wavefront-cli/config.rb', line 104

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) ⇒ Object



123
# File 'lib/wavefront-cli/config.rb', line 123

def display(_data, _method); end

#do_aboutObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wavefront-cli/config.rb', line 56

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_deleteObject



100
101
102
# File 'lib/wavefront-cli/config.rb', line 100

def do_delete
  delete_section(profile, config_file)
end

#do_envvarsObject



115
116
117
118
119
# File 'lib/wavefront-cli/config.rb', line 115

def do_envvars
  %w[WAVEFRONT_ENDPOINT WAVEFRONT_TOKEN WAVEFRONT_PROXY].each do |v|
    puts format('%-20s %s', v, ENV[v] || 'unset')
  end
end

#do_locationObject



43
44
45
# File 'lib/wavefront-cli/config.rb', line 43

def do_location
  puts config_file
end

#do_profilesObject



47
48
49
# File 'lib/wavefront-cli/config.rb', line 47

def do_profiles
  read_config.sections.each { |s| puts s }
end

#do_setupObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wavefront-cli/config.rb', line 77

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_showObject



51
52
53
54
# File 'lib/wavefront-cli/config.rb', line 51

def do_show
  present?
  puts IO.read(config_file)
end

#input_prompt(label, default) ⇒ Object



129
130
131
132
133
# File 'lib/wavefront-cli/config.rb', line 129

def input_prompt(label, default)
  ret = format('  %s', label)
  ret.<< format(' [%s]', default) unless default.nil?
  ret + ':> '
end

#present?Boolean

Returns:

Raises:



162
163
164
165
# File 'lib/wavefront-cli/config.rb', line 162

def present?
  return true if config_file.exist?
  raise WavefrontCli::Exception::ConfigFileNotFound, config_file
end

#read_config(_nocheck = false) ⇒ Object



174
175
176
177
# File 'lib/wavefront-cli/config.rb', line 174

def read_config(_nocheck = false)
  present?
  IniFile.load(config_file)
end

#read_inputObject

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



138
139
140
141
142
# File 'lib/wavefront-cli/config.rb', line 138

def read_input
  STDIN.gets.strip
rescue NoMethodError
  abort "\nInput aborted at user request."
end

#read_thing(thing) ⇒ String

Read something, and return its checked, sanitized value

Returns:



147
148
149
150
# File 'lib/wavefront-cli/config.rb', line 147

def read_thing(thing)
  print input_prompt(thing[:text], thing[:default])
  validate_input(read_input, thing[:default], thing[:test])
end

#runObject



125
126
127
# File 'lib/wavefront-cli/config.rb', line 125

def run
  dispatch
end

#validate_input(input, default, test) ⇒ Object

Raises:



152
153
154
155
156
157
158
159
160
# File 'lib/wavefront-cli/config.rb', line 152

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_optsObject



121
# File 'lib/wavefront-cli/config.rb', line 121

def validate_opts; end