From d4a7a5cafd62715c34b1a8b86c72f5c17d9a070e Mon Sep 17 00:00:00 2001 From: RaviAnand Mohabir Date: Mon, 30 Oct 2023 12:21:39 +0100 Subject: [PATCH] feat: :sparkles: add repeat flag to automatically repeat sync process --- cmd/sync.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/cmd/sync.go b/cmd/sync.go index 897ca3a..2657fba 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "log" + "time" "gitea.ravianand.me/Dan6erbond/listy/core" "gitea.ravianand.me/Dan6erbond/listy/lists" @@ -35,17 +36,29 @@ to quickly create a Cobra application.`, panic(err) } - for _, user := range users { - numAdded, err := lists.SyncMonthlyList(ctx, app, user) + repeat, err := cmd.Flags().GetDuration("repeat") - if err != nil { - log.Println(err) - } + 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 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 + } } } }, @@ -62,5 +75,5 @@ func init() { // Cobra supports local flags which will only run when this command // is called directly, e.g.: - // syncCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + syncCmd.Flags().DurationP("repeat", "r", 0, "Repeat duration for sync process") }