github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/ctl/master/get_config.go (about)

     1  // Copyright 2020 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 master
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/pingcap/tiflow/dm/ctl/common"
    23  	"github.com/pingcap/tiflow/dm/pb"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  const cmdGetTaskConfig = "get-task-config"
    28  
    29  // NewGetCfgCmd creates a getCfg command.
    30  func NewGetCfgCmd() *cobra.Command {
    31  	cmd := &cobra.Command{
    32  		Use:     "get-config <task | master | worker | source> <name> [--file filename]",
    33  		Short:   "Gets the configuration",
    34  		Hidden:  true,
    35  		RunE:    getCfgFunc,
    36  		Aliases: []string{cmdGetTaskConfig},
    37  	}
    38  	cmd.Flags().StringP("file", "f", "", "write config to file")
    39  	return cmd
    40  }
    41  
    42  func convertCfgType(t string) pb.CfgType {
    43  	switch t {
    44  	case "task":
    45  		return pb.CfgType_TaskType
    46  	case "master":
    47  		return pb.CfgType_MasterType
    48  	case "worker":
    49  		return pb.CfgType_WorkerType
    50  	case "source":
    51  		return pb.CfgType_SourceType
    52  	default:
    53  		return pb.CfgType_InvalidType
    54  	}
    55  }
    56  
    57  // getCfgFunc gets config.
    58  func getCfgFunc(cmd *cobra.Command, args []string) error {
    59  	if cmd.CalledAs() == cmdGetTaskConfig {
    60  		args = append([]string{"task"}, args...)
    61  	}
    62  	if len(args) != 2 {
    63  		cmd.SetOut(os.Stdout)
    64  		common.PrintCmdUsage(cmd)
    65  		return errors.New("please check output to see error")
    66  	}
    67  
    68  	cfgType := args[0]
    69  	tp := convertCfgType(cfgType)
    70  	if tp == pb.CfgType_InvalidType {
    71  		common.PrintLinesf("invalid config type '%s'", cfgType)
    72  		return errors.New("please check output to see error")
    73  	}
    74  
    75  	cfgName := args[1]
    76  	filename, err := cmd.Flags().GetString("file")
    77  	if err != nil {
    78  		common.PrintLinesf("can not get filename")
    79  		return err
    80  	}
    81  
    82  	return sendGetConfigRequest(tp, cfgName, filename)
    83  }
    84  
    85  func sendGetConfigRequest(tp pb.CfgType, name, output string) error {
    86  	ctx, cancel := context.WithTimeout(context.Background(), common.GlobalConfig().RPCTimeout)
    87  	defer cancel()
    88  
    89  	resp := &pb.GetCfgResponse{}
    90  	err := common.SendRequest(
    91  		ctx,
    92  		"GetCfg",
    93  		&pb.GetCfgRequest{
    94  			Type: tp,
    95  			Name: name,
    96  		},
    97  		&resp,
    98  	)
    99  	if err != nil {
   100  		common.PrintLinesf("can not get %s config of %s", tp, name)
   101  		return err
   102  	}
   103  
   104  	if resp.Result && len(output) != 0 {
   105  		err = os.WriteFile(output, []byte(resp.Cfg), 0o600)
   106  		if err != nil {
   107  			common.PrintLinesf("can not write config to file %s", output)
   108  			return err
   109  		}
   110  		resp.Msg = fmt.Sprintf("write config to file %s succeed", output)
   111  		resp.Cfg = ""
   112  	}
   113  	common.PrettyPrintResponse(resp)
   114  	return nil
   115  }