github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_capture_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  	apiv2client "github.com/pingcap/tiflow/pkg/api/v2"
    18  	cmdcontext "github.com/pingcap/tiflow/pkg/cmd/context"
    19  	"github.com/pingcap/tiflow/pkg/cmd/factory"
    20  	"github.com/pingcap/tiflow/pkg/cmd/util"
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  // capture holds capture information.
    25  type capture struct {
    26  	ID            string `json:"id"`
    27  	IsOwner       bool   `json:"is-owner"`
    28  	AdvertiseAddr string `json:"address"`
    29  	ClusterID     string `json:"cluster-id"`
    30  }
    31  
    32  // listCaptureOptions defines flags for the `cli capture list` command.
    33  type listCaptureOptions struct {
    34  	apiv2Client apiv2client.APIV2Interface
    35  }
    36  
    37  // newListCaptureOptions creates new listCaptureOptions for the `cli capture list` command.
    38  func newListCaptureOptions() *listCaptureOptions {
    39  	return &listCaptureOptions{}
    40  }
    41  
    42  // complete adapts from the command line args to the data and client required.
    43  func (o *listCaptureOptions) complete(f factory.Factory) error {
    44  	apiv2Client, err := f.APIV2Client()
    45  	if err != nil {
    46  		return err
    47  	}
    48  	o.apiv2Client = apiv2Client
    49  	return nil
    50  }
    51  
    52  // run runs the `cli capture list` command.
    53  func (o *listCaptureOptions) run(cmd *cobra.Command) error {
    54  	ctx := cmdcontext.GetDefaultContext()
    55  
    56  	raw, err := o.apiv2Client.Captures().List(ctx)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	captures := make([]*capture, 0, len(raw))
    61  	for _, c := range raw {
    62  		captures = append(captures,
    63  			&capture{
    64  				ID:            c.ID,
    65  				IsOwner:       c.IsOwner,
    66  				AdvertiseAddr: c.AdvertiseAddr,
    67  				ClusterID:     c.ClusterID,
    68  			})
    69  	}
    70  
    71  	return util.JSONPrint(cmd, captures)
    72  }
    73  
    74  // newCmdListCapture creates the `cli capture list` command.
    75  func newCmdListCapture(f factory.Factory) *cobra.Command {
    76  	o := newListCaptureOptions()
    77  
    78  	command := &cobra.Command{
    79  		Use:   "list",
    80  		Short: "List all captures in TiCDC cluster",
    81  		Args:  cobra.NoArgs,
    82  		Run: func(cmd *cobra.Command, args []string) {
    83  			util.CheckErr(o.complete(f))
    84  			util.CheckErr(o.run(cmd))
    85  		},
    86  	}
    87  
    88  	return command
    89  }