From e327219a51f7ab69a696d48b3f04774d321e0341 Mon Sep 17 00:00:00 2001 From: lupinthe14th Date: Thu, 1 Apr 2021 23:15:49 +0900 Subject: [PATCH] feat: add sync subcommand and refactor code --- cmd/backup.go | 14 ++------------ cmd/sync.go | 43 +++++++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/cmd/backup.go b/cmd/backup.go index a5aa8b7..b0ad0d1 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -34,7 +34,7 @@ import ( // backupCmd represents the backup command var backupCmd = &cobra.Command{ Use: "backup", - Short: "A brief description of your command", + Short: "Concurrency doveadm backup in Go", RunE: func(cmd *cobra.Command, args []string) error { fmt.Println("backup called") var ( @@ -48,7 +48,7 @@ var backupCmd = &cobra.Command{ wg.Add(1) go func(u *models.User) { if err := doveadm.Backup(u); err != nil { - fmt.Printf("doveadm backup failed: %v", err) + fmt.Printf("doveadm backup failed: %v\n", err) } wg.Done() }(user) @@ -60,14 +60,4 @@ var backupCmd = &cobra.Command{ func init() { rootCmd.AddCommand(backupCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // backupCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // backupCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/sync.go b/cmd/sync.go index 99c783d..68f7bbf 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -21,36 +21,43 @@ package cmd import ( + "encoding/json" "fmt" + "os" + "sync" + "github.com/lupinthe14th/dovectl/models" + "github.com/lupinthe14th/dovectl/pkg/doveadm" "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) { + Short: "Concurrency doveadm sync in Go", + RunE: func(cmd *cobra.Command, args []string) error { fmt.Println("sync called") + var ( + users models.Users + wg sync.WaitGroup + ) + if err := json.NewDecoder(os.Stdin).Decode(&users); err != nil { + return fmt.Errorf("json decode error: %v", err) + } + for _, user := range users { + wg.Add(1) + go func(u *models.User) { + if err := doveadm.Sync(u); err != nil { + fmt.Printf("doveadm sync failed: %v\n", err) + } + wg.Done() + }(user) + } + wg.Wait() + return nil }, } 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().BoolP("toggle", "t", false, "Help message for toggle") }