From 1d72b87bca4042d36cea1f1e775803a5252cd224 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Wed, 15 Jun 2022 16:32:32 +0530 Subject: now macli handles logging in by itself --- auth/auth.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'auth/auth.go') 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("") + } + + 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() -- cgit v1.2.3