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}$/.freeze

Constants included from Constants

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

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_response_blocks, #check_status, #cli_output_class, #conds_to_query, #descriptive_name, #dispatch, #display_api_error, #display_class, #display_no_api_response, #do_describe, #do_dump, #do_import, #do_list, #do_search, #do_set, #do_undelete, #dump_json, #dump_yaml, #extract_values, #failed_validation_message, #format_var, #handle_error, #handle_response, #hcl_fields, #import_to_create, #item_dump_call, #load_display_class, #matching_method, #method_word_list, #mk_creds, #mk_opts, #name_of_do_method, #no_api_response, #ok_exit, #one_or_all, #options_and_exit, #parseable_output, #range_hash, #require_sdk_class, #search_key, #smart_delete, #smart_delete_message, #status_error_handler, #unsupported_format_message, #validate_id, #validate_tags, #validator_exception, #validator_method, #warning_message

Constructor Details

#initialize(options) ⇒ Config

rubocop:disable Lint/MissingSuper



40
41
42
43
44
# File 'lib/wavefront-cli/config.rb', line 40

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.



16
17
18
# File 'lib/wavefront-cli/config.rb', line 16

def config_file
  @config_file
end

#profileObject (readonly)

Returns the value of attribute profile.



16
17
18
# File 'lib/wavefront-cli/config.rb', line 16

def profile
  @profile
end

Instance Method Details

#_config_filePathname

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

Returns:

  • (Pathname)

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



183
184
185
# File 'lib/wavefront-cli/config.rb', line 183

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

#base_configObject



74
75
76
77
78
79
# 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) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# 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) ⇒ Object



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

def display(_data, _method); end

#do_aboutObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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_deleteObject



108
109
110
# File 'lib/wavefront-cli/config.rb', line 108

def do_delete
  delete_section(profile, config_file)
end

#do_envvarsObject



123
124
125
126
127
128
129
# 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_locationObject

rubocop:enable Lint/MissingSuper



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

def do_location
  puts config_file
end

#do_profilesObject



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

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

#do_setupObject



81
82
83
84
85
86
87
88
89
90
91
92
# 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_showObject



55
56
57
58
# File 'lib/wavefront-cli/config.rb', line 55

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

#input_prompt(label, default) ⇒ Object



139
140
141
142
143
# 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?Boolean

Returns:

  • (Boolean)

Raises:



174
175
176
177
178
# 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) ⇒ Object



187
188
189
190
# File 'lib/wavefront-cli/config.rb', line 187

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



148
149
150
151
152
# 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) ⇒ String

Read something, and return its checked, sanitized value

Returns:



157
158
159
160
# 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

#runObject



135
136
137
# File 'lib/wavefront-cli/config.rb', line 135

def run
  dispatch
end

#validate_input(input, default, test) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# 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_optsObject



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

def validate_opts; end