github.com/pingcap/tiup@v1.15.1/components/dm/command/replay.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 command
    15  
    16  import (
    17  	"fmt"
    18  	"path"
    19  	"strings"
    20  
    21  	"github.com/pingcap/errors"
    22  	"github.com/pingcap/tiup/pkg/checkpoint"
    23  	"github.com/pingcap/tiup/pkg/cluster/audit"
    24  	"github.com/pingcap/tiup/pkg/cluster/spec"
    25  	"github.com/pingcap/tiup/pkg/tui"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  func newReplayCmd() *cobra.Command {
    30  	cmd := &cobra.Command{
    31  		Use:   "replay <audit-id>",
    32  		Short: "Replay previous operation and skip successed steps",
    33  		RunE: func(cmd *cobra.Command, args []string) error {
    34  			if len(args) != 1 {
    35  				return cmd.Help()
    36  			}
    37  
    38  			file := path.Join(spec.AuditDir(), args[0])
    39  			if !checkpoint.HasCheckPoint() {
    40  				if err := checkpoint.SetCheckPoint(file); err != nil {
    41  					return errors.Annotate(err, "set checkpoint failed")
    42  				}
    43  			}
    44  
    45  			args, err := audit.CommandArgs(file)
    46  			if err != nil {
    47  				return errors.Annotate(err, "read audit log failed")
    48  			}
    49  
    50  			if !skipConfirm {
    51  				if err := tui.PromptForConfirmOrAbortError(
    52  					fmt.Sprintf("Will replay the command `tiup dm %s`\nDo you want to continue? [y/N]: ", strings.Join(args[1:], " ")),
    53  				); err != nil {
    54  					return err
    55  				}
    56  			}
    57  
    58  			rootCmd.SetArgs(args[1:])
    59  			return rootCmd.Execute()
    60  		},
    61  	}
    62  
    63  	return cmd
    64  }