class Config

Attributes

file_path[R]

Public Class Methods

component_classes() click to toggle source

The mapping of component string names to class constants.

# 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
new(file_path = nil) click to toggle source

@param file_path [String] The file path to look for the configuration file.

If not provided, the default file path will be used.
# 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

Public Instance Methods

component_config(component_name) click to toggle source

Gets the configuration for a component.

@param component_name [String] the name of the component to fetch the

configuration for
# File lib/panda_motd/config.rb, line 32
def component_config(component_name)
  @config["components"][component_name.to_s]
end
components_enabled() click to toggle source

A list of enabled components' class constants.

# 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

Private Instance Methods

create_config(file_path) click to toggle source

Creates a configuration file at a given file path, from the default configuration file.

@param file_path [String] the file path at which to create the config

# File lib/panda_motd/config.rb, line 55
def create_config(file_path)
  default_config_path = File.join(
    File.dirname(__dir__), "panda_motd", "default_config.yaml"
  )
  FileUtils.cp(default_config_path, file_path)
end
load_config(file_path) click to toggle source

Loads a configuration file.

@param file_path [String] the file path of the config to load

# File lib/panda_motd/config.rb, line 65
def load_config(file_path)
  @config = YAML.safe_load(File.read(file_path))
  @config["components"] = [] if @config["components"].nil?
end