Class: Agentilda::Config

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

Overview

Default option overrides, read from ~/.local/config/agentilda.json.

The file is keyed by command, so it can grow without every command seeing every key:

{
"run": { "timeout": 1800, "jobs": 4 }
}

Precedence is: an option passed on the command line, then this file, then the built-in default. The file supplies defaults, never mandates — nothing here can force --commit, which stays a flag a person types.

An unreadable file is an error, not a shrug: a config silently ignored is how a timeout somebody set stops applying with no signal anywhere.

Constant Summary collapse

PATH =
File.join(Dir.home, ".local", "config", "agentilda.json")

Class Method Summary collapse

Class Method Details

.for(command, path: PATH) ⇒ Hash{Symbol => Object}

Returns that command's overrides, {} when the file or the section is absent.

Parameters:

  • command (Symbol, String)

    the command whose section to read

  • path (String) (defaults to: PATH)

    the config file

Returns:

  • (Hash{Symbol => Object})

    that command's overrides, {} when the file or the section is absent

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/agentilda/config.rb', line 30

def self.for(command, path: PATH)
  return {} unless File.file?(path)

  data = JSON.parse(File.read(path))
  raise Error, "#{path}: expected a JSON object keyed by command" unless data.is_a?(Hash)

  section = data.fetch(command.to_s, {})
  raise Error, "#{path}: \"#{command}\" must be a JSON object" unless section.is_a?(Hash)

  section.transform_keys(&:to_sym)
rescue JSON::ParserError => e
  raise Error, "#{path} is not valid JSON: #{e.message.lines.first.to_s.strip}"
end