Class: SshMenu::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/sshmenu/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg_file = nil) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sshmenu/config.rb', line 11

def initialize(cfg_file = nil)
  @config_file = cfg_file || self.class.default_config_file
  create_config_file(@config_file) unless File.exist? @config_file

  config = YAML.load_file(@config_file)
  config = {} unless config

  merge_defaults config
  validate_config config
  gen_user_host config['connections']

  @general = config['general']
  @connections = config['connections']

  puts "Config is #{config.inspect}" if $DEBUG
rescue Psych::SyntaxError => e
  raise ConfigException, "YAML syntax error: #{e.message}"
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



9
10
11
# File 'lib/sshmenu/config.rb', line 9

def config_file
  @config_file
end

#connectionsObject (readonly)

Returns the value of attribute connections.



9
10
11
# File 'lib/sshmenu/config.rb', line 9

def connections
  @connections
end

#generalObject (readonly)

Returns the value of attribute general.



9
10
11
# File 'lib/sshmenu/config.rb', line 9

def general
  @general
end

Class Method Details

.default_config_fileObject



68
69
70
# File 'lib/sshmenu/config.rb', line 68

def self.default_config_file
  File.join(Dir.home, '.config', 'sshmenu', 'config.yml')
end

.default_editorObject



64
65
66
# File 'lib/sshmenu/config.rb', line 64

def self.default_editor
  ENV['EDITOR'] || 'vi'
end

.default_ssh_clientObject



60
61
62
# File 'lib/sshmenu/config.rb', line 60

def self.default_ssh_client
  'ssh'
end

Instance Method Details

#create_config_file(cfg_file) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/sshmenu/config.rb', line 40

def create_config_file(cfg_file)
  puts "Create sample config file #{cfg_file}"
  project_dir = File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))
  sample_cfg = File.join(project_dir, 'data', 'sshmenu', 'sampleconfig.yml');
  FileUtils.mkdir_p(File.dirname(cfg_file))
  FileUtils.cp(sample_cfg, cfg_file)
end

#gen_user_host(conns) ⇒ Object



54
55
56
57
58
# File 'lib/sshmenu/config.rb', line 54

def gen_user_host(conns)
  conns.each do |conn|
    conn['user_host'] = conn['user'] ? "#{conn['user']}@#{conn['host']}" : conn['host']
  end
end

#merge_defaults(config) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/sshmenu/config.rb', line 30

def merge_defaults(config)
  config['general'] ||= {}
  gen = config['general']
  gen['ssh_exec'] ||= self.class.default_ssh_client
  gen['ssh_opts'] ||= ''
  gen['editor'] ||= self.class.default_editor

  config['connections'] ||= []
end

#validate_config(config) ⇒ Object



48
49
50
51
52
# File 'lib/sshmenu/config.rb', line 48

def validate_config(config)
  unless config['connections'].all? {|conn| conn['host']}
    raise ConfigException, "invalid connection found in #{@config_file}: missing host"
  end
end