Class: CodexNotify::TrustedConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/codex_notify/trusted_config_loader.rb

Defined Under Namespace

Classes: ConfigFile, Error

Constant Summary collapse

DEFAULT_RELATIVE_PATH =
Pathname('codex-notify/config.yml')
TOP_LEVEL_KEYS =
%w[env_policy default_destination destinations].freeze
DESTINATION_KEYS =
%w[token channel].freeze
ENV_POLICIES =
%w[legacy restricted].freeze

Instance Method Summary collapse

Constructor Details

#initialize(environment: ENV, home: nil, stderr: $stderr) ⇒ TrustedConfigLoader

Returns a new instance of TrustedConfigLoader.



19
20
21
22
23
# File 'lib/codex_notify/trusted_config_loader.rb', line 19

def initialize(environment: ENV, home: nil, stderr: $stderr)
  @environment = environment
  @home = home
  @stderr = stderr
end

Instance Method Details

#default_pathObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/codex_notify/trusted_config_loader.rb', line 35

def default_path
  xdg_home = environment['XDG_CONFIG_HOME']
  if xdg_home && !xdg_home.empty?
    base = Pathname(xdg_home)
    raise Error, 'XDG_CONFIG_HOME must be an absolute path' unless base.absolute?

    return base.join(DEFAULT_RELATIVE_PATH)
  end

  home_path = home || environment['HOME']
  home_path = Dir.home if !home_path || home_path.empty?
  Pathname(home_path).expand_path.join('.config', DEFAULT_RELATIVE_PATH)
rescue ArgumentError, SystemCallError => e
  raise Error, "could not resolve the home configuration directory: #{e.class}"
end

#load(explicit_path: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/codex_notify/trusted_config_loader.rb', line 25

def load(explicit_path: nil)
  files = []
  explicit = explicit_path && Pathname(explicit_path).expand_path
  files << load_file(explicit, kind: :config_explicit, required: true) if explicit

  default = default_path
  files << load_file(default, kind: :config, required: false) unless explicit == default
  files.compact
end