Class: Explore::Cli::ConfigStore
- Inherits:
-
Object
- Object
- Explore::Cli::ConfigStore
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #defaults ⇒ Object
- #delete ⇒ Object
-
#initialize(env:, path: nil) ⇒ ConfigStore
constructor
A new instance of ConfigStore.
- #load ⇒ Object
- #resolve(base_url:, account:) ⇒ Object
- #save(config) ⇒ Object
- #save_login(base_url:, api_key:, account:) ⇒ Object
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
#path ⇒ Object (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
#defaults ⇒ Object
27 28 29 30 |
# File 'lib/explore/cli/config_store.rb', line 27 def defaults data = normalized_data(load) data.fetch("default") { {} } end |
#delete ⇒ Object
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.}", exit_code: 1) end |
#load ⇒ Object
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.}", exit_code: 1) rescue SystemCallError => e raise Error.new("Could not read stored Explore credentials at #{path}: #{e.}", 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?(account) find_by_account(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.}", 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 save_login(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" => account } credentials << credential save({ "version" => CURRENT_VERSION, "default" => credential, "credentials" => credentials.sort_by { |entry| [ entry.fetch("base_url"), entry.fetch("account") ] } }) end |