Class: KnuckleCluster::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/knuckle_cluster/configuration.rb

Constant Summary collapse

DEFAULT_PROFILE_FILE =
File.join(ENV['HOME'],'.ssh/knuckle_cluster').freeze

Class Method Summary collapse

Class Method Details

.listObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/knuckle_cluster/configuration.rb', line 42

def self.list
  data = []

  KnuckleCluster::Configuration.load_data.each do |k,v|
    tmp = {'name': k}
    tmp['tunnels'] = v['tunnels'] ? v['tunnels'].keys.join(', ') : ''
    tmp['shortcuts'] = v['shortcuts'] ? v['shortcuts'].keys.join(', ') : ''

    data << tmp
  end
  puts "\nVersion #{KnuckleCluster::VERSION}"
  puts "\nAvailable connections"
  tp data.sort{|x,y| x[:name] <=> y[:name]}
end

.load_data(profile_file = nil) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/knuckle_cluster/configuration.rb', line 34

def self.load_data(profile_file = nil)
  @data ||= (
    profile_file ||= DEFAULT_PROFILE_FILE
    raise "File #{profile_file} not found" unless File.exists?(profile_file)
    YAML.load_file(profile_file)
  )
end

.load_parameters(profile:, profile_file: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/knuckle_cluster/configuration.rb', line 5

def self.load_parameters(profile:, profile_file: nil)

  data = load_data(profile_file)

  unless data.keys.include?(profile)
    raise "Config file does not include profile for #{profile}"
  end

  #Figure out all the profiles to inherit from
  tmp_data = data[profile]
  profile_inheritance = [profile]
  while(tmp_data && tmp_data.keys.include?('profile'))
    profile_name = tmp_data['profile']
    break if profile_inheritance.include? profile_name
    profile_inheritance << profile_name
    tmp_data = data[profile_name]
  end

  #Starting at the very lowest profile, build an options hash
  output = {}
  profile_inheritance.reverse.each do |prof|
    output.merge!(data[prof] || {})
  end

  output.delete('profile')

  keys_to_symbols(output)
end