port and xdg open

This commit is contained in:
abs3nt 2023-01-10 18:01:40 -08:00
parent 25e19cfe4f
commit b3b33733b0
3 changed files with 20 additions and 8 deletions

View File

@ -25,9 +25,18 @@ then
to use add your information to ~/.config/gospt/client.yml like this to use add your information to ~/.config/gospt/client.yml like this
``` ```
client_id: ID client_id: "idgoeshere"
client_secret: SECRET client_secret: "secretgoeshere"
port: "8888"
``` ```
if you dont want to store your secret in the file in plaintext you can use a command to retreive it:
```
client_secret_cmd: "secret spotify_secret"
```
you should have either client_secret or client_secret_cmd
then run then run

View File

@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"gospt/internal/config" "gospt/internal/config"
@ -25,17 +26,17 @@ var (
) )
func GetClient(ctx *gctx.Context) (*spotify.Client, error) { func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
if config.Values.ClientId == "" || config.Values.ClientSecret == "" { if config.Values.ClientId == "" || config.Values.ClientSecret == "" || config.Values.Port == "" {
configDir, _ := os.UserConfigDir() configDir, _ := os.UserConfigDir()
fmt.Println("PLEASE WRITE YOUR CONFIG FILE IN", filepath.Join(configDir, "gospt/client.yml")) fmt.Println("PLEASE WRITE YOUR CONFIG FILE IN", filepath.Join(configDir, "gospt/client.yml"))
fmt.Println("GO HERE TO AND MAKE AN APPLICATION: https://developer.spotify.com/dashboard/applications") fmt.Println("GO HERE TO AND MAKE AN APPLICATION: https://developer.spotify.com/dashboard/applications")
fmt.Println("\nclient_id: \"idgoesherelikethis\"\nclient_secret: \"secretgoesherelikethis\"") fmt.Print("\nclient_id: \"idgoesherelikethis\"\nclient_secret: \"secretgoesherelikethis\"\nport:\"8888\"\n\n")
return nil, fmt.Errorf("\nINVALID CONFIG") return nil, fmt.Errorf("\nINVALID CONFIG")
} }
auth = spotifyauth.New( auth = spotifyauth.New(
spotifyauth.WithClientID(config.Values.ClientId), spotifyauth.WithClientID(config.Values.ClientId),
spotifyauth.WithClientSecret(config.Values.ClientSecret), spotifyauth.WithClientSecret(config.Values.ClientSecret),
spotifyauth.WithRedirectURL("http://localhost:1024/callback"), spotifyauth.WithRedirectURL(fmt.Sprintf("http://localhost:%s/callback", config.Values.Port)),
spotifyauth.WithScopes( spotifyauth.WithScopes(
spotifyauth.ScopeImageUpload, spotifyauth.ScopeImageUpload,
spotifyauth.ScopePlaylistReadPrivate, spotifyauth.ScopePlaylistReadPrivate,
@ -102,14 +103,15 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
log.Println("Got request for:", r.URL.String()) log.Println("Got request for:", r.URL.String())
}) })
go func() { go func() {
err := http.ListenAndServe(":1024", nil) err := http.ListenAndServe(fmt.Sprintf(":%s", config.Values.Port), nil)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
}() }()
url := auth.AuthURL(state) url := auth.AuthURL(state)
fmt.Println("GO HERE", url) fmt.Println(url)
cmd := exec.Command("xdg-open", url)
cmd.Start()
// wait for auth to complete // wait for auth to complete
client := <-ch client := <-ch

View File

@ -4,4 +4,5 @@ var Values struct {
ClientId string `yaml:"client_id"` ClientId string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"` ClientSecret string `yaml:"client_secret"`
ClientSecretCmd string `yaml:"client_secret_cmd"` ClientSecretCmd string `yaml:"client_secret_cmd"`
Port string `yaml:"port"`
} }