github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_changefeed_pause.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  	"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  // pauseChangefeedOptions defines flags for the `cli changefeed pause` command.
    25  type pauseChangefeedOptions struct {
    26  	apiClient apiv2client.APIV2Interface
    27  
    28  	changefeedID string
    29  	namespace    string
    30  }
    31  
    32  // newPauseChangefeedOptions creates new options for the `cli changefeed pause` command.
    33  func newPauseChangefeedOptions() *pauseChangefeedOptions {
    34  	return &pauseChangefeedOptions{}
    35  }
    36  
    37  // addFlags receives a *cobra.Command reference and binds
    38  // flags related to template printing to it.
    39  func (o *pauseChangefeedOptions) addFlags(cmd *cobra.Command) {
    40  	cmd.PersistentFlags().StringVarP(&o.namespace, "namespace", "n", "default", "Replication task (changefeed) Namespace")
    41  	cmd.PersistentFlags().StringVarP(&o.changefeedID, "changefeed-id", "c", "", "Replication task (changefeed) ID")
    42  	_ = cmd.MarkPersistentFlagRequired("changefeed-id")
    43  }
    44  
    45  // complete adapts from the command line args to the data and client required.
    46  func (o *pauseChangefeedOptions) complete(f factory.Factory) error {
    47  	apiClient, err := f.APIV2Client()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	o.apiClient = apiClient
    53  	return nil
    54  }
    55  
    56  // run the `cli changefeed pause` command.
    57  func (o *pauseChangefeedOptions) run() error {
    58  	ctx := context.GetDefaultContext()
    59  	return o.apiClient.Changefeeds().Pause(ctx, o.namespace, o.changefeedID)
    60  }
    61  
    62  // newCmdPauseChangefeed creates the `cli changefeed pause` command.
    63  func newCmdPauseChangefeed(f factory.Factory) *cobra.Command {
    64  	o := newPauseChangefeedOptions()
    65  
    66  	command := &cobra.Command{
    67  		Use:   "pause",
    68  		Short: "Pause a replication task (changefeed)",
    69  		Args:  cobra.NoArgs,
    70  		Run: func(cmd *cobra.Command, args []string) {
    71  			util.CheckErr(o.complete(f))
    72  			util.CheckErr(o.run())
    73  		},
    74  	}
    75  
    76  	o.addFlags(command)
    77  
    78  	return command
    79  }