From ae30971b5044a4671ddbb9ebe4f61a0b1ae6d29e Mon Sep 17 00:00:00 2001 From: a Date: Wed, 18 Jan 2023 22:58:27 -0600 Subject: [PATCH 1/7] Asdf --- src/commands/commands.go | 54 +++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/commands/commands.go b/src/commands/commands.go index 555e682..b54e857 100644 --- a/src/commands/commands.go +++ b/src/commands/commands.go @@ -2,8 +2,9 @@ package commands import ( "encoding/json" + "errors" "fmt" - "io/ioutil" + "io" "math" "math/rand" "net/url" @@ -728,7 +729,7 @@ func SetDevice(ctx *gctx.Context, client *spotify.Client, device spotify.PlayerD return err } configDir, _ := os.UserConfigDir() - err = ioutil.WriteFile(filepath.Join(configDir, "gospt/device.json"), out, 0o644) + err = os.WriteFile(filepath.Join(configDir, "gospt/device.json"), out, 0o644) if err != nil { return err } @@ -915,7 +916,7 @@ func activateDevice(ctx *gctx.Context, client *spotify.Client) error { return err } defer deviceFile.Close() - deviceValue, err := ioutil.ReadAll(deviceFile) + deviceValue, err := io.ReadAll(deviceFile) if err != nil { return err } @@ -936,35 +937,36 @@ func activateDevice(ctx *gctx.Context, client *spotify.Client) error { func GetRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullPlaylist, error) { configDir, _ := os.UserConfigDir() - if _, err := os.Stat(filepath.Join(configDir, "gospt/radio.json")); err == nil { - playlistFile, err := os.Open(filepath.Join(configDir, "gospt/radio.json")) - if err != nil { - return nil, err - } - defer playlistFile.Close() - playlistValue, err := ioutil.ReadAll(playlistFile) - if err != nil { - return nil, err - } - var playlist *spotify.FullPlaylist - err = json.Unmarshal(playlistValue, &playlist) - if err != nil { - return nil, err - } - return playlist, nil + playlistFile, err := os.ReadFile(filepath.Join(configDir, "gospt/radio.json")) + if errors.Is(err, os.ErrNotExist) { + return CreateRadioPlaylist(ctx, client) } - // private flag doesnt work - playlist, err := client.CreatePlaylistForUser(ctx, ctx.UserId, "autoradio", "Automanaged radio playlist", false, false) if err != nil { return nil, err } - out, err := json.MarshalIndent(playlist, "", " ") - if err != nil { - return nil, err - } - err = ioutil.WriteFile(filepath.Join(configDir, "gospt/radio.json"), out, 0o644) + var playlist *spotify.FullPlaylist + err = json.Unmarshal(playlistFile, &playlist) if err != nil { return nil, err } return playlist, nil } + +func CreateRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullPlaylist, error) { + // private flag doesnt work + configDir, _ := os.UserConfigDir() + playlist, err := client.CreatePlaylistForUser(ctx, ctx.UserId, "autoradio", "Automanaged radio playlist", false, false) + if err != nil { + return nil, err + } + raw, err := json.MarshalIndent(playlist, "", " ") + if err != nil { + return nil, err + } + err = os.WriteFile(filepath.Join(configDir, "gospt/radio.json"), raw, 0o644) + if err != nil { + return nil, err + } + + return playlist, nil +} -- 2.45.2 From 8f528e41710571ce49e71bc0b4b1982b2561c048 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Wed, 18 Jan 2023 21:05:02 -0800 Subject: [PATCH 2/7] a --- src/commands/commands.go | 48 +++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/commands/commands.go b/src/commands/commands.go index 555e682..79ec332 100644 --- a/src/commands/commands.go +++ b/src/commands/commands.go @@ -2,6 +2,7 @@ package commands import ( "encoding/json" + "errors" "fmt" "io/ioutil" "math" @@ -936,35 +937,36 @@ func activateDevice(ctx *gctx.Context, client *spotify.Client) error { func GetRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullPlaylist, error) { configDir, _ := os.UserConfigDir() - if _, err := os.Stat(filepath.Join(configDir, "gospt/radio.json")); err == nil { - playlistFile, err := os.Open(filepath.Join(configDir, "gospt/radio.json")) - if err != nil { - return nil, err - } - defer playlistFile.Close() - playlistValue, err := ioutil.ReadAll(playlistFile) - if err != nil { - return nil, err - } - var playlist *spotify.FullPlaylist - err = json.Unmarshal(playlistValue, &playlist) - if err != nil { - return nil, err - } - return playlist, nil + playlistFile, err := os.ReadFile(filepath.Join(configDir, "gospt/radio.json")) + if errors.Is(err, os.ErrNotExist) { + return CreateRadioPlaylist(ctx, client) } - // private flag doesnt work - playlist, err := client.CreatePlaylistForUser(ctx, ctx.UserId, "autoradio", "Automanaged radio playlist", false, false) if err != nil { return nil, err } - out, err := json.MarshalIndent(playlist, "", " ") - if err != nil { - return nil, err - } - err = ioutil.WriteFile(filepath.Join(configDir, "gospt/radio.json"), out, 0o644) + var playlist *spotify.FullPlaylist + err = json.Unmarshal(playlistFile, &playlist) if err != nil { return nil, err } return playlist, nil } + +func CreateRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullPlaylist, error) { + // private flag doesnt work + configDir, _ := os.UserConfigDir() + playlist, err := client.CreatePlaylistForUser(ctx, ctx.UserId, "autoradio", "Automanaged radio playlist", false, false) + if err != nil { + return nil, err + } + raw, err := json.MarshalIndent(playlist, "", " ") + if err != nil { + return nil, err + } + err = os.WriteFile(filepath.Join(configDir, "gospt/radio.json"), raw, 0o644) + if err != nil { + return nil, err + } + + return playlist, nil +} -- 2.45.2 From 55d4a4de5b6b303a4b111a4c969e2b3844c2219c Mon Sep 17 00:00:00 2001 From: a Date: Wed, 18 Jan 2023 23:12:13 -0600 Subject: [PATCH 3/7] ioutil --- src/auth/auth.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/auth/auth.go b/src/auth/auth.go index 07ce019..fb1cdc8 100644 --- a/src/auth/auth.go +++ b/src/auth/auth.go @@ -3,6 +3,7 @@ package auth import ( "encoding/json" "fmt" + "io" "io/ioutil" "log" "net/http" @@ -63,7 +64,7 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) { return nil, err } defer authFile.Close() - authValue, err := ioutil.ReadAll(authFile) + authValue, err := io.ReadAll(authFile) if err != nil { return nil, err } @@ -82,7 +83,7 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) { if err != nil { panic(err.Error()) } - err = ioutil.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644) + err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644) if err != nil { panic("FAILED TO SAVE AUTH") } @@ -91,7 +92,7 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) { if err != nil { panic(err.Error()) } - err = ioutil.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644) + err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644) if err != nil { panic("FAILED TO SAVE AUTH") } -- 2.45.2 From 95c00e5c0de48f8f5736a99813852861f1ab00b3 Mon Sep 17 00:00:00 2001 From: a Date: Wed, 18 Jan 2023 23:15:39 -0600 Subject: [PATCH 4/7] asdas --- src/auth/auth.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/auth/auth.go b/src/auth/auth.go index fb1cdc8..3ae5fbf 100644 --- a/src/auth/auth.go +++ b/src/auth/auth.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -139,7 +138,7 @@ func completeAuth(w http.ResponseWriter, r *http.Request) { if err != nil { panic(err.Error()) } - err = ioutil.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644) + err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644) if err != nil { panic("FAILED TO SAVE AUTH") } -- 2.45.2 From 29313f4b31c6d174d8a54203ff92ce7177e437ad Mon Sep 17 00:00:00 2001 From: a Date: Wed, 18 Jan 2023 23:16:09 -0600 Subject: [PATCH 5/7] sddsd --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b5f5247..7b5bab1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bin/ -gospt +bin/* +/gospt -- 2.45.2 From ef9405ff9344ecb2f22f9f39fdd6839aa3091e44 Mon Sep 17 00:00:00 2001 From: a Date: Wed, 18 Jan 2023 23:17:59 -0600 Subject: [PATCH 6/7] fix makefile --- Makefile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index d4db0c0..56c2230 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ -build: +build: gospt + +gospt: $(shell find . -name '*.go') mkdir -p bin - go build -o ./bin/gospt . + go build -o gospt . completions: mkdir -p completions @@ -16,6 +18,7 @@ tidy: clean: rm -rf bin + rm -rf gospt rm -rf completions uninstall: @@ -23,7 +26,7 @@ uninstall: rm /usr/share/zsh/site-functions/_gospt install: - cp bin/gospt /usr/bin + cp gospt /usr/bin cp completions/_gospt /usr/share/zsh/site-functions/_gospt cp completions/gospt /usr/share/bash-completion/completions/gospt cp completions/gospt.fish /usr/share/fish/vendor_completions.d/gospt.fish -- 2.45.2 From 53afafff134d08cd06dc532485cb75c60525adc2 Mon Sep 17 00:00:00 2001 From: a Date: Wed, 18 Jan 2023 23:18:53 -0600 Subject: [PATCH 7/7] remove extra call to get config file --- src/auth/auth.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/auth/auth.go b/src/auth/auth.go index 3ae5fbf..310d2c0 100644 --- a/src/auth/auth.go +++ b/src/auth/auth.go @@ -27,7 +27,6 @@ var ( func GetClient(ctx *gctx.Context) (*spotify.Client, error) { if config.Values.ClientId == "" || config.Values.ClientSecret == "" || config.Values.Port == "" { - configDir, _ := os.UserConfigDir() 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.Print("\nclient_id: \"idgoesherelikethis\"\nclient_secret: \"secretgoesherelikethis\"\nport:\"8888\"\n\n") -- 2.45.2