diff --git a/src/cmd/mute.go b/src/cmd/mute.go new file mode 100644 index 0000000..54bd1ff --- /dev/null +++ b/src/cmd/mute.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "gospt/src/commands" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(muteCmd) +} + +var muteCmd = &cobra.Command{ + Use: "mute", + Short: "mutes playback", + Long: `Mutes the spotify device, playback will continue`, + RunE: func(cmd *cobra.Command, args []string) error { + err := commands.SetVolume(ctx, client, 0) + if err != nil { + return err + } + return nil + }, +} diff --git a/src/cmd/seek.go b/src/cmd/seek.go new file mode 100644 index 0000000..826eff9 --- /dev/null +++ b/src/cmd/seek.go @@ -0,0 +1,48 @@ +package cmd + +import ( + "strconv" + + "gospt/src/commands" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(seekCmd) +} + +var seekCmd = &cobra.Command{ + Use: "seek {forward/backward/songposition in seconds}", + Short: "seek forward/backward or to a given second", + Args: cobra.MinimumNArgs(1), + Long: `Seeks forward or backward, or seeks to a given position in seconds`, + RunE: func(cmd *cobra.Command, args []string) error { + if args[0] == "forward" || args[0] == "f" { + err := commands.Seek(ctx, client, true) + if err != nil { + return err + } + return nil + } + + if args[0] == "backward" || args[0] == "b" { + err := commands.Seek(ctx, client, false) + if err != nil { + return err + } + return nil + } + + pos, err := strconv.Atoi(args[0]) + if err != nil { + return err + } + pos = pos * 1000 + err = commands.SetPosition(ctx, client, pos) + if err != nil { + return err + } + return nil + }, +} diff --git a/src/cmd/unmute.go b/src/cmd/unmute.go new file mode 100644 index 0000000..da4bb13 --- /dev/null +++ b/src/cmd/unmute.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "gospt/src/commands" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(unmuteCmd) +} + +var unmuteCmd = &cobra.Command{ + Use: "unmute", + Short: "unmutes playback", + Long: `unmutes the spotify device, playback will continue`, + RunE: func(cmd *cobra.Command, args []string) error { + err := commands.SetVolume(ctx, client, 100) + if err != nil { + return err + } + return nil + }, +} diff --git a/src/cmd/volume.go b/src/cmd/volume.go new file mode 100644 index 0000000..5a6ae8f --- /dev/null +++ b/src/cmd/volume.go @@ -0,0 +1,47 @@ +package cmd + +import ( + "strconv" + + "gospt/src/commands" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(volumeCmd) +} + +var volumeCmd = &cobra.Command{ + Use: "volume", + Short: "sets the volume", + Args: cobra.MinimumNArgs(1), + Long: `Sets the volume to the given percent [0-100] or increases/decreases by 5 percent if you say up or down`, + RunE: func(cmd *cobra.Command, args []string) error { + if args[0] == "up" { + err := commands.ChangeVolume(ctx, client, 5) + if err != nil { + return err + } + return nil + } + + if args[0] == "down" { + err := commands.ChangeVolume(ctx, client, -5) + if err != nil { + return err + } + return nil + } + + vol, err := strconv.Atoi(args[0]) + if err != nil { + return err + } + err = commands.SetVolume(ctx, client, vol) + if err != nil { + return err + } + return nil + }, +} diff --git a/src/commands/commands.go b/src/commands/commands.go index 383cc0b..d5dfb39 100644 --- a/src/commands/commands.go +++ b/src/commands/commands.go @@ -16,6 +16,50 @@ import ( "github.com/zmb3/spotify/v2" ) +func SetVolume(ctx *gctx.Context, client *spotify.Client, vol int) error { + return client.Volume(ctx, vol) +} + +func SetPosition(ctx *gctx.Context, client *spotify.Client, pos int) error { + err := client.Seek(ctx, pos) + if err != nil { + fmt.Println(err.Error()) + return err + } + return nil +} + +func Seek(ctx *gctx.Context, client *spotify.Client, fwd bool) error { + current, err := client.PlayerCurrentlyPlaying(ctx) + if err != nil { + return err + } + newPos := current.Progress + 5000 + if !fwd { + newPos = current.Progress - 5000 + } + err = client.Seek(ctx, newPos) + if err != nil { + return err + } + return nil +} + +func ChangeVolume(ctx *gctx.Context, client *spotify.Client, vol int) error { + state, err := client.PlayerState(ctx) + if err != nil { + return err + } + newVolume := state.Device.Volume + vol + if newVolume > 100 { + newVolume = 100 + } + if newVolume < 0 { + newVolume = 0 + } + return client.Volume(ctx, newVolume) +} + func Play(ctx *gctx.Context, client *spotify.Client) error { err := client.Play(ctx) if err != nil {