Sha256: d345b4d5be811e49b723ce68f2b18b5e971435240c2c5511879c5278b5459750

Contents?: true

Size: 1.74 KB

Versions: 25

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require "yaml"
require "fileutils"
require "gemsmith/authenticators/basic"
require "gemsmith/authenticators/ruby_gems"

module Gemsmith
  # Generates gem credentials for RubyGems and/or alternative servers.
  class Credentials
    DEFAULT_KEY = :rubygems_api_key
    DEFAULT_URL = "https://rubygems.org"

    attr_reader :key, :url

    def self.file_path
      File.join ENV.fetch("HOME"), ".gem", "credentials"
    end

    def self.authenticators
      [Authenticators::RubyGems, Authenticators::Basic]
    end

    def initialize key: DEFAULT_KEY, url: DEFAULT_URL, shell: Thor::Shell::Basic.new
      @key = key
      @url = url
      @shell = shell
      @credentials = read
    end

    def authenticator
      self.class
          .authenticators
          .find { |auth| auth.url.include? url }
          .then { |selected| selected || Authenticators::Basic }
    end

    def value
      String credentials[key]
    end

    def valid?
      exist? && !String(credentials[key]).empty?
    end

    def create
      write unless valid?
    end

    private

    attr_reader :credentials, :shell

    def exist?
      File.exist? self.class.file_path
    end

    def read
      Hash YAML.load_file(self.class.file_path)
    rescue StandardError
      {}
    end

    def write
      file_path = self.class.file_path

      FileUtils.mkdir_p File.dirname file_path
      File.open(file_path, "w") { |file| file << YAML.dump(update) }
      FileUtils.chmod 0o600, file_path
    end

    def update
      login = shell.ask %(What is your "#{url}" login?)
      password = shell.ask %(What is your "#{url}" password?), echo: false
      shell.say

      credentials.merge key => authenticator.new(login, password).authorization
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
gemsmith-15.0.0 lib/gemsmith/credentials.rb
gemsmith-14.11.0 lib/gemsmith/credentials.rb
gemsmith-14.10.1 lib/gemsmith/credentials.rb
gemsmith-14.10.0 lib/gemsmith/credentials.rb
gemsmith-14.9.0 lib/gemsmith/credentials.rb
gemsmith-14.8.0 lib/gemsmith/credentials.rb
gemsmith-14.7.0 lib/gemsmith/credentials.rb
gemsmith-14.6.0 lib/gemsmith/credentials.rb
gemsmith-14.5.0 lib/gemsmith/credentials.rb
gemsmith-14.4.0 lib/gemsmith/credentials.rb
gemsmith-14.3.0 lib/gemsmith/credentials.rb
gemsmith-14.2.0 lib/gemsmith/credentials.rb
gemsmith-14.1.3 lib/gemsmith/credentials.rb
gemsmith-14.1.2 lib/gemsmith/credentials.rb
gemsmith-14.1.1 lib/gemsmith/credentials.rb
gemsmith-14.1.0 lib/gemsmith/credentials.rb
gemsmith-14.0.2 lib/gemsmith/credentials.rb
gemsmith-14.0.1 lib/gemsmith/credentials.rb
gemsmith-14.0.0 lib/gemsmith/credentials.rb
gemsmith-13.8.0 lib/gemsmith/credentials.rb