github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_processor_list.go (about)

     1  // Copyright 2023 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  // listProcessorOptions defines flags for the `cli processor list` command.
    25  type listProcessorOptions struct {
    26  	apiClient apiv2client.APIV2Interface
    27  }
    28  
    29  // newListProcessorOptions creates new listProcessorOptions for the `cli processor list` command.
    30  func newListProcessorOptions() *listProcessorOptions {
    31  	return &listProcessorOptions{}
    32  }
    33  
    34  // complete adapts from the command line args to the data and client required.
    35  func (o *listProcessorOptions) complete(f factory.Factory) error {
    36  	apiClient, err := f.APIV2Client()
    37  	if err != nil {
    38  		return err
    39  	}
    40  	o.apiClient = apiClient
    41  	return nil
    42  }
    43  
    44  // run runs the `cli processor list` command.
    45  func (o *listProcessorOptions) run(cmd *cobra.Command) error {
    46  	ctx := cmdcontext.GetDefaultContext()
    47  	processors, err := o.apiClient.Processors().List(ctx)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	return util.JSONPrint(cmd, processors)
    52  }
    53  
    54  // newCmdListProcessor creates the `cli processor list` command.
    55  func newCmdListProcessor(f factory.Factory) *cobra.Command {
    56  	o := newListProcessorOptions()
    57  
    58  	command := &cobra.Command{
    59  		Use:   "list",
    60  		Short: "List all processors in TiCDC cluster",
    61  		Args:  cobra.NoArgs,
    62  		Run: func(cmd *cobra.Command, args []string) {
    63  			util.CheckErr(o.complete(f))
    64  			util.CheckErr(o.run(cmd))
    65  		},
    66  	}
    67  
    68  	return command
    69  }