Skip to content

Commit 8a8583c

Browse files
committed
Open Default Browser instead of printing the link.
1 parent 05ecbf8 commit 8a8583c

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

spt/auth.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"log"
99
"net/http"
1010
"os"
11+
"os/exec"
1112
"path/filepath"
13+
"runtime"
1214

1315
_auth "github.com/zmb3/spotify/v2/auth"
1416
"golang.org/x/oauth2"
@@ -17,6 +19,24 @@ import (
1719
"github.com/zmb3/spotify/v2"
1820
)
1921

22+
// Taken from: https://gist.github.com/hyg/9c4afcd91fe24316cbf0l
23+
func openbrowser(url string) error {
24+
var err error
25+
26+
switch runtime.GOOS {
27+
case "linux":
28+
err = exec.Command("xdg-open", url).Start()
29+
case "windows":
30+
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
31+
case "darwin":
32+
err = exec.Command("open", url).Start()
33+
default:
34+
err = fmt.Errorf("unsupported platform")
35+
}
36+
37+
return err
38+
}
39+
2040
var (
2141
redirectURI = "http://localhost:8080/callback"
2242
scopes = []string{
@@ -85,8 +105,11 @@ func InitClient() error {
85105
}()
86106
url := auth.AuthURL(state)
87107

88-
fmt.Println("Please log in to Spotify by visiting the following page in your browser: ")
89-
fmt.Println(url)
108+
fmt.Println("Please log in to Spotify through the browser.")
109+
if err := openbrowser(url); err != nil {
110+
fmt.Printf("Error opening the default browsers: %s\nPlease open the following link Manually:\n", err.Error())
111+
fmt.Println(url)
112+
}
90113

91114
// wait for auth to complete
92115
payload := <-ch

0 commit comments

Comments
 (0)