Class: MywayConfig::Loaders::XdgConfigLoader

Inherits:
Anyway::Loaders::Base
  • Object
show all
Defined in:
lib/myway_config/loaders/xdg_config_loader.rb

Overview

XDG Base Directory Specification loader for Anyway Config

Loads configuration from XDG-compliant paths:

  1. $XDG_CONFIG_HOME/app/app.yml (if XDG_CONFIG_HOME is set)
  2. ~/.config/app/app.yml (XDG default fallback)

On macOS, also checks: 3. ~/Library/Application Support/app/app.yml

This loader runs BEFORE the project-local config loader, so project configs take precedence over user-global configs.

Examples:

XDG config file location

~/.config/myapp/myapp.yml

Custom XDG_CONFIG_HOME

export XDG_CONFIG_HOME=/my/config
# Looks for /my/config/myapp/myapp.yml

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config_paths(name) ⇒ Array<String>

Returns all XDG config paths to check for a given app name

Parameters:

  • name (String)

    the application name

Returns:

  • (Array<String>)

    list of potential config directory paths



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/myway_config/loaders/xdg_config_loader.rb', line 33

def config_paths(name)
  paths = []

  # macOS Application Support (lowest priority for XDG loader)
  if macos?
    macos_path = File.expand_path("~/Library/Application Support/#{name}")
    paths << macos_path if Dir.exist?(File.dirname(macos_path))
  end

  # XDG default: ~/.config/{name}
  xdg_default = File.expand_path("~/.config/#{name}")
  paths << xdg_default

  # XDG_CONFIG_HOME override (highest priority for XDG loader)
  if ENV['XDG_CONFIG_HOME'] && !ENV['XDG_CONFIG_HOME'].empty?
    xdg_home = File.join(ENV['XDG_CONFIG_HOME'], name.to_s)
    paths << xdg_home unless xdg_home == xdg_default
  end

  paths
end

.find_config_file(name) ⇒ String?

Find the first existing config file for the given app name

Parameters:

  • name (String)

    config name (e.g., 'myapp')

Returns:

  • (String, nil)

    path to config file or nil if not found



59
60
61
62
63
64
65
# File 'lib/myway_config/loaders/xdg_config_loader.rb', line 59

def find_config_file(name)
  config_paths(name).reverse_each do |dir|
    file = File.join(dir, "#{name}.yml")
    return file if File.exist?(file)
  end
  nil
end

Instance Method Details

#call(name:, **_options) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/myway_config/loaders/xdg_config_loader.rb', line 74

def call(name:, **_options)
  config_file = self.class.find_config_file(name)
  return {} unless config_file

  trace!(:xdg, path: config_file) do
    load_yaml(config_file, name)
  end
end