Class: Services::BusbarConfig
- Inherits:
-
Object
- Object
- Services::BusbarConfig
- Defined in:
- lib/busbar_cli/services/busbar_config.rb
Class Method Summary collapse
- .config_key_exist(config_key) ⇒ Object
- .current ⇒ Object
- .first_run ⇒ Object
- .get(config_key) ⇒ Object
- .interactive_set(config_key) ⇒ Object
- .interactive_set_all ⇒ Object
- .list_keys ⇒ Object
- .set(config_key, config_value) ⇒ Object
- .write_from_file(file_path) ⇒ Object
- .write_from_url(url) ⇒ Object
Class Method Details
.config_key_exist(config_key) ⇒ Object
10 11 12 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 10 def config_key_exist(config_key) Helpers::BusbarConfig::CONFIG_OPTIONS.key? config_key.to_sym end |
.current ⇒ Object
61 62 63 64 65 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 61 def current puts puts 'Current Busbar configuration:' File.open(BUSBAR_CONFIG_FILE_PATH, 'r') { |f| puts f.read } end |
.first_run ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 18 def first_run @first_run = true selector_position = ARGV.count - 2 if (ARGV[selector_position] == '-a') || (ARGV[selector_position] == '--i-set-all') Helpers::BusbarConfig.ensure_dependencies Helpers::BusbarConfig.create_empty_config_file interactive_set_all current elsif (ARGV[selector_position] == '-f') || (ARGV[selector_position] == '--file') || \ (ARGV[selector_position] =~ /--file.*$/) file_path = if ARGV[selector_position].include?('=') ARGV[selector_position].split('=')[1] else ARGV[selector_position + 1] end write_from_file(file_path) current elsif (ARGV[selector_position] == '-u') || (ARGV[selector_position] == '--url') || \ (ARGV[selector_position] =~ /--url.*$/) url = if ARGV[selector_position].include?('=') ARGV[selector_position].split('=')[1] else ARGV[selector_position + 1] end write_from_url(url) current else puts puts 'Busbar Config file not found!' puts puts 'Current Options:' puts ' -a, [--i-set-all] # Set all configuration keys interactivelly' puts ' -f, [--file=FILE] # Create the busbar config using an external file' puts ' -u, [--url=URL] # Create the busbar config using an external URL' puts end exit(0) end |
.get(config_key) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 67 def get(config_key) return unless config_key_exist(config_key) = File.open(BUSBAR_CONFIG_FILE_PATH, 'r') = YAML.safe_load() .close [config_key] end |
.interactive_set(config_key) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 99 def interactive_set(config_key) return unless Helpers::BusbarConfig::CONFIG_OPTIONS.key? config_key.to_sym thor_ask = Thor::Shell::Basic.new proceed = nil until proceed == 'Yes' exit(0) if proceed == 'No' puts config_value = thor_ask.ask(Helpers::BusbarConfig::CONFIG_OPTIONS[config_key.to_sym][:text], default: Helpers::BusbarConfig::CONFIG_OPTIONS[config_key.to_sym][:default]) puts puts "The busbar config key '#{config_key}' will be set with the value '#{config_value}'" puts proceed = thor_ask.ask('Proceed', default: 'Yes', limited_to: %w(Yes No Retry)) end set(config_key, config_value) end |
.interactive_set_all ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 119 def interactive_set_all thor_ask = Thor::Shell::Basic.new = {} proceed = nil until proceed == 'Yes' exit(0) if proceed == 'No' puts Helpers::BusbarConfig::CONFIG_OPTIONS.each do |config_key, _| config_value = thor_ask.ask(Helpers::BusbarConfig::CONFIG_OPTIONS[config_key][:text], default: Helpers::BusbarConfig::CONFIG_OPTIONS[config_key][:default]) [config_key.to_s] = config_value end puts puts 'The Busbar config file will be created with the options bellow:' puts puts .to_yaml puts proceed = thor_ask.ask('Proceed', default: 'Yes', limited_to: %w(Yes No Retry)) end Helpers::BusbarConfig.write_from_hash(, @first_run) end |
.list_keys ⇒ Object
14 15 16 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 14 def list_keys Helpers::BusbarConfig::CONFIG_OPTIONS.keys end |
.set(config_key, config_value) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 75 def set(config_key, config_value) return unless config_key_exist(config_key) Services::Kube.validate_profile(config_value) if config_key == 'busbar_profile' unless @first_run Helpers::BusbarConfig.ensure_dependencies = File.open(BUSBAR_CONFIG_FILE_PATH, 'r+') = YAML.safe_load() [config_key.to_s] = config_value File.open(BUSBAR_CONFIG_FILE_PATH, 'w') { |f| f.write(.to_yaml) } config_value end |
.write_from_file(file_path) ⇒ Object
86 87 88 89 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 86 def write_from_file(file_path) Helpers::BusbarConfig.ensure_dependencies FileUtils.copy(File.(file_path), BUSBAR_CONFIG_FILE_PATH) end |
.write_from_url(url) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/busbar_cli/services/busbar_config.rb', line 91 def write_from_url(url) Helpers::BusbarConfig.ensure_dependencies response = Net::HTTP.get(URI(url)) open(BUSBAR_CONFIG_FILE_PATH, 'wb') do |file| file.write(response) end end |