package internal import ( "context" "log" "exporter/config" "github.com/gotd/td/tg" "github.com/pkg/errors" "github.com/spf13/cobra" ) var ( channelID int64 channelAlias string ) func ExportCmd() *cobra.Command { cmd := &cobra.Command{ Use: "export", 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(exportCmd), } cmd.Flags().Int64Var(&channelID, "channel", 0, "channel id") cmd.Flags().StringVar(&channelAlias, "alias", "", "channel alias") return cmd } func exportCmd(ctx context.Context) error { if channelID == 0 && channelAlias == "" { return errors.New("channel id or alias is required") } var channel *tg.Channel var err error if channelAlias != "" { channel, err = client.ChannelInfoByAlias(ctx, channelAlias) if err != nil { return err } } else { channel, err = client.ChannelInfoByID(ctx, channelID) if err != nil { return err } } if channel.GetID() == 0 { return errors.New("channel not found") } cfg := NewDBChannel(channel.GetID(), channel.Username, channel.Title) if err := cfg.Get(ctx); err != nil { return err } // https://t.me/yunpanshare/37426 for { if err := client.Channel(ctx, channel, cfg); err != nil { return err } } }