From e56ffc60a3486e486f28842a94f30e5edf4f8694 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Sun, 18 Feb 2024 11:34:08 -0800 Subject: [PATCH] logging --- src/app/fx.go | 4 ++-- src/components/cli/cli.go | 12 +++++++++--- src/components/commands/activate_device.go | 3 +-- src/components/commands/commander.go | 3 +++ src/components/commands/repeat.go | 6 +----- src/components/commands/shuffle.go | 14 ++++++++++++++ src/services/auth.go | 9 +++------ 7 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 src/components/commands/shuffle.go diff --git a/src/app/fx.go b/src/app/fx.go index 85a14e2..c8f8cd7 100644 --- a/src/app/fx.go +++ b/src/app/fx.go @@ -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(), + Level: slog.LevelDebug.Level(), + TimeFormat: "[15:04:05.000]", })) }, services.NewSpotifyClient, diff --git a/src/components/cli/cli.go b/src/components/cli/cli.go index 6828ff4..eed1c5e 100644 --- a/src/components/cli/cli.go +++ b/src/components/cli/cli.go @@ -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) } } diff --git a/src/components/commands/activate_device.go b/src/components/commands/activate_device.go index d6eb60f..6a987d7 100644 --- a/src/components/commands/activate_device.go +++ b/src/components/commands/activate_device.go @@ -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 } diff --git a/src/components/commands/commander.go b/src/components/commands/commander.go index b2bac94..54785bb 100644 --- a/src/components/commands/commander.go +++ b/src/components/commands/commander.go @@ -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, diff --git a/src/components/commands/repeat.go b/src/components/commands/repeat.go index e1f203d..1c69d2c 100644 --- a/src/components/commands/repeat.go +++ b/src/components/commands/repeat.go @@ -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 } diff --git a/src/components/commands/shuffle.go b/src/components/commands/shuffle.go new file mode 100644 index 0000000..46f118f --- /dev/null +++ b/src/components/commands/shuffle.go @@ -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 +} diff --git a/src/services/auth.go b/src/services/auth.go index b8e0455..94f8f07 100644 --- a/src/services/auth.go +++ b/src/services/auth.go @@ -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 }