Class: Exa::CLI::ConfigStore

Inherits:
Object
  • Object
show all
Defined in:
lib/exa/cli/config_store.rb

Defined Under Namespace

Classes: UnknownAccountError

Constant Summary collapse

DEFAULT_RELATIVE_PATH =
File.join(".config", "exa", "config.yml")
DEFAULT_DATA =
{
  "version" => 1,
  "accounts" => {},
  "default" => nil
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, env: ENV) ⇒ ConfigStore

Returns a new instance of ConfigStore.



20
21
22
23
# File 'lib/exa/cli/config_store.rb', line 20

def initialize(path: nil, env: ENV)
  @env = env
  @path = path || env["EXA_CONFIG_PATH"] || File.join(config_dir(env), "config.yml")
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/exa/cli/config_store.rb', line 18

def path
  @path
end

Instance Method Details

#readObject



25
26
27
28
29
30
31
32
# File 'lib/exa/cli/config_store.rb', line 25

def read
  if File.exist?(path)
    data = safe_load(File.read(path))
    normalize_data(data)
  else
    default_data
  end
end

#remove_account(name) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/exa/cli/config_store.rb', line 44

def (name)
  data = read
  removed = data["accounts"].delete(name)
  return false unless removed

  data["default"] = nil if data["default"] == name
  write(data)
  true
end

#set_default(name) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/exa/cli/config_store.rb', line 54

def set_default(name)
  data = read
  unless data["accounts"].key?(name)
    raise UnknownAccountError, "Account #{name.inspect} not found"
  end

  data["default"] = name
  write(data)
end

#upsert_account(name, api_key:, base_url:, make_default: true) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/exa/cli/config_store.rb', line 34

def (name, api_key:, base_url:, make_default: true)
  data = read
  data["accounts"][name] = {
    "api_key" => api_key,
    "base_url" => base_url
  }.compact
  data["default"] ||= name if make_default
  write(data)
end