Class: Gitcamp::Github::Auth

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/gitcamp/github/auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/gitcamp/github/auth.rb', line 5

def client
  @client
end

#usernameObject (readonly)

Returns the value of attribute username.



5
6
7
# File 'lib/gitcamp/github/auth.rb', line 5

def username
  @username
end

Instance Method Details

#loginObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitcamp/github/auth.rb', line 8

def 
  say 'Authenticating with GitHub', Thor::Shell::Color::YELLOW

  netrc = Netrc.read

  if netrc['api.github.com']
    @username, oauth_token = netrc['api.github.com']
  else
    @username = ask('Username:')
    password = ask_password('Password:')

    begin
      client = Octokit::Client.new(:login => @username, :password => password)
    rescue Octokit::Unauthorized => ex
      puts "Failed to authenticate you with those GitHub credentials"
      exit false
    end

    auth = client.create_authorization(:scopes => %w[repo])
    oauth_token = auth.token

    unless netrc['api.github.com']
      netrc['api.github.com'] = @username, auth.token
      netrc.save
    end
  end

  client = Octokit::Client.new(:login => @username, :oauth_token => oauth_token)

  say "Authenticated as #{@username}", Thor::Shell::Color::GREEN

  client
end