diff options
Diffstat (limited to 'auth/auth.go')
-rw-r--r-- | auth/auth.go | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/auth/auth.go b/auth/auth.go index e75d228..a8d28c7 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -21,6 +21,9 @@ package auth import ( "os" "os/user" + "os/exec" + "runtime" + "errors" "fmt" ) @@ -42,14 +45,36 @@ func Login() { clientId := askClientId() challenge := codeChallenge() link := generateLink(clientId, challenge) - fmt.Println("Please open this link in the browser:") - fmt.Println(link) + + openInBrowser(link) + listen(clientId, challenge) } func generateLink(clientId, challenge string) string { return "https://myanimelist.net/v1/oauth2/authorize?response_type=code&client_id=" + clientId + "&code_challenge=" + challenge } +func openInBrowser(url string) { + fmt.Println("Attempting to launch \x1b[36m" + url + "\x1b[0m in your default web browser. If it doesn't launch please manually copy-paste the link.") + + var err error + switch runtime.GOOS { + case "linux": + err = exec.Command("xdg-open", url).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + err = exec.Command("open", url).Start() + default: + err = errors.New("<failed to detect platform>") + } + + if err != nil { + fmt.Println("There was an error while launching your browser.", err) + fmt.Println("Please manually copy and paste the above URL into your web browser.") + } +} + func Logout() { deleteClientId() deleteToken() |