diff --git a/authmanager/authmanager.go b/authmanager/authmanager.go index da24aec..b6be9f3 100644 --- a/authmanager/authmanager.go +++ b/authmanager/authmanager.go @@ -65,6 +65,31 @@ func GetClient(ctx context.Context) (*spotify.Client, error) { return nil, err } client := spotify.New(auth.Client(context.Background(), tok)) + new_token, err := client.Token() + if err != nil { + return nil, err + } + if new_token != tok { + out, err := json.MarshalIndent(new_token, "", " ") + if err != nil { + panic(err.Error()) + } + homdir, _ := os.UserHomeDir() + err = ioutil.WriteFile(filepath.Join(homdir, ".config/gospt/auth.json"), out, 0o644) + if err != nil { + panic("FAILED TO SAVE AUTH") + } + } + out, err := json.MarshalIndent(tok, "", " ") + if err != nil { + panic(err.Error()) + } + homdir, _ := os.UserHomeDir() + err = ioutil.WriteFile(filepath.Join(homdir, ".config/gospt/auth.json"), out, 0o644) + if err != nil { + panic("FAILED TO SAVE AUTH") + } + return client, nil } // first start an HTTP server diff --git a/runner/runner.go b/runner/runner.go index 8830538..fb41739 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -13,39 +13,73 @@ func Run(client *spotify.Client, args []string) error { if err != nil { return fmt.Errorf("Failed to get current user") } - fmt.Println("YOU ARE", user.DisplayName) + fmt.Println("The following commands are currently supported:\nplay pause next shuffle\nhave fun", user.DisplayName) return nil } ctx := context.Background() switch args[0] { case "play": - err := client.Play(ctx) + err := Play(ctx, client, args) if err != nil { return err } - fmt.Println("Playing!") case "pause": - err := client.Pause(ctx) + err := Pause(ctx, client, args) if err != nil { return err } - fmt.Println("Pausing!") case "next": - err := client.Next(ctx) + err := Skip(ctx, client, args) if err != nil { return err } - fmt.Println("Skipping!") case "shuffle": - state, err := client.PlayerState(ctx) - if err != nil { - return fmt.Errorf("Failed to get current playstate") - } - err = client.Shuffle(ctx, !state.ShuffleState) + err := Shuffle(ctx, client, args) if err != nil { return err } - fmt.Println("Shuffle set to", !state.ShuffleState) + default: + return fmt.Errorf("Unsupported Command") } return nil } + +func Play(ctx context.Context, client *spotify.Client, args []string) error { + err := client.Play(ctx) + if err != nil { + return err + } + fmt.Println("Playing!") + return nil +} + +func Pause(ctx context.Context, client *spotify.Client, args []string) error { + err := client.Pause(ctx) + if err != nil { + return err + } + fmt.Println("Pausing!") + return nil +} + +func Skip(ctx context.Context, client *spotify.Client, args []string) error { + err := client.Next(ctx) + if err != nil { + return err + } + fmt.Println("Skipping!") + return nil +} + +func Shuffle(ctx context.Context, client *spotify.Client, args []string) error { + state, err := client.PlayerState(ctx) + if err != nil { + return fmt.Errorf("Failed to get current playstate") + } + err = client.Shuffle(ctx, !state.ShuffleState) + if err != nil { + return err + } + fmt.Println("Shuffle set to", !state.ShuffleState) + return nil +}