Sha256: a68a0a4cc4d6f5dc44ed740250db51aa6299de6f3c1409ecb3a9e93aecdd208e

Contents?: true

Size: 1.21 KB

Versions: 40

Compression:

Stored size: 1.21 KB

Contents

require 'netrc'
require 'yaml'

module Travis
  module Tools
    # This is used when running `travis login --auto`
    class TokenFinder
      attr_accessor :netrc, :hub, :explode, :github

      def self.find(options = {})
        new(options).find
      end

      def initialize(options = {})
        self.netrc   = options[:netrc]  || Netrc.default_path
        self.hub     = options[:hub]    || ENV['HUB_CONFIG'] || '~/.config/hub'
        self.github  = options[:github] || 'github.com'
        self.explode = options[:explode]
      end

      def hub=(file)
        @hub = File.expand_path(file)
      end

      def netrc=(file)
        @netrc = File.expand_path(file)
      end

      def find
        find_netrc || find_hub
      end

      def find_netrc
        return unless File.readable? netrc
        data = Netrc.read(netrc)[github]
        data.detect { |e| e.size == 40 } if data
      rescue => e
        raise e if explode
      end

      def find_hub
        return unless File.readable? hub
        data   = YAML.load_file(File.expand_path(hub))
        data &&= Array(data[github])
        data.first['oauth_token'] if data.size == 1
      rescue => e
        raise e if explode
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
travis-1.5.6.travis.320.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.319.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.317.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.315.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.314.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.312.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.307.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.306.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.304.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.303.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.301.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.299.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.297.4 lib/travis/tools/token_finder.rb
travis-1.5.6.travis.296.4 lib/travis/tools/token_finder.rb
travis-1.5.5 lib/travis/tools/token_finder.rb
travis-1.5.4 lib/travis/tools/token_finder.rb
travis-1.5.3 lib/travis/tools/token_finder.rb
travis-1.5.2 lib/travis/tools/token_finder.rb
travis-1.5.1 lib/travis/tools/token_finder.rb
travis-1.5.0 lib/travis/tools/token_finder.rb