Skip to content

Commit

Permalink
update main.go (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Aug 6, 2023
1 parent dc1451a commit 1f4ca3c
Showing 1 changed file with 2 additions and 54 deletions.
56 changes: 2 additions & 54 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,25 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package main

import (
"context"
"fmt"
"log"

"github.com/JunNishimura/Chatify/cmd"
"github.com/JunNishimura/spotify/v2"
spotifyauth "github.com/JunNishimura/spotify/v2/auth"
"github.com/spf13/cobra"
)

const (
redirectURI = "http://localhost:8888/callback"
)

var (
auth *spotifyauth.Authenticator
clientChannel = make(chan *spotify.Client, 1)
)

func main() {
auth = spotifyauth.New(
spotifyauth.WithClientID(clientViper.GetString(SpotifyIDKeyName)),
spotifyauth.WithClientSecret(clientViper.GetString(SpotifySecretKeyName)),
spotifyauth.WithRedirectURL(redirectURI),
spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate),
)

// check if token viper is set
if !isClientInfoSet() {
authorize()
} else {
ctx := context.Background()
token := getToken()

newToken, err := auth.RefreshToken(ctx, token)
if err != nil {
log.Fatalf("fail to get a new access token: %v", err)
}

// update an access token if it has expired
if token.AccessToken != newToken.AccessToken {
if err := saveToken(newToken); err != nil {
log.Fatalf("fail to save token: %v", err)
}
}

spotifyClient := spotify.New(auth.Client(ctx, newToken))

clientChannel <- spotifyClient
}

client := <-clientChannel

user, err := client.CurrentUser(context.Background())
if err != nil {
log.Fatalf("fail to get current user: %v\n", err)
}
fmt.Printf("logged in as: %s\n", user.DisplayName)

rootCmd := &cobra.Command{
Use: "chatify",
Short: "chatify is a CLI tool that suggests music recommendations for you",
Long: "chatify is a CLI tool that suggests music recommendations for you",
Run: func(cmd *cobra.Command, args []string) {},
}

heyCommand := cmd.NewHeyCommand(context.Background(), client, clientViper.GetString(OpenAIApiKeyName))
// heyCommand := cmd.NewHeyCommand(context.Background(), client, clientViper.GetString(OpenAIApiKeyName))
greetingCommand := cmd.NewGreetingCommand()

rootCmd.AddCommand(
heyCommand,
// heyCommand,
greetingCommand,
)

Expand Down

0 comments on commit 1f4ca3c

Please sign in to comment.