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.
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::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
#cannot_noop!, #check_response_blocks, #check_status, #cli_output_class, #conds_to_query, #descriptive_name, #dispatch, #display, #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, #initialize, #item_dump_call, #load_display_class, #matching_method, #method_word_list, #mk_creds, #mk_opts, #name_of_do_method, #ok_exit, #one_or_all, #options_and_exit, #parseable_output, #range_hash, #require_sdk_class, #run, #search_key, #smart_delete, #smart_delete_message, #status_error_handler, #unsupported_format_message, #validate_id, #validate_input, #validate_tags, #validator_exception, #validator_method, #warning_message
Instance Attribute Details
#config_file ⇒ Object
Returns the value of attribute config_file.
12
13
14
|
# File 'lib/wavefront-cli/config.rb', line 12
def config_file
@config_file
end
|
#profile ⇒ Object
Returns the value of attribute profile.
12
13
14
|
# File 'lib/wavefront-cli/config.rb', line 12
def profile
@profile
end
|
Instance Method Details
#_config_file ⇒ Pathname
Returns path to config file, from options, or from a constant if not supplied.
179
180
181
|
# File 'lib/wavefront-cli/config.rb', line 179
def _config_file
Pathname.new(options[:config] || DEFAULT_CONFIG)
end
|
#_sdk_class ⇒ Object
40
41
42
|
# File 'lib/wavefront-cli/config.rb', line 40
def _sdk_class
'Wavefront::Cluster'
end
|
#base_config ⇒ Object
68
69
70
71
72
73
|
# File 'lib/wavefront-cli/config.rb', line 68
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
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/wavefront-cli/config.rb', line 88
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
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/wavefront-cli/config.rb', line 106
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
|
#do_about ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/wavefront-cli/config.rb', line 57
def do_about
require 'wavefront-sdk/defs/version'
{ '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 }
end
|
#do_cluster ⇒ Object
123
124
125
|
# File 'lib/wavefront-cli/config.rb', line 123
def do_cluster
wf.describe
end
|
#do_delete ⇒ Object
102
103
104
|
# File 'lib/wavefront-cli/config.rb', line 102
def do_delete
delete_section(profile, config_file)
end
|
#do_envvars ⇒ Object
117
118
119
120
121
|
# File 'lib/wavefront-cli/config.rb', line 117
def do_envvars
%w[WAVEFRONT_ENDPOINT WAVEFRONT_TOKEN WAVEFRONT_PROXY].map do |v|
format('%-20<var>s %<value>s', var: v, value: ENV[v] || 'unset')
end
end
|
#do_location ⇒ Object
44
45
46
|
# File 'lib/wavefront-cli/config.rb', line 44
def do_location
config_file
end
|
#do_profiles ⇒ Object
48
49
50
|
# File 'lib/wavefront-cli/config.rb', line 48
def do_profiles
read_config.sections.sort
end
|
#do_setup ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/wavefront-cli/config.rb', line 75
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
52
53
54
55
|
# File 'lib/wavefront-cli/config.rb', line 52
def do_show
present?
File.read(config_file)
end
|
def display(_data, _method); end
135
136
137
138
139
|
# File 'lib/wavefront-cli/config.rb', line 135
def input_prompt(label, default)
ret = format(' %<label>s', label: label)
ret << format(' [%<value>s]', value: default) unless default.nil?
"#{ret}:> "
end
|
#no_api_response ⇒ Object
129
130
131
|
# File 'lib/wavefront-cli/config.rb', line 129
def no_api_response
%w[do_location do_profiles do_show do_envvars do_about]
end
|
#post_initialize(options) ⇒ Object
35
36
37
38
|
# File 'lib/wavefront-cli/config.rb', line 35
def post_initialize(options)
@config_file = _config_file
@profile = options[:'<profile>'] || 'default'
end
|
#read_config(_nocheck = false) ⇒ Object
183
184
185
186
|
# File 'lib/wavefront-cli/config.rb', line 183
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
144
145
146
147
148
|
# File 'lib/wavefront-cli/config.rb', line 144
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
153
154
155
156
|
# File 'lib/wavefront-cli/config.rb', line 153
def read_thing(thing)
print input_prompt(thing[:text], thing[:default])
validate_thing_input(read_input, thing[:default], thing[:test])
end
|
#validate_opts ⇒ Object
127
|
# File 'lib/wavefront-cli/config.rb', line 127
def validate_opts; end
|