Class: Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = nil) ⇒ Config

Returns a new instance of Config.

Parameters:

  • file_path (String) (defaults to: nil)

    The file path to look for the configuration file. If not provided, the default file path will be used.



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

def initialize(file_path = nil)
  @file_path = file_path || File.join(Dir.home, ".config", "panda-motd.yaml")
  unless File.exist?(@file_path)
    create_config(@file_path)
    puts "panda-motd created a default config file at: #{@file_path}"
  end
  load_config(@file_path)
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



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

def file_path
  @file_path
end

Class Method Details

.component_classesObject

The mapping of component string names to class constants.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/panda_motd/config.rb', line 37

def self.component_classes
  {
    ascii_text_art: ASCIITextArt,
    service_status: ServiceStatus,
    uptime: Uptime,
    ssl_certificates: SSLCertificates,
    filesystems: Filesystems,
    last_login: LastLogin,
    fail_2_ban: Fail2Ban,
  }
end

Instance Method Details

#component_config(component_name) ⇒ Object

Gets the configuration for a component.

Parameters:

  • component_name (String)

    the name of the component to fetch the configuration for



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

def component_config(component_name)
  @config["components"][component_name.to_s]
end

#components_enabledObject

A list of enabled components’ class constants.



21
22
23
24
25
26
# File 'lib/panda_motd/config.rb', line 21

def components_enabled
  # iterate config hash and grab names of enabled components
  enabled = @config["components"].map { |c, s| c if s["enabled"] }.compact
  # get the class constant
  enabled.map { |e| Config.component_classes[e.to_sym] }
end