package internal import ( "context" "strconv" "strings" "exporter/database/telegram_resource/public/table" . "github.com/go-jet/jet/v2/postgres" "github.com/pkg/errors" "github.com/spf13/cobra" ) func ChannelSetCmd() *cobra.Command { cmd := &cobra.Command{ Use: "set", Short: "set a channel config", RunE: func(cmd *cobra.Command, args []string) error { return channelSetCmd(context.Background(), args) }, } cmd.Flags().Int64Var(&pkID, "pk", 0, "channel pk id") return cmd } func channelSetCmd(ctx context.Context, args []string) error { if pkID == 0 { return errors.New("channel id or alias is required") } tbl := table.Channels val := []interface{}{} for _, arg := range args { kv := strings.Split(arg, "=") if len(kv) != 2 { return errors.New("invalid arg format") } switch kv[0] { case "min_id": v, err := strconv.ParseInt(kv[1], 10, 64) if err != nil { return errors.Wrap(err, "invalid min_id") } val = append(val, tbl.MinID.SET(Int(v))) case "offset": v, err := strconv.ParseInt(kv[1], 10, 64) if err != nil { return errors.Wrap(err, "invalid offset") } val = append(val, tbl.Offset.SET(Int(v))) case "media", "export_media": v := kv[1] == "true" val = append(val, tbl.ExportMedia.SET(Bool(v))) default: return errors.New("invalid arg key") } } if len(val) == 0 { return errors.New("no arg provided") } stmt := tbl.UPDATE().SET(val[0]).WHERE(tbl.ID.EQ(Int(pkID))) if len(val) > 1 { stmt = tbl.UPDATE().SET(val[0], val[1:]...).WHERE(tbl.ID.EQ(Int(pkID))) } _, err := stmt.ExecContext(ctx, db) return err }