Class: WavefrontCli::Config
- Inherits:
-
Base
- Object
- Base
- WavefrontCli::Config
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
Returns a new instance of Config.
39
40
41
42
43
|
# File 'lib/wavefront-cli/config.rb', line 39
def initialize(options)
@options = options
@config_file = _config_file
@profile = options[:'<profile>'] || 'default'
end
|
Instance Attribute Details
#config_file ⇒ Object
Returns the value of attribute config_file.
16
17
18
|
# File 'lib/wavefront-cli/config.rb', line 16
def config_file
@config_file
end
|
#profile ⇒ Object
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_file ⇒ Pathname
Returns path to config file, from options, or from a constant if not supplied.
181
182
183
|
# File 'lib/wavefront-cli/config.rb', line 181
def _config_file
Pathname.new(options[:config] || DEFAULT_CONFIG)
end
|
#base_config ⇒ Object
72
73
74
75
76
77
|
# File 'lib/wavefront-cli/config.rb', line 72
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
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/wavefront-cli/config.rb', line 92
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
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/wavefront-cli/config.rb', line 110
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
131
|
# File 'lib/wavefront-cli/config.rb', line 131
def display(_data, _method); end
|
#do_about ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/wavefront-cli/config.rb', line 58
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 ⇒ Object
106
107
108
|
# File 'lib/wavefront-cli/config.rb', line 106
def do_delete
delete_section(profile, config_file)
end
|
#do_envvars ⇒ Object
121
122
123
124
125
126
127
|
# File 'lib/wavefront-cli/config.rb', line 121
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 ⇒ Object
45
46
47
|
# File 'lib/wavefront-cli/config.rb', line 45
def do_location
puts config_file
end
|
#do_profiles ⇒ Object
49
50
51
|
# File 'lib/wavefront-cli/config.rb', line 49
def do_profiles
read_config.sections.each { |s| puts s }
end
|
#do_setup ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/wavefront-cli/config.rb', line 79
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 ⇒ Object
53
54
55
56
|
# File 'lib/wavefront-cli/config.rb', line 53
def do_show
present?
puts IO.read(config_file)
end
|
137
138
139
140
141
|
# File 'lib/wavefront-cli/config.rb', line 137
def input_prompt(label, default)
ret = format(' %<label>s', label: label)
ret.<< format(' [%<value>s]', value: default) unless default.nil?
ret + ':> '
end
|
#present? ⇒ Boolean
172
173
174
175
176
|
# File 'lib/wavefront-cli/config.rb', line 172
def present?
return true if config_file.exist?
raise WavefrontCli::Exception::ConfigFileNotFound, config_file
end
|
#read_config(_nocheck = false) ⇒ Object
185
186
187
188
|
# File 'lib/wavefront-cli/config.rb', line 185
def read_config(_nocheck = false)
present?
IniFile.load(config_file)
end
|
Read STDIN and strip the whitespace. The rescue is there to catch a ctrl-d
146
147
148
149
150
|
# File 'lib/wavefront-cli/config.rb', line 146
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
155
156
157
158
|
# File 'lib/wavefront-cli/config.rb', line 155
def read_thing(thing)
print input_prompt(thing[:text], thing[:default])
validate_input(read_input, thing[:default], thing[:test])
end
|
#run ⇒ Object
133
134
135
|
# File 'lib/wavefront-cli/config.rb', line 133
def run
dispatch
end
|
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/wavefront-cli/config.rb', line 160
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 ⇒ Object
129
|
# File 'lib/wavefront-cli/config.rb', line 129
def validate_opts; end
|