Class: Bastion::Config

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

Overview

Config class

Constant Summary collapse

USER =
"#{ENV['HOME']}/.bastion/config"
SYSTEM =
"/etc/bastion/config"
ENVVAR =
"BASTION_HOST"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
15
16
17
# File 'lib/bastion/config.rb', line 10

def initialize(options)
  @system = read(SYSTEM)
  @user   = read(USER)
  @env    = ENV[ENVVAR]
  @option = options[:host]
  @host   = @option || @env || @user || @system
  save @host if options[:save]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



40
41
42
# File 'lib/bastion/config.rb', line 40

def method_missing(name)
  instance_variable_get :"@#{name}"
end

Instance Method Details

#infoObject



19
20
21
22
23
24
25
# File 'lib/bastion/config.rb', line 19

def info
  puts "System : #{@system}"
  puts "User   : #{@user}"
  puts "Env    : #{@env}"
  puts "Option : #{@option}"
  puts "Using  : #{@host}"
end

#read(file, default = nil) ⇒ Object



27
28
29
30
31
# File 'lib/bastion/config.rb', line 27

def read(file, default = nil)
  return default unless File.exist?(file)
  value = File.read(file)
  value.empty? ? nil : value
end

#save(value) ⇒ Object



33
34
35
36
37
38
# File 'lib/bastion/config.rb', line 33

def save(value)
  dir = File.dirname(USER)
  Dir.mkdir(dir) unless Dir.exist?(dir)
  File.write USER, value
  File.read USER
end