diff --git a/cmd/sync/command.go b/cmd/sync/command.go index f85f4dbc..3400bdaf 100644 --- a/cmd/sync/command.go +++ b/cmd/sync/command.go @@ -22,6 +22,7 @@ func AddCommands(parent *cobra.Command) { ch := notify.NewOfficialChanify(chanifyToken) full := utils.Must1(cmd.Flags().GetBool(`full`)) + every := utils.Must1(cmd.Flags().GetDuration(`every`)) cred := Credential{ Author: os.Getenv(`AUTHOR`), @@ -34,20 +35,39 @@ func AddCommands(parent *cobra.Command) { } gs := New(client.InitHostConfigs(), cred, ".", full) - for { + + sync := func() error { if err := gs.Sync(); err != nil { ch.Send("同步失败", err.Error(), true) - log.Println(err) - time.Sleep(time.Minute * 15) - continue - } else { - log.Println(`同步完成。`) - ch.Send(`同步成功`, `全部完成,没有错误。`, false) + return err + } + + ch.Send(`同步成功`, `全部完成,没有错误。`, false) + log.Println(`同步完成。`) + return nil + } + + if every <= 0 { + if err := sync(); err != nil { + log.Fatalln(err) + } + os.Exit(0) + } + + for { + for range 3 { + if err := sync(); err != nil { + log.Println(err) + time.Sleep(time.Second * 5) + continue + } + break } - time.Sleep(time.Hour) + time.Sleep(every) } }, } syncCmd.Flags().Bool(`full`, false, `初次备份是否全量扫描更新。`) + syncCmd.Flags().Duration(`every`, 0, `每隔多久同步一次。如果不设置,默认只同步一次。`) parent.AddCommand(syncCmd) }