Class: Rake::GemMaintenance::CredentialStore
- Inherits:
-
Object
- Object
- Rake::GemMaintenance::CredentialStore
- Defined in:
- lib/rake/gem/maintenance/credential_store.rb
Overview
Persists rubygems.org credentials (username, API key, OTP seed) to a local config file.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_to_env(username_env_var:, api_key_env_var:) ⇒ Object
-
#initialize(path: self.class.default_path) ⇒ CredentialStore
constructor
A new instance of CredentialStore.
- #read ⇒ Object
- #update(username:, api_key:, api_key_env_var:) ⇒ Object
- #write(credentials) ⇒ Object
Constructor Details
#initialize(path: self.class.default_path) ⇒ CredentialStore
Returns a new instance of CredentialStore.
20 21 22 |
# File 'lib/rake/gem/maintenance/credential_store.rb', line 20 def initialize(path: self.class.default_path) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
24 25 26 |
# File 'lib/rake/gem/maintenance/credential_store.rb', line 24 def path @path end |
Class Method Details
.default_path ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/rake/gem/maintenance/credential_store.rb', line 11 def self.default_path base = if Gem.win_platform? ENV.fetch("APPDATA", File.("~")) else ENV.fetch("XDG_CONFIG_HOME", File.join(Dir.home, ".config")) end File.join(base, "rake-gem-maintenance", "credentials.yml") end |
Instance Method Details
#apply_to_env(username_env_var:, api_key_env_var:) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/rake/gem/maintenance/credential_store.rb', line 40 def apply_to_env(username_env_var:, api_key_env_var:) creds = read set_env_if_absent(username_env_var, creds[:username]) set_env_if_absent("RUBYGEMS_OTP_SEED", creds[:rubygems_otp_seed]) set_env_if_absent(api_key_env_var, creds[:gem_host_api_key]) end |
#read ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/rake/gem/maintenance/credential_store.rb', line 26 def read return {} unless File.exist?(@path) YAML.safe_load_file(@path, symbolize_names: true) || {} rescue StandardError {} end |
#update(username:, api_key:, api_key_env_var:) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/rake/gem/maintenance/credential_store.rb', line 47 def update(username:, api_key:, api_key_env_var:) otp_seed = ENV.fetch("RUBYGEMS_OTP_SEED", nil) updated = read.merge(username: username, gem_host_api_key: api_key) updated[:rubygems_otp_seed] = otp_seed if otp_seed && !otp_seed.empty? write(updated) ENV[api_key_env_var] = api_key end |
#write(credentials) ⇒ Object
34 35 36 37 38 |
# File 'lib/rake/gem/maintenance/credential_store.rb', line 34 def write(credentials) FileUtils.mkdir_p(File.dirname(@path)) File.write(@path, credentials.transform_keys(&:to_s).to_yaml) File.chmod(0o600, @path) unless Gem.win_platform? end |