logging
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
abs3nt 2024-02-18 11:34:08 -08:00
parent 1747097e7a
commit e56ffc60a3
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
7 changed files with 33 additions and 18 deletions

View File

@ -18,8 +18,8 @@ var Services = fx.Options(
fx.Provide(
func() *slog.Logger {
return slog.New(tint.NewHandler(os.Stdout, &tint.Options{
AddSource: true,
Level: slog.LevelDebug.Level(),
TimeFormat: "[15:04:05.000]",
}))
},
services.NewSpotifyClient,

View File

@ -2,7 +2,6 @@ package cli
import (
"fmt"
"log/slog"
"os"
"strconv"
@ -19,7 +18,7 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
defer func() {
err := s.Shutdown()
if err != nil {
slog.Error("SHUTDOWN", "error shutting down", err)
c.Log.Error("SHUTDOWN", "error shutting down", err)
}
}()
app := &cli.App{
@ -232,6 +231,13 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
return c.Repeat()
},
},
{
Name: "shuffle",
Usage: "Toggle shuffle mode",
Action: func(cCtx *cli.Context) error {
return c.Shuffle()
},
},
{
Name: "seek",
Usage: "Seek to a position in the song",
@ -265,7 +271,7 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
},
}
if err := app.Run(os.Args); err != nil {
slog.Error("COMMANDER", "run error", err)
c.Log.Error("COMMANDER", "run error", err)
os.Exit(1)
}
}

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"io"
"log/slog"
"os"
"path/filepath"
@ -33,7 +32,7 @@ func (c *Commander) activateDevice(ctx context.Context) (spotify.ID, error) {
return "", err
}
} else {
slog.Error("COMMANDER", "failed to activated device", "YOU MUST RUN gospt setdevice FIRST")
c.Log.Error("COMMANDER", "failed to activated device", "YOU MUST RUN gospt setdevice FIRST")
}
return device.ID, nil
}

View File

@ -20,12 +20,14 @@ type CommanderParams struct {
Context context.Context
Client *spotify.Client
Log *slog.Logger
}
type Commander struct {
Context context.Context
Client *spotify.Client
User *spotify.PrivateUser
Log *slog.Logger
}
func NewCommander(p CommanderParams) CommanderResult {
@ -38,6 +40,7 @@ func NewCommander(p CommanderParams) CommanderResult {
Context: p.Context,
Client: p.Client,
User: currentUser,
Log: p.Log,
}
return CommanderResult{
Commander: c,

View File

@ -1,9 +1,5 @@
package commands
import (
"fmt"
)
func (c *Commander) Repeat() error {
state, err := c.Client.PlayerState(c.Context)
if err != nil {
@ -18,6 +14,6 @@ func (c *Commander) Repeat() error {
if err != nil {
return err
}
fmt.Println("Repeat set to", newState)
c.Log.Info("COMMANDER", "Repeat set to", newState)
return nil
}

View File

@ -0,0 +1,14 @@
package commands
func (c *Commander) Shuffle() error {
state, err := c.Client.PlayerState(c.Context)
if err != nil {
return err
}
err = c.Client.Shuffle(c.Context, !state.ShuffleState)
if err != nil {
return err
}
c.Log.Info("COMMANDER", "shuffle state", !state.ShuffleState)
return nil
}

View File

@ -40,10 +40,7 @@ func (fn roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)
func NewSpotifyClient(conf *config.Config) (c SpotifyClientResult, err error) {
if conf.ClientId == "" || (conf.ClientSecret == "" && conf.ClientSecretCmd == "") || conf.Port == "" {
fmt.Println("PLEASE WRITE YOUR CONFIG FILE IN", filepath.Join(configDir, "gospt/gospt.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")
return SpotifyClientResult{}, fmt.Errorf("\nINVALID CONFIG")
return SpotifyClientResult{}, fmt.Errorf("INVALID CONFIG")
}
if conf.ClientSecretCmd != "" {
args := strings.Fields(conf.ClientSecretCmd)
@ -129,7 +126,7 @@ func NewSpotifyClient(conf *config.Config) (c SpotifyClientResult, err error) {
_ = server.ListenAndServe()
}()
url := auth.AuthURL(state)
fmt.Println(url)
slog.Info("AUTH", "url", url)
cmd := exec.Command("xdg-open", url)
_ = cmd.Start()
// wait for auth to complete
@ -141,7 +138,7 @@ func NewSpotifyClient(conf *config.Config) (c SpotifyClientResult, err error) {
if err != nil {
return SpotifyClientResult{}, err
}
fmt.Println("You are logged in as:", user.ID)
slog.Info("AUTH", "You are logged in as:", user.ID)
return SpotifyClientResult{Client: client}, nil
}