Class: OSDN::CLI::Command::Login
- Inherits:
-
Base
- Object
- Base
- OSDN::CLI::Command::Login
show all
- Defined in:
- lib/osdn/cli/command/login.rb
Instance Attribute Summary
Attributes inherited from Base
#credential, #format, #logger
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#credential_path, #initialize, #load_credential, #load_variables, #set_client_token, #set_credential, #update_token, #update_variables, #write_credential, #write_variables
Class Method Details
.description ⇒ Object
61
62
63
|
# File 'lib/osdn/cli/command/login.rb', line 61
def self.description
"Login and save access token."
end
|
Instance Method Details
#help ⇒ Object
57
58
59
|
# File 'lib/osdn/cli/command/login.rb', line 57
def help
puts "#{$0} login"
end
|
#launch_brwoser(url) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/osdn/cli/command/login.rb', line 32
def launch_brwoser(url)
puts "Access following URL to get auth code;\n#{url}"
if ENV['DISPLAY']
%w(/usr/bin/xdg-open /usr/bin/X11/xdg-open /usr/local/bin/xdg-open
/usr/bin/x-www-browser /usr/bin/firefox /usr/local/bin/firefox
).each do |bin|
File.executable?(bin) or next
exec(bin, url) if fork.nil?
return
end
end
case RUBY_PLATFORM
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
spawn("start #{url.gsub(/&/, '^&')}")
when /darwin|mac os/
exec("/usr/bin/open", url) if fork.nil?
end
end
|
#prompt(msg = "", newline = false) ⇒ Object
51
52
53
54
55
|
# File 'lib/osdn/cli/command/login.rb', line 51
def prompt(msg = "", newline = false)
require 'readline'
msg += "\n" if newline
Readline.readline(msg, true).to_s.squeeze(" ").strip
end
|
#run ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/osdn/cli/command/login.rb', line 3
def run
logger.debug "Trying login"
scope = %w(profile group group_write)
auth_url = "https://#{OSDNClient.configure.host}/account/oauth2ui/authorize?client_id=#{CLI.client_id}&state=cli#{Time.now.to_i}&response_type=code&scope=#{scope.join('%20')}"
launch_brwoser auth_url
puts
authcode = prompt("Type your auth code: ")
puts
if authcode.empty?
logger.error "Empty auth code, login has been canceled."
return
end
api = OSDNClient::DefaultApi.new
begin
set_credential api.token(CLI.client_id, CLI.client_secret, code: authcode)
rescue OSDNClient::ApiError => e
begin
err = JSON.parse(e.response_body)
logger.fatal err["error_description"]
rescue
logger.fatal "Failed to get access token"
end
return
end
end
|