Class: Motoko::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

def initialize
  @columns = %w[host customer role]
  @sort_by = %w[customer host]
  @shortcuts = Hash.new { {} }
  @columns_spec = Hash.new { {} }.merge(default_columns_spec)

  load_system_config
  load_user_config
  load_project_config
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



7
8
9
# File 'lib/motoko/config.rb', line 7

def columns
  @columns
end

#columns_specObject

Returns the value of attribute columns_spec.



7
8
9
# File 'lib/motoko/config.rb', line 7

def columns_spec
  @columns_spec
end

#shortcutsObject

Returns the value of attribute shortcuts.



7
8
9
# File 'lib/motoko/config.rb', line 7

def shortcuts
  @shortcuts
end

#sort_byObject

Returns the value of attribute sort_by.



7
8
9
# File 'lib/motoko/config.rb', line 7

def sort_by
  @sort_by
end

Instance Method Details

#default_columns_specObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/motoko/config.rb', line 64

def default_columns_spec
  YAML.safe_load("    ---\n    host:\n      resolver: identity\n    customer:\n      formatter: ellipsis\n      max_length: 20\n    cpu:\n      resolver: cpu\n    os:\n      resolver: os\n      human_name: Operating System\n    reboot_required:\n      resolver: reboot_required\n      formatter: boolean\n      human_name: R\n  COLUMNS_SPEC\nend\n")

#load_classes(directory) ⇒ Object



58
59
60
61
62
# File 'lib/motoko/config.rb', line 58

def load_classes(directory)
  Dir["#{directory}/formatters/*.rb", "#{directory}/resolvers/*.rb"].sort.each do |file|
    require file
  end
end

#load_config(directory) ⇒ Object



41
42
43
44
# File 'lib/motoko/config.rb', line 41

def load_config(directory)
  load_classes(directory)
  load_only_config(File.join(directory, 'config.yaml'))
end

#load_only_config(filename) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/motoko/config.rb', line 46

def load_only_config(filename)
  return unless File.readable?(filename)

  config = YAML.safe_load(File.read(filename))

  @columns = config.delete('columns') if config.key?('columns')
  @sort_by = config.delete('sort_by') if config.key?('sort_by')

  @shortcuts.merge!(config.delete('shortcuts')) if config.key?('shortcuts')
  @columns_spec.merge!(config.delete('columns_spec')) if config.key?('columns_spec')
end

#load_project_configObject



37
38
39
# File 'lib/motoko/config.rb', line 37

def load_project_config
  load_only_config('.motoko.yaml')
end

#load_system_configObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/motoko/config.rb', line 20

def load_system_config
  [
    '/usr/local/etc/motoko',
    '/etc/motoko',
  ].each do |d|
    if File.directory?(d)
      load_config(d)
      break
    end
  end
end

#load_user_configObject



32
33
34
35
# File 'lib/motoko/config.rb', line 32

def load_user_config
  d = File.expand_path('~/.config/motoko')
  load_config(d) if File.directory?(d)
end