log settings
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
abs3nt 2024-02-18 22:17:28 -08:00
parent 59fff8dfef
commit 86c2c38dfe
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
8 changed files with 115 additions and 44 deletions

10
main.go
View File

@ -1,22 +1,28 @@
package main package main
import ( import (
"gfx.cafe/util/go/fxplus"
"go.uber.org/fx" "go.uber.org/fx"
"git.asdf.cafe/abs3nt/gspot/src/app"
"git.asdf.cafe/abs3nt/gspot/src/components/cache" "git.asdf.cafe/abs3nt/gspot/src/components/cache"
"git.asdf.cafe/abs3nt/gspot/src/components/cli" "git.asdf.cafe/abs3nt/gspot/src/components/cli"
"git.asdf.cafe/abs3nt/gspot/src/components/commands" "git.asdf.cafe/abs3nt/gspot/src/components/commands"
"git.asdf.cafe/abs3nt/gspot/src/components/logger"
"git.asdf.cafe/abs3nt/gspot/src/services"
) )
func main() { func main() {
var s fx.Shutdowner var s fx.Shutdowner
app := fx.New( app := fx.New(
fxplus.WithLogger,
fx.Populate(&s), fx.Populate(&s),
app.Config, services.Config,
fx.Provide( fx.Provide(
fxplus.Context,
cache.NewCache, cache.NewCache,
commands.NewCommander, commands.NewCommander,
services.NewSpotifyClient,
logger.NewLogger,
), ),
fx.Invoke( fx.Invoke(
cli.Run, cli.Run,

View File

@ -1,39 +0,0 @@
package app
import (
"log/slog"
"os"
"gfx.cafe/util/go/fxplus"
"git.asdf.cafe/abs3nt/gunner"
"github.com/lmittmann/tint"
"go.uber.org/fx"
"git.asdf.cafe/abs3nt/gspot/src/config"
"git.asdf.cafe/abs3nt/gspot/src/services"
)
var Services = fx.Options(
fx.NopLogger,
fx.Provide(
func() *slog.Logger {
return slog.New(tint.NewHandler(os.Stdout, &tint.Options{
Level: slog.LevelDebug.Level(),
TimeFormat: "[15:04:05.000]",
}))
},
services.NewSpotifyClient,
fxplus.Context,
),
)
var Config = fx.Options(
fx.Provide(
func() *config.Config {
c := &config.Config{}
gunner.LoadApp(c, "gspot")
return c
},
),
Services,
)

View File

@ -29,6 +29,7 @@ func (c *Commander) Play() error {
} }
func (c *Commander) PlayLikedSongs(position int) error { func (c *Commander) PlayLikedSongs(position int) error {
c.Log.Debug("Playing liked songs")
err := c.ClearRadio() err := c.ClearRadio()
if err != nil { if err != nil {
return err return err
@ -37,6 +38,7 @@ func (c *Commander) PlayLikedSongs(position int) error {
if err != nil { if err != nil {
return err return err
} }
c.Log.Debug("got playlist", "id", playlist.ID)
songs, err := c.Client.CurrentUsersTracks(c.Context, spotify.Limit(50), spotify.Offset(position)) songs, err := c.Client.CurrentUsersTracks(c.Context, spotify.Limit(50), spotify.Offset(position))
if err != nil { if err != nil {
return err return err
@ -49,11 +51,13 @@ func (c *Commander) PlayLikedSongs(position int) error {
if err != nil { if err != nil {
return err return err
} }
c.Log.Debug("added songs to playlist")
err = c.Client.PlayOpt(c.Context, &spotify.PlayOptions{ err = c.Client.PlayOpt(c.Context, &spotify.PlayOptions{
PlaybackContext: &playlist.URI, PlaybackContext: &playlist.URI,
}) })
if err != nil { if err != nil {
if isNoActiveError(err) { if isNoActiveError(err) {
c.Log.Debug("need to activate device")
deviceID, err := c.activateDevice() deviceID, err := c.activateDevice()
if err != nil { if err != nil {
return err return err
@ -67,7 +71,9 @@ func (c *Commander) PlayLikedSongs(position int) error {
} }
} }
} }
c.Log.Debug("starting loop")
for page := 2; page <= 5; page++ { for page := 2; page <= 5; page++ {
c.Log.Debug("doing loop", "page", page)
songs, err := c.Client.CurrentUsersTracks(c.Context, spotify.Limit(50), spotify.Offset((50*(page-1))+position)) songs, err := c.Client.CurrentUsersTracks(c.Context, spotify.Limit(50), spotify.Offset((50*(page-1))+position))
if err != nil { if err != nil {
return err return err
@ -81,7 +87,7 @@ func (c *Commander) PlayLikedSongs(position int) error {
return err return err
} }
} }
c.Log.Debug("done")
return err return err
} }

View File

@ -0,0 +1,67 @@
package logger
import (
"log/slog"
"os"
"path/filepath"
"strings"
"github.com/lmittmann/tint"
"go.uber.org/fx"
"git.asdf.cafe/abs3nt/gspot/src/config"
)
type LoggerResult struct {
fx.Out
Logger *slog.Logger
}
type LoggerParams struct {
fx.In
Config *config.Config
}
func NewLogger(p LoggerParams) LoggerResult {
lvl := slog.LevelInfo
configLevel := strings.ToUpper(p.Config.LogLevel)
switch configLevel {
case "INFO":
lvl = slog.LevelInfo
case "WARN":
lvl = slog.LevelWarn
case "ERROR":
lvl = slog.LevelError
case "DEBUG":
lvl = slog.LevelDebug
}
if strings.ToUpper(p.Config.LogOutput) == "FILE" {
fp := ""
p, err := os.UserConfigDir()
if err != nil {
p, err := os.UserHomeDir()
if err != nil {
os.Exit(1)
}
fp = filepath.Join(p, ".config", "gspot", "gspot.log")
} else {
fp = filepath.Join(p, "gspot", "gspot.log")
}
f, err := os.Create(fp)
if err != nil {
os.Exit(1)
}
return LoggerResult{
Logger: slog.New(slog.NewJSONHandler(f, &slog.HandlerOptions{
Level: lvl.Level(),
})),
}
}
return LoggerResult{
Logger: slog.New(tint.NewHandler(os.Stdout, &tint.Options{
Level: lvl.Level(),
TimeFormat: "[15:04:05.000]",
})),
}
}

View File

@ -604,8 +604,11 @@ func Tick() tea.Cmd {
func (m *mainModel) TickPlayback() { func (m *mainModel) TickPlayback() {
playing, _ := m.commands.Client.PlayerCurrentlyPlaying(m.commands.Context) playing, _ := m.commands.Client.PlayerCurrentlyPlaying(m.commands.Context)
if playing != nil && playing.Playing && playing.Item != nil { if playing != nil && playing.Playing && playing.Item != nil {
if currentlyPlaying == nil || currentlyPlaying.Item == nil ||
currentlyPlaying.Item.ID != playing.Item.ID {
playbackContext, _ = m.getContext(playing)
}
currentlyPlaying = playing currentlyPlaying = playing
playbackContext, _ = m.getContext(playing)
} }
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(1 * time.Second)
quit := make(chan struct{}) quit := make(chan struct{})
@ -613,10 +616,14 @@ func (m *mainModel) TickPlayback() {
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
m.commands.Log.Debug("TICKING PLAYBACK")
playing, _ := m.commands.Client.PlayerCurrentlyPlaying(m.commands.Context) playing, _ := m.commands.Client.PlayerCurrentlyPlaying(m.commands.Context)
if playing != nil && playing.Playing && playing.Item != nil { if playing != nil && playing.Playing && playing.Item != nil {
if currentlyPlaying == nil || currentlyPlaying.Item == nil ||
currentlyPlaying.Item.ID != playing.Item.ID {
playbackContext, _ = m.getContext(playing)
}
currentlyPlaying = playing currentlyPlaying = playing
playbackContext, _ = m.getContext(playing)
} }
case <-quit: case <-quit:
ticker.Stop() ticker.Stop()
@ -665,18 +672,21 @@ func (m *mainModel) getContext(playing *spotify.CurrentlyPlaying) (string, error
id := strings.Split(string(context.URI), ":")[2] id := strings.Split(string(context.URI), ":")[2]
switch context.Type { switch context.Type {
case "album": case "album":
m.commands.Log.Debug("ALBUM CONTEXT")
album, err := m.commands.Client.GetAlbum(m.commands.Context, spotify.ID(id)) album, err := m.commands.Client.GetAlbum(m.commands.Context, spotify.ID(id))
if err != nil { if err != nil {
return "", err return "", err
} }
return album.Name, nil return album.Name, nil
case "playlist": case "playlist":
m.commands.Log.Debug("PLAYLIST CONTEXT")
playlist, err := m.commands.Client.GetPlaylist(m.commands.Context, spotify.ID(id)) playlist, err := m.commands.Client.GetPlaylist(m.commands.Context, spotify.ID(id))
if err != nil { if err != nil {
return "", err return "", err
} }
return playlist.Name, nil return playlist.Name, nil
case "artist": case "artist":
m.commands.Log.Debug("ARTIST CONTEXT")
artist, err := m.commands.Client.GetArtist(m.commands.Context, spotify.ID(id)) artist, err := m.commands.Client.GetArtist(m.commands.Context, spotify.ID(id))
if err != nil { if err != nil {
return "", err return "", err

View File

@ -267,6 +267,7 @@ func SavedTracksView(commands *commands.Commander) ([]list.Item, error) {
} }
func MainView(c *commands.Commander) ([]list.Item, error) { func MainView(c *commands.Commander) ([]list.Item, error) {
c.Log.Debug("SWITCHING TO MAIN VIEW")
wg := errgroup.Group{} wg := errgroup.Group{}
var saved_items *spotify.SavedTrackPage var saved_items *spotify.SavedTrackPage
var playlists *spotify.SimplePlaylistPage var playlists *spotify.SimplePlaylistPage

View File

@ -5,4 +5,6 @@ type Config struct {
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"` Port string `yaml:"port"`
LogLevel string `yaml:"log_level" default:"info"`
LogOutput string `yaml:"log_output" default:"stdout"`
} }

18
src/services/config.go Normal file
View File

@ -0,0 +1,18 @@
package services
import (
"git.asdf.cafe/abs3nt/gunner"
"go.uber.org/fx"
"git.asdf.cafe/abs3nt/gspot/src/config"
)
var Config = fx.Options(
fx.Provide(
func() *config.Config {
c := &config.Config{}
gunner.LoadApp(c, "gspot")
return c
},
),
)