69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package data
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"emtlabs.com/spotify-sync/api"
|
|
log "github.com/sirupsen/logrus"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
const playout_key string = "/v1/playout"
|
|
|
|
var plog *log.Entry
|
|
|
|
func initPlayout() error {
|
|
plog = dlog.WithField("data", "playout")
|
|
return nil
|
|
}
|
|
func GetPlayout() (*api.Playout, error) {
|
|
//set log
|
|
l := plog.WithField("method", "get")
|
|
l.Debugln("getting current playout from db")
|
|
ctx := context.Background()
|
|
resp, err := Client.Get(ctx, playout_key)
|
|
if err != nil {
|
|
l.Errorln(err)
|
|
return nil, err
|
|
}
|
|
if resp.Count == 0 {
|
|
l.Warningln("no playout available")
|
|
return nil, err
|
|
} else if resp.Count > 1 {
|
|
err = errors.New("multiple playouts available")
|
|
l.Errorln(err)
|
|
return nil, err
|
|
}
|
|
//decode playout
|
|
play := api.Playout{}
|
|
resp_raw := resp.Kvs[0].Value
|
|
|
|
err = yaml.Unmarshal(resp_raw, &play)
|
|
if err != nil {
|
|
l.Errorln(err)
|
|
return nil, err
|
|
}
|
|
l.Debugln("current playout in db (", play, ")")
|
|
return &play, nil
|
|
}
|
|
|
|
// SendPlayout sends a playout to the db
|
|
func SendPlayout(playout *api.Playout) error {
|
|
l := plog.WithField("method", "put")
|
|
|
|
data, err := yaml.Marshal(playout)
|
|
if err != nil {
|
|
l.Errorln(err)
|
|
return err
|
|
}
|
|
l.Debugln("sending playout to db", string(data))
|
|
ctx := context.Background()
|
|
_, err = Client.KV.Put(ctx, playout_key, string(data))
|
|
if err != nil {
|
|
l.Errorln(err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|