package internal import ( "context" "log" "time" "exporter/config" "exporter/pkg/errorx" "github.com/pkg/errors" "github.com/spf13/cobra" "go.uber.org/zap" ) var pkID int64 func ChannelExportCmd() *cobra.Command { cmd := &cobra.Command{ Use: "export", Aliases: []string{"e"}, Short: "export channels", PreRunE: func(cmd *cobra.Command, args []string) error { log.Println("init client") defer log.Println("init client done") return InitClient(config.C) }, RunE: wrapE(channelExportCmd), } cmd.Flags().Int64Var(&pkID, "pk", 0, "channel pk id") return cmd } func channelExportCmd(ctx context.Context) error { if pkID == 0 { return errors.New("db channel id required") } cfg, err := NewDBChannelFromDB(pkID) if err != nil { return err } logger.Info("get remote channel info", zap.Int64("uuid", cfg.UUID), zap.String("title", cfg.Title)) channel, err := client.ChannelInfoByID(ctx, cfg.UUID) if err != nil { return err } logger.Info("exporting history msg", zap.Int64("uuid", cfg.UUID)) var continueRetryTimes int for { if err := client.Channel(ctx, channel, cfg, false); err != nil { logger.Error("failed to export channel", zap.Error(err), zap.Int64("uuid", channel.GetID())) if errors.Is(err, errorx.ErrNoNewMessage) { break } continueRetryTimes++ if continueRetryTimes > 6 { return err } logger.Info("retry after 1 min") time.Sleep(time.Minute) continue } continueRetryTimes = 0 } return nil }