Skip to content

Commit

Permalink
feat: add sync subcommand and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
lupinthe14th committed Apr 1, 2021
1 parent 404bb01 commit e327219
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
14 changes: 2 additions & 12 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand All @@ -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")
}
43 changes: 25 additions & 18 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit e327219

Please sign in to comment.