Class: Agentd::Config

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

Overview

Loads config from ~/.agentd/config.json Expected format: { “api_key”: “…”, “endpoint”: “…” }

Constant Summary collapse

CONFIG_PATH =
File.expand_path("~/.agentd/config.json")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Config

Returns a new instance of Config.



11
12
13
14
# File 'lib/agentd/config.rb', line 11

def initialize(data = {})
  @api_key  = data["api_key"]
  @endpoint = data["endpoint"]
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/agentd/config.rb', line 9

def api_key
  @api_key
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



9
10
11
# File 'lib/agentd/config.rb', line 9

def endpoint
  @endpoint
end

Class Method Details

.loadObject



16
17
18
19
20
21
# File 'lib/agentd/config.rb', line 16

def self.load
  return new unless File.exist?(CONFIG_PATH)
  new(JSON.parse(File.read(CONFIG_PATH)))
rescue JSON::ParserError
  new
end

.save(api_key:, endpoint: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/agentd/config.rb', line 23

def self.save(api_key:, endpoint: nil)
  dir = File.dirname(CONFIG_PATH)
  FileUtils.mkdir_p(dir)
  existing = load
  data = {
    "api_key"  => api_key || existing.api_key,
    "endpoint" => endpoint || existing.endpoint
  }.compact
  File.write(CONFIG_PATH, JSON.pretty_generate(data))
end