Sha256: fc96e678dc6937c70a2c8ef723cbd829c338d963f48aadddf682a586ecb7c56f

Contents?: true

Size: 1.27 KB

Versions: 52

Compression:

Stored size: 1.27 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]
        self.github  = 'github.com' if github.nil? or github == 'api.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

52 entries across 52 versions & 1 rubygems

Version Path
travis-1.6.4.travis.410.4 lib/travis/tools/token_finder.rb
travis-1.6.4.travis.406.4 lib/travis/tools/token_finder.rb
travis-1.6.4.travis.405.4 lib/travis/tools/token_finder.rb
travis-1.6.4.travis.395.4 lib/travis/tools/token_finder.rb
travis-1.6.3 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.394.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.392.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.390.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.389.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.388.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.387.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.386.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.385.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.384.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.381.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.379.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.377.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.376.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.374.4 lib/travis/tools/token_finder.rb
travis-1.6.3.travis.373.4 lib/travis/tools/token_finder.rb