You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.8 KiB
Go

/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"context"
"fmt"
"log"
"time"
"gitea.ravianand.me/Dan6erbond/listy/core"
"gitea.ravianand.me/Dan6erbond/listy/lists"
"gitea.ravianand.me/Dan6erbond/listy/users"
"github.com/spf13/cobra"
)
// syncCmd represents the sync command
var syncCmd = &cobra.Command{
Use: "sync",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
app := core.NewApp()
users, err := users.GetUsers(ctx, app)
if err != nil {
panic(err)
}
repeat, err := cmd.Flags().GetDuration("repeat")
if err != nil {
panic(err)
}
for range time.Tick(repeat) {
for _, user := range users {
numAdded, err := lists.SyncMonthlyList(ctx, app, user)
if err != nil {
log.Println(err)
}
if numAdded == 0 {
fmt.Printf("No new songs for %s", user.SpotifyID)
} else {
fmt.Printf("Added %d songs for %s", numAdded, user.SpotifyID)
}
if repeat == time.Duration(0) {
break
}
}
}
},
}
func init() {
rootCmd.AddCommand(syncCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// syncCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
syncCmd.Flags().DurationP("repeat", "r", 0, "Repeat duration for sync process")
}