Class: Services::BusbarConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/busbar_cli/services/busbar_config.rb

Class Method Summary collapse

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

.currentObject



50
51
52
53
54
# File 'lib/busbar_cli/services/busbar_config.rb', line 50

def current
  puts
  puts 'Current Busbar configuration:'
  File.open(BUSBAR_CONFIG_FILE_PATH, 'r') { |f| puts f.read }
end

.first_runObject



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
# 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

  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
  end
  exit(0)
end

.get(config_key) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/busbar_cli/services/busbar_config.rb', line 56

def get(config_key)
  return unless config_key_exist(config_key)
  busbar_config_file = File.open(BUSBAR_CONFIG_FILE_PATH, 'r')
  busbar_config_hash = YAML.safe_load(busbar_config_file)
  busbar_config_file.close
  busbar_config_hash[config_key]
end

.interactive_set(config_key) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/busbar_cli/services/busbar_config.rb', line 80

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_allObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/busbar_cli/services/busbar_config.rb', line 100

def interactive_set_all
  thor_ask = Thor::Shell::Basic.new
  busbar_config_hash = {}
  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])
      busbar_config_hash[config_key.to_s] = config_value
    end

    puts
    puts 'The Busbar config file will be created with the options bellow:'
    puts
    puts busbar_config_hash.to_yaml
    puts
    proceed = thor_ask.ask('Proceed', default: 'Yes', limited_to: %w(Yes No Retry))
  end

  Helpers::BusbarConfig.write_from_hash(busbar_config_hash, @first_run)
end

.list_keysObject



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



64
65
66
67
68
69
70
71
72
73
# File 'lib/busbar_cli/services/busbar_config.rb', line 64

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
  busbar_config_file = File.open(BUSBAR_CONFIG_FILE_PATH, 'r+')
  busbar_config_hash = YAML.safe_load(busbar_config_file)
  busbar_config_hash[config_key.to_s] = config_value
  File.open(BUSBAR_CONFIG_FILE_PATH, 'w') { |f| f.write(busbar_config_hash.to_yaml) }
  config_value
end

.write_from_file(file_path) ⇒ Object



75
76
77
78
# File 'lib/busbar_cli/services/busbar_config.rb', line 75

def write_from_file(file_path)
  Helpers::BusbarConfig.ensure_dependencies
  FileUtils.copy(File.expand_path(file_path), BUSBAR_CONFIG_FILE_PATH)
end