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.2.travis.371.4 lib/travis/tools/token_finder.rb
travis-1.6.2 lib/travis/tools/token_finder.rb
travis-1.6.2.travis.370.4 lib/travis/tools/token_finder.rb
travis-1.6.2.travis.369.4 lib/travis/tools/token_finder.rb
travis-1.6.2.travis.368.4 lib/travis/tools/token_finder.rb
travis-1.6.2.travis.367.4 lib/travis/tools/token_finder.rb
travis-1.6.2.travis.366.4 lib/travis/tools/token_finder.rb
travis-1.6.2.travis.365.4 lib/travis/tools/token_finder.rb
travis-1.6.2.travis.364.4 lib/travis/tools/token_finder.rb
travis-1.6.1 lib/travis/tools/token_finder.rb
travis-1.6.1.travis.363.4 lib/travis/tools/token_finder.rb
travis-1.6.0 lib/travis/tools/token_finder.rb
travis-1.5.9.travis.362.4 lib/travis/tools/token_finder.rb
travis-1.5.9.travis.361.4 lib/travis/tools/token_finder.rb
travis-1.5.9.travis.360.4 lib/travis/tools/token_finder.rb
travis-1.5.9.travis.358.4 lib/travis/tools/token_finder.rb
travis-1.5.8 lib/travis/tools/token_finder.rb
travis-1.5.8.travis.356.4 lib/travis/tools/token_finder.rb
travis-1.5.7 lib/travis/tools/token_finder.rb
travis-1.5.7.travis.345.4 lib/travis/tools/token_finder.rb