Class: Constancy::Config

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

Constant Summary collapse

CONFIG_FILENAMES =
%w( constancy.yml )
VALID_CONFIG_KEYS =
%w( sync consul vault constancy )
VALID_CONSUL_CONFIG_KEYS =
%w( url datacenter token_source )
VALID_VAULT_CONFIG_KEYS =
%w( url consul_token_path consul_token_field )
VALID_CONSTANCY_CONFIG_KEYS =
%w( verbose chomp delete color )
DEFAULT_CONSUL_URL =
"http://localhost:8500"
DEFAULT_CONSUL_TOKEN_SOURCE =
"none"
DEFAULT_VAULT_CONSUL_TOKEN_FIELD =
"token"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, targets: nil, call_external_apis: true) ⇒ Config

Returns a new instance of Config.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/constancy/config.rb', line 39

def initialize(path: nil, targets: nil, call_external_apis: true)
  if path.nil? or File.directory?(path)
    self.config_file = Constancy::Config.discover(dir: path)
  elsif File.exist?(path)
    self.config_file = path
  else
    raise Constancy::ConfigFileNotFound.new
  end

  if self.config_file.nil? or not File.exist?(self.config_file) or not File.readable?(self.config_file)
    raise Constancy::ConfigFileNotFound.new
  end

  self.config_file = File.expand_path(self.config_file)
  self.base_dir = File.dirname(self.config_file)
  self.target_whitelist = targets
  self.call_external_apis = call_external_apis
  parse!
end

Instance Attribute Details

#base_dirObject

Returns the value of attribute base_dir.



21
22
23
# File 'lib/constancy/config.rb', line 21

def base_dir
  @base_dir
end

#call_external_apisObject

Returns the value of attribute call_external_apis.



21
22
23
# File 'lib/constancy/config.rb', line 21

def call_external_apis
  @call_external_apis
end

#config_fileObject

Returns the value of attribute config_file.



21
22
23
# File 'lib/constancy/config.rb', line 21

def config_file
  @config_file
end

#consulObject

Returns the value of attribute consul.



21
22
23
# File 'lib/constancy/config.rb', line 21

def consul
  @consul
end

#consul_token_sourceObject

Returns the value of attribute consul_token_source.



21
22
23
# File 'lib/constancy/config.rb', line 21

def consul_token_source
  @consul_token_source
end

#sync_targetsObject

Returns the value of attribute sync_targets.



21
22
23
# File 'lib/constancy/config.rb', line 21

def sync_targets
  @sync_targets
end

#target_whitelistObject

Returns the value of attribute target_whitelist.



21
22
23
# File 'lib/constancy/config.rb', line 21

def target_whitelist
  @target_whitelist
end

#vault_configObject

Returns the value of attribute vault_config.



21
22
23
# File 'lib/constancy/config.rb', line 21

def vault_config
  @vault_config
end

Class Method Details

.discover(dir: nil) ⇒ Object

discover the nearest config file



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/constancy/config.rb', line 25

def discover(dir: nil)
  dir ||= Dir.pwd

  CONFIG_FILENAMES.each do |filename|
    full_path = File.join(dir, filename)
    if File.exist?(full_path)
      return full_path
    end
  end

  dir == "/" ? nil : self.discover(dir: File.dirname(dir))
end

Instance Method Details

#chomp?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/constancy/config.rb', line 63

def chomp?
  @do_chomp
end

#color?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/constancy/config.rb', line 71

def color?
  @use_color
end

#delete?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/constancy/config.rb', line 67

def delete?
  @do_delete
end

#verbose?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/constancy/config.rb', line 59

def verbose?
  @is_verbose
end