github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_changefeed_list.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package cli 15 16 import ( 17 "time" 18 19 "github.com/pingcap/tiflow/cdc/api/owner" 20 "github.com/pingcap/tiflow/cdc/model" 21 "github.com/pingcap/tiflow/pkg/api/v2" 22 "github.com/pingcap/tiflow/pkg/cmd/context" 23 "github.com/pingcap/tiflow/pkg/cmd/factory" 24 "github.com/pingcap/tiflow/pkg/cmd/util" 25 "github.com/spf13/cobra" 26 ) 27 28 const timeFormat = "2006-01-02 15:04:05.000" 29 30 // changefeedCommonInfo holds some common used information of a changefeed. 31 type changefeedCommonInfo struct { 32 ID string `json:"id"` 33 Namespace string `json:"namespace"` 34 Summary *owner.ChangefeedResp `json:"summary"` 35 } 36 37 // listChangefeedOptions defines flags for the `cli changefeed list` command. 38 type listChangefeedOptions struct { 39 apiClient v2.APIV2Interface 40 41 listAll bool 42 namespace string 43 } 44 45 // newListChangefeedOptions creates new options for the `cli changefeed list` command. 46 func newListChangefeedOptions() *listChangefeedOptions { 47 return &listChangefeedOptions{} 48 } 49 50 // addFlags receives a *cobra.Command reference and binds 51 // flags related to template printing to it. 52 func (o *listChangefeedOptions) addFlags(cmd *cobra.Command) { 53 cmd.PersistentFlags().StringVarP(&o.namespace, "namespace", "n", "default", "Replication task (changefeed) Namespace") 54 cmd.PersistentFlags().BoolVarP(&o.listAll, "all", "a", false, "List all replication tasks(including removed and finished)") 55 } 56 57 // complete adapts from the command line args to the data and client required. 58 func (o *listChangefeedOptions) complete(f factory.Factory) error { 59 apiClient, err := f.APIV2Client() 60 if err != nil { 61 return err 62 } 63 o.apiClient = apiClient 64 return nil 65 } 66 67 // run the `cli changefeed list` command. 68 func (o *listChangefeedOptions) run(cmd *cobra.Command) error { 69 ctx := context.GetDefaultContext() 70 71 raw, err := o.apiClient.Changefeeds().List(ctx, o.namespace, "all") 72 if err != nil { 73 return err 74 } 75 cfs := make([]*changefeedCommonInfo, 0, len(raw)) 76 77 for _, cf := range raw { 78 if !o.listAll { 79 if cf.FeedState == model.StateFinished || 80 cf.FeedState == model.StateRemoved { 81 continue 82 } 83 } 84 cfci := &changefeedCommonInfo{ 85 ID: cf.ID, 86 Namespace: cf.Namespace, 87 Summary: &owner.ChangefeedResp{ 88 FeedState: string(cf.FeedState), 89 TSO: cf.CheckpointTSO, 90 Checkpoint: time.Time(cf.CheckpointTime).Format(timeFormat), 91 RunningError: cf.RunningError, 92 }, 93 } 94 cfs = append(cfs, cfci) 95 } 96 97 return util.JSONPrint(cmd, cfs) 98 } 99 100 // newCmdListChangefeed creates the `cli changefeed list` command. 101 func newCmdListChangefeed(f factory.Factory) *cobra.Command { 102 o := newListChangefeedOptions() 103 104 command := &cobra.Command{ 105 Use: "list", 106 Short: "List all replication tasks (changefeeds) in TiCDC cluster", 107 Args: cobra.NoArgs, 108 Run: func(cmd *cobra.Command, args []string) { 109 util.CheckErr(o.complete(f)) 110 util.CheckErr(o.run(cmd)) 111 }, 112 } 113 114 o.addFlags(command) 115 116 return command 117 }