IMPROVED: add status to cache (#12)
ci/woodpecker/push/woodpecker Pipeline was successful Details
ci/woodpecker/tag/woodpecker Pipeline was successful Details

Reviewed-on: https://gitea.asdf.cafe/abs3nt/gospt/pulls/12
This commit is contained in:
abs3nt 2023-02-28 10:53:58 -08:00
parent 9625551767
commit 7ea37f8061
2 changed files with 20 additions and 6 deletions

View File

@ -2,6 +2,7 @@ pipeline:
build:
image: golang:1.19
commands:
- go mod tidy
- go build -o gospt
- mkdir completions
- ./gospt completion zsh > completions/gospt_zsh

View File

@ -889,11 +889,25 @@ func (c *Commands) Previous(ctx *gctx.Context) error {
}
func (c *Commands) Status(ctx *gctx.Context) error {
state, err := c.Client().PlayerState(ctx)
state, err := cache.DefaultCache().GetOrDo("state", func() (string, error) {
state, err := c.Client().PlayerState(ctx)
if err != nil {
return "", err
}
str, err := c.FormatState(state)
if err != nil {
return "", nil
}
return str, nil
}, 5*time.Second)
if err != nil {
return err
}
return c.PrintState(state)
if err != nil {
return err
}
fmt.Println(state)
return nil
}
func (c *Commands) Link(ctx *gctx.Context) (string, error) {
@ -983,15 +997,14 @@ func (c *Commands) PlaylistTracks(ctx *gctx.Context, playlist spotify.ID, page i
return c.Client().GetPlaylistTracks(ctx, playlist, spotify.Limit(50), spotify.Offset((page-1)*50))
}
func (c *Commands) PrintState(state *spotify.PlayerState) error {
func (c *Commands) FormatState(state *spotify.PlayerState) (string, error) {
state.Item.AvailableMarkets = []string{}
state.Item.Album.AvailableMarkets = []string{}
out, err := json.MarshalIndent(state, "", " ")
if err != nil {
return err
return "", err
}
fmt.Println(string(out))
return nil
return (string(out)), nil
}
func (c *Commands) PrintPlaying(current *spotify.CurrentlyPlaying) error {