Module: Hackmac::Config

Extended by:
ComplexConfig::Provider::Shortcuts, FileUtils, Term::ANSIColor
Defined in:
lib/hackmac/config.rb

Overview

A module that provides configuration loading and management functionality for Hackmac

The Config module handles the initialization and retrieval of Hackmac’s configuration settings from specified files or default locations. It manages configuration directories, creates default configuration files when needed, and loads configurations into memory for use throughout the application.

Examples:

Hackmac::Config.load
# Loads the default configuration file
Hackmac::Config.load(path: '/custom/config/path.yml')
# Loads a custom configuration file from the specified path

Constant Summary collapse

DEFAULT =
File.read(File.join(File.dirname(__FILE__), 'hackmac.yml'))

Class Method Summary collapse

Class Method Details

.load(path: ENV.fetch('HACKMAC_CONFIG', '~/.config/hackmac/hackmac.yml')) ⇒ ComplexConfig::Provider

Loads the Hackmac configuration from the specified path or default location

This method initializes the configuration system by reading from a specified file path or using the default configuration file location. It ensures the configuration directory exists, sets up the configuration provider’s directory, creates a default configuration file if one doesn’t exist, and loads the configuration into memory for use throughout the application.

Parameters:

  • path (String) (defaults to: ENV.fetch('HACKMAC_CONFIG', '~/.config/hackmac/hackmac.yml'))

    the file path to load the configuration from, defaults to the value of the HACKMAC_CONFIG environment variable or the default location ~/.config/hackmac/hackmac.yml

Returns:

  • (ComplexConfig::Provider)

    the loaded configuration provider object that can be used to access configuration values



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

def self.load(path: ENV.fetch('HACKMAC_CONFIG', '~/.config/hackmac/hackmac.yml'))
  path = Pathname.new(path).expand_path
  mkdir_p path.dirname
  ComplexConfig::Provider.config_dir = path.dirname
  unless path.exist?
    File.secure_write(path.to_s, DEFAULT)
  end
  puts "Loading config from #{color(33) { path.to_s.inspect} }."
  ComplexConfig::Provider[path.basename.to_s.sub(/\..*?\z/, '')]
end