Class: HammerCLI::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/settings.rb

Constant Summary collapse

CFG_PATH =
['~/.hammer/', '/etc/hammer/', "#{::RbConfig::CONFIG['sysconfdir']}/hammer/"].uniq
CFG_PATH_LOCAL =
['./config/']

Class Method Summary collapse

Class Method Details

.clearObject



55
56
57
58
# File 'lib/hammer_cli/settings.rb', line 55

def self.clear
  empty
  load(default_settings)
end

.default_settingsObject



74
75
76
77
78
79
# File 'lib/hammer_cli/settings.rb', line 74

def self.default_settings
  {
    :use_defaults => true,
    :completion_cache_file => '~/.cache/hammer_completion.json'
  }
end

.dumpObject



65
66
67
# File 'lib/hammer_cli/settings.rb', line 65

def self.dump
  settings
end

.emptyObject



60
61
62
63
# File 'lib/hammer_cli/settings.rb', line 60

def self.empty
  settings.clear
  path_history.clear
end

.get(*keys) ⇒ Object



10
11
12
13
14
15
# File 'lib/hammer_cli/settings.rb', line 10

def self.get(*keys)
  keys.inject(settings) do |value, key|
    return nil unless value
    value[key.to_sym]
  end
end

.load(settings_hash) ⇒ Object



51
52
53
# File 'lib/hammer_cli/settings.rb', line 51

def self.load(settings_hash)
  deep_merge!(settings, settings_hash)
end

.load_from_defaultsObject



46
47
48
49
# File 'lib/hammer_cli/settings.rb', line 46

def self.load_from_defaults
  load_from_paths CFG_PATH
  load_from_paths CFG_PATH_LOCAL
end

.load_from_file(file_path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hammer_cli/settings.rb', line 32

def self.load_from_file(file_path)
  if File.file? file_path
    begin
      config = YAML::load(File.open(file_path))
      if config
        load(config)
        path_history << file_path
      end
    rescue Exception => e
      warn _("Warning: Couldn't load configuration file %{path}: %{message}.") % { path: file_path, message: e.message }
    end
  end
end

.load_from_paths(files) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hammer_cli/settings.rb', line 17

def self.load_from_paths(files)
  files.reverse.each do |path|
    full_path = File.expand_path path
    if File.directory? full_path
      # check for cli_config.yml
      load_from_file(File.join(full_path, 'cli_config.yml'))
      load_from_file(File.join(full_path, 'defaults.yml'))
      # load config for modules
      Dir.glob(File.join(full_path, 'cli.modules.d/*.yml')).sort.each do |f|
        load_from_file(f)
      end
    end
  end
end

.path_historyObject



69
70
71
72
# File 'lib/hammer_cli/settings.rb', line 69

def self.path_history
  @path_history ||= []
  @path_history
end