Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
ctl: support using task-file when need task-name (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
lance6716 authored Aug 4, 2020
1 parent f571300 commit 3ab722c
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 13 deletions.
19 changes: 19 additions & 0 deletions dm/ctl/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions dm/ctl/master/get_task_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// NewGetTaskCfgCmd creates a getTaskCfg command
func NewGetTaskCfgCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-task-config <task-name> [--file filename]",
Use: "get-task-config <task-name | task-file> [--file filename]",
Short: "get task config",
RunE: getTaskCfgFunc,
}
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions dm/ctl/master/operate_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// NewOperateSchemaCmd creates a OperateSchema command.
func NewOperateSchemaCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "operate-schema <operate-type> <-s source ...> <task-name> <-d database> <-t table> [schema-file]",
Use: "operate-schema <operate-type> <-s source ...> <task-name | task-file> <-d database> <-t table> [schema-file]",
Short: "get/set/remove the schema for an upstream table",
RunE: operateSchemaCmd,
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions dm/ctl/master/pause_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// NewPauseTaskCmd creates a PauseTask command
func NewPauseTaskCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pause-task [-s source ...] <task-name>",
Use: "pause-task [-s source ...] <task-name | task-file>",
Short: "pause a specified running task",
RunE: pauseTaskFunc,
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions dm/ctl/master/resume_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// NewResumeTaskCmd creates a ResumeTask command
func NewResumeTaskCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "resume-task [-s source ...] <task-name>",
Use: "resume-task [-s source ...] <task-name | task-file>",
Short: "resume a specified paused task",
RunE: resumeTaskFunc,
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions dm/ctl/master/stop_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// NewStopTaskCmd creates a StopTask command
func NewStopTaskCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "stop-task [-s source ...] <task-name>",
Use: "stop-task [-s source ...] <task-name | task-file>",
Short: "stop a specified task",
RunE: stopTaskFunc,
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/dmctl_basic/check_list/pause_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...\] <task-name> \[flags\]" 1
"pause-task \[-s source ...\] <task-name | task-file> \[flags\]" 1
}

function pause_task_success() {
Expand Down
2 changes: 1 addition & 1 deletion tests/dmctl_basic/check_list/resume_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...\] <task-name> \[flags\]" 1
"resume-task \[-s source ...\] <task-name | task-file> \[flags\]" 1
}

function resume_task_success() {
Expand Down
2 changes: 1 addition & 1 deletion tests/dmctl_basic/check_list/stop_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...\] <task-name> \[flags\]" 1
"stop-task \[-s source ...\] <task-name | task-file> \[flags\]" 1
}
4 changes: 4 additions & 0 deletions tests/dmctl_basic/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ab722c

Please sign in to comment.