Class: Explore::Cli::ConfigStore

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

Constant Summary collapse

DEFAULT_RELATIVE_PATH =
".config/explore/credentials.json"
CURRENT_VERSION =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ConfigStore.



12
13
14
15
# File 'lib/explore/cli/config_store.rb', line 12

def initialize(env:, path: nil)
  @env = env
  @path = path || env["EXPLORE_CONFIG_PATH"] || default_path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



82
83
84
# File 'lib/explore/cli/config_store.rb', line 82

def path
  @path
end

Instance Method Details

#defaultsObject



27
28
29
30
# File 'lib/explore/cli/config_store.rb', line 27

def defaults
  data = normalized_data(load)
  data.fetch("default") { {} }
end

#deleteObject



73
74
75
76
77
78
79
80
# File 'lib/explore/cli/config_store.rb', line 73

def delete
  return false unless File.exist?(path)

  File.delete(path)
  true
rescue SystemCallError => e
  raise Error.new("Could not remove stored Explore credentials at #{path}: #{e.message}", exit_code: 1)
end

#loadObject



17
18
19
20
21
22
23
24
25
# File 'lib/explore/cli/config_store.rb', line 17

def load
  return {} unless File.file?(path)

  JSON.parse(File.read(path))
rescue JSON::ParserError => e
  raise Error.new("Stored Explore credentials at #{path} are invalid JSON: #{e.message}", exit_code: 1)
rescue SystemCallError => e
  raise Error.new("Could not read stored Explore credentials at #{path}: #{e.message}", exit_code: 1)
end

#resolve(base_url:, account:) ⇒ Object



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

def resolve(base_url:, account:)
  data = normalized_data(load)
  credential = if present_value?()
    (data.fetch("credentials"), account:, base_url:)
  elsif present_value?(base_url)
    find_default_for_base_url(data.fetch("credentials"), base_url:) || data.fetch("default")
  else
    data.fetch("default")
  end

  credential || {}
end

#save(config) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/explore/cli/config_store.rb', line 64

def save(config)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, JSON.pretty_generate(config))
  File.chmod(0o600, path)
  path
rescue SystemCallError => e
  raise Error.new("Could not save Explore credentials to #{path}: #{e.message}", exit_code: 1)
end

#save_login(base_url:, api_key:, account:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/explore/cli/config_store.rb', line 45

def (base_url:, api_key:, account:)
  data = normalized_data(load)
  credentials = data.fetch("credentials").reject do |entry|
    same_credential_target?(entry, base_url:, account:)
  end
  credential = {
    "base_url" => base_url,
    "api_key" => api_key,
    "account" => 
  }
  credentials << credential

  save({
    "version" => CURRENT_VERSION,
    "default" => credential,
    "credentials" => credentials.sort_by { |entry| [ entry.fetch("base_url"), entry.fetch("account") ] }
  })
end