diff --git a/dm/ctl/common/util.go b/dm/ctl/common/util.go index 1129f36628..93ff075bd8 100644 --- a/dm/ctl/common/util.go +++ b/dm/ctl/common/util.go @@ -221,3 +221,22 @@ func IsDDL(sql string) (bool, error) { return false, nil } } + +// GetTaskNameFromArgOrFile tries to retrieve name field from the file if arg is filename-like, otherwise returns arg directly +func GetTaskNameFromArgOrFile(arg string) string { + if !strings.HasSuffix(arg, ".yaml") { + return arg + } + var ( + content []byte + err error + ) + if content, err = GetFileContent(arg); err != nil { + return arg + } + cfg := config.NewTaskConfig() + if err := cfg.Decode(string(content)); err != nil { + return arg + } + return cfg.Name +} diff --git a/dm/ctl/master/get_task_config.go b/dm/ctl/master/get_task_config.go index 22fb3269a1..94bea22182 100644 --- a/dm/ctl/master/get_task_config.go +++ b/dm/ctl/master/get_task_config.go @@ -29,7 +29,7 @@ import ( // NewGetTaskCfgCmd creates a getTaskCfg command func NewGetTaskCfgCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "get-task-config [--file filename]", + Use: "get-task-config [--file filename]", Short: "get task config", RunE: getTaskCfgFunc, } @@ -45,7 +45,7 @@ func getTaskCfgFunc(cmd *cobra.Command, _ []string) (err error) { err = errors.New("please check output to see error") return } - taskName := cmd.Flags().Arg(0) + taskName := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0)) filename, err := cmd.Flags().GetString("file") if err != nil { common.PrintLines("can not get filename") diff --git a/dm/ctl/master/operate_schema.go b/dm/ctl/master/operate_schema.go index 436cb327b8..d81bd342d3 100644 --- a/dm/ctl/master/operate_schema.go +++ b/dm/ctl/master/operate_schema.go @@ -27,7 +27,7 @@ import ( // NewOperateSchemaCmd creates a OperateSchema command. func NewOperateSchemaCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "operate-schema <-s source ...> <-d database> <-t table> [schema-file]", + Use: "operate-schema <-s source ...> <-d database> <-t table> [schema-file]", Short: "get/set/remove the schema for an upstream table", RunE: operateSchemaCmd, } @@ -59,7 +59,7 @@ func operateSchemaCmd(cmd *cobra.Command, _ []string) (err error) { } opType := cmd.Flags().Arg(0) - taskName := cmd.Flags().Arg(1) + taskName := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(1)) schemaFile := cmd.Flags().Arg(2) var schemaContent []byte op := convertSchemaOpType(opType) diff --git a/dm/ctl/master/pause_task.go b/dm/ctl/master/pause_task.go index cb60c4fef6..d696804ed2 100644 --- a/dm/ctl/master/pause_task.go +++ b/dm/ctl/master/pause_task.go @@ -26,7 +26,7 @@ import ( // NewPauseTaskCmd creates a PauseTask command func NewPauseTaskCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "pause-task [-s source ...] ", + Use: "pause-task [-s source ...] ", Short: "pause a specified running task", RunE: pauseTaskFunc, } @@ -41,7 +41,7 @@ func pauseTaskFunc(cmd *cobra.Command, _ []string) (err error) { err = errors.New("please check output to see error") return } - name := cmd.Flags().Arg(0) + name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0)) sources, err := common.GetSourceArgs(cmd) if err != nil { diff --git a/dm/ctl/master/resume_task.go b/dm/ctl/master/resume_task.go index ddf7afa9fc..8715145776 100644 --- a/dm/ctl/master/resume_task.go +++ b/dm/ctl/master/resume_task.go @@ -26,7 +26,7 @@ import ( // NewResumeTaskCmd creates a ResumeTask command func NewResumeTaskCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "resume-task [-s source ...] ", + Use: "resume-task [-s source ...] ", Short: "resume a specified paused task", RunE: resumeTaskFunc, } @@ -41,7 +41,7 @@ func resumeTaskFunc(cmd *cobra.Command, _ []string) (err error) { err = errors.New("please check output to see error") return } - name := cmd.Flags().Arg(0) + name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0)) sources, err := common.GetSourceArgs(cmd) if err != nil { diff --git a/dm/ctl/master/stop_task.go b/dm/ctl/master/stop_task.go index f32239df64..90fd21ae3d 100644 --- a/dm/ctl/master/stop_task.go +++ b/dm/ctl/master/stop_task.go @@ -26,7 +26,7 @@ import ( // NewStopTaskCmd creates a StopTask command func NewStopTaskCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "stop-task [-s source ...] ", + Use: "stop-task [-s source ...] ", Short: "stop a specified task", RunE: stopTaskFunc, } @@ -41,7 +41,7 @@ func stopTaskFunc(cmd *cobra.Command, _ []string) (err error) { err = errors.New("please check output to see error") return } - name := cmd.Flags().Arg(0) + name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0)) sources, err := common.GetSourceArgs(cmd) if err != nil { diff --git a/tests/dmctl_basic/check_list/pause_task.sh b/tests/dmctl_basic/check_list/pause_task.sh index 0440b906f7..8275aeaa17 100644 --- a/tests/dmctl_basic/check_list/pause_task.sh +++ b/tests/dmctl_basic/check_list/pause_task.sh @@ -3,7 +3,7 @@ function pause_task_wrong_arg() { run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ "pause-task" \ - "pause-task \[-s source ...\] \[flags\]" 1 + "pause-task \[-s source ...\] \[flags\]" 1 } function pause_task_success() { diff --git a/tests/dmctl_basic/check_list/resume_task.sh b/tests/dmctl_basic/check_list/resume_task.sh index aa52a6988e..d1ae3c5deb 100644 --- a/tests/dmctl_basic/check_list/resume_task.sh +++ b/tests/dmctl_basic/check_list/resume_task.sh @@ -3,7 +3,7 @@ function resume_task_wrong_arg() { run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ "resume-task" \ - "resume-task \[-s source ...\] \[flags\]" 1 + "resume-task \[-s source ...\] \[flags\]" 1 } function resume_task_success() { diff --git a/tests/dmctl_basic/check_list/stop_task.sh b/tests/dmctl_basic/check_list/stop_task.sh index 87dfc10777..071801a2a6 100644 --- a/tests/dmctl_basic/check_list/stop_task.sh +++ b/tests/dmctl_basic/check_list/stop_task.sh @@ -3,5 +3,5 @@ function stop_task_wrong_arg() { run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ "stop-task" \ - "stop-task \[-s source ...\] \[flags\]" 1 + "stop-task \[-s source ...\] \[flags\]" 1 } diff --git a/tests/dmctl_basic/run.sh b/tests/dmctl_basic/run.sh index 2781431521..0ccd4ac0c0 100755 --- a/tests/dmctl_basic/run.sh +++ b/tests/dmctl_basic/run.sh @@ -194,6 +194,10 @@ function run() { query_status_running_tasks check_sync_diff $WORK_DIR $cur/conf/diff_config.toml 20 + # test use task file instead of task name + pause_task_success "$cur/conf/dm-task.yaml" + resume_task_success "$cur/conf/dm-task.yaml" + update_relay_success $cur/conf/source1.yaml $SOURCE_ID1 update_relay_success $cur/conf/source2.yaml $SOURCE_ID2 # check worker config backup file is correct