Skip to content

Commit

Permalink
cli: support interactive cmd (#351)
Browse files Browse the repository at this point in the history
* cli: support interactive cmd

Signed-off-by: 5kbpers <tangminghua@pingcap.com>

* make check

Signed-off-by: 5kbpers <tangminghua@pingcap.com>
  • Loading branch information
5kbpers authored Mar 19, 2020
1 parent 137e959 commit abccd4c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
60 changes: 58 additions & 2 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package cmd

import (
"context"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"

"github.com/BurntSushi/toml"
"github.com/chzyer/readline"
_ "github.com/go-sql-driver/mysql" // mysql driver
"github.com/google/uuid"
"github.com/mattn/go-shellwords"
"github.com/pingcap/errors"
pd "github.com/pingcap/pd/client"
"github.com/pingcap/ticdc/cdc/kv"
Expand All @@ -23,7 +28,10 @@ import (
)

func init() {
rootCmd.AddCommand(newCliCommand())
cliCmd := newCliCommand()
cliCmd.PersistentFlags().StringVar(&cliPdAddr, "pd", "http://127.0.0.1:2379", "PD address")
cliCmd.PersistentFlags().BoolVarP(&interact, "interact", "i", false, "Run cdc cli with readline")
rootCmd.AddCommand(cliCmd)
}

var (
Expand All @@ -36,6 +44,8 @@ var (

cdcEtcdCli kv.CDCEtcdClient
pdCli pd.Client

interact bool
)

func newCliCommand() *cobra.Command {
Expand Down Expand Up @@ -69,6 +79,11 @@ func newCliCommand() *cobra.Command {

return nil
},
Run: func(cmd *cobra.Command, args []string) {
if interact {
loop()
}
},
}
command.AddCommand(
newCaptureCommand(),
Expand All @@ -77,7 +92,6 @@ func newCliCommand() *cobra.Command {
newMetadataCommand(),
newTsoCommand(),
)
command.PersistentFlags().StringVar(&cliPdAddr, "pd", "http://127.0.0.1:2379", "PD address")

return command
}
Expand Down Expand Up @@ -251,3 +265,45 @@ func strictDecodeFile(path, component string, cfg interface{}) error {

return errors.Trace(err)
}

func loop() {
l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
HistoryFile: "/tmp/readline.tmp",
InterruptPrompt: "^C",
EOFPrompt: "^D",
HistorySearchFold: true,
})
if err != nil {
panic(err)
}
defer l.Close()

for {
line, err := l.Readline()
if err != nil {
if err == readline.ErrInterrupt {
break
} else if err == io.EOF {
break
}
continue
}
if line == "exit" {
os.Exit(0)
}
args, err := shellwords.Parse(line)
if err != nil {
fmt.Printf("parse command err: %v\n", err)
continue
}

command := newCliCommand()
command.SetArgs(args)
_ = command.ParseFlags(args)
command.SetOutput((os.Stdout))
if err = command.Execute(); err != nil {
command.Println(err)
}
}
}
22 changes: 13 additions & 9 deletions cmd/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
)

var (
changefeedID string
captureID string
changefeedID *string
captureID *string
)

// cf holds changefeed id, which is used for output only
Expand Down Expand Up @@ -126,19 +126,20 @@ func newQueryChangefeedCommand() *cobra.Command {
Use: "query",
Short: "Query information and status of a replicaiton task (changefeed)",
RunE: func(cmd *cobra.Command, args []string) error {
info, err := cdcEtcdCli.GetChangeFeedInfo(context.Background(), changefeedID)
info, err := cdcEtcdCli.GetChangeFeedInfo(context.Background(), *changefeedID)
if err != nil && errors.Cause(err) != model.ErrChangeFeedNotExists {
return err
}
status, err := cdcEtcdCli.GetChangeFeedStatus(context.Background(), changefeedID)
status, err := cdcEtcdCli.GetChangeFeedStatus(context.Background(), *changefeedID)
if err != nil && errors.Cause(err) != model.ErrChangeFeedNotExists {
return err
}
meta := &cfMeta{Info: info, Status: status}
return jsonPrint(cmd, meta)
},
}
command.PersistentFlags().StringVar(&changefeedID, "changefeed-id", "", "Replication task (changefeed) ID")
changefeedID = command.PersistentFlags().String("changefeed-id", "", "Replication task (changefeed) ID")
_ = command.MarkPersistentFlagRequired("changefeed-id")
return command
}

Expand All @@ -147,20 +148,23 @@ func newQueryProcessorCommand() *cobra.Command {
Use: "query",
Short: "Query information and status of a sub replication task (processor)",
RunE: func(cmd *cobra.Command, args []string) error {
_, status, err := cdcEtcdCli.GetTaskStatus(context.Background(), changefeedID, captureID)
_, status, err := cdcEtcdCli.GetTaskStatus(context.Background(), *changefeedID, *captureID)
if err != nil && errors.Cause(err) != model.ErrTaskStatusNotExists {
return err
}
_, position, err := cdcEtcdCli.GetTaskPosition(context.Background(), changefeedID, captureID)
_, position, err := cdcEtcdCli.GetTaskPosition(context.Background(), *changefeedID, *captureID)
if err != nil && errors.Cause(err) != model.ErrTaskPositionNotExists {
return err
}
meta := &processorMeta{Status: status, Position: position}
return jsonPrint(cmd, meta)
},
}
command.PersistentFlags().StringVar(&changefeedID, "changefeed-id", "", "Replication task (changefeed) ID")
command.PersistentFlags().StringVar(&captureID, "capture-id", "", "Capture ID")
changefeedID = command.PersistentFlags().String("changefeed-id", "", "Replication task (changefeed) ID")
captureID = command.PersistentFlags().String("capture-id", "", "Capture ID")

_ = command.MarkPersistentFlagRequired("changefeed-id")
_ = command.MarkPersistentFlagRequired("capture-id")
return command
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/Shopify/sarama v1.26.1
github.com/biogo/store v0.0.0-20190426020002-884f370e325d
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3
github.com/edwingeng/deque v0.0.0-20191220032131-8596380dee17
github.com/go-sql-driver/mysql v1.4.1
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 // indirect
Expand All @@ -16,6 +17,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/klauspost/compress v1.10.2 // indirect
github.com/mattn/go-shellwords v1.0.3
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pingcap/check v0.0.0-20191216031241-8a5a85928f12
github.com/pingcap/errors v0.11.5-0.20190809092503-95897b64e011
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3 h1:T7Bw4H6z3WAZ2khw+gfKdYmbKHyy5xiHtk9IHfZqm7g=
github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa h1:OaNxuTZr7kxeODyLWsRMC+OD03aFUH+mW6r2d+MWa5Y=
Expand Down Expand Up @@ -214,6 +217,7 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-shellwords v1.0.3 h1:K/VxK7SZ+cvuPgFSLKi5QPI9Vr/ipOf4C1gN+ntueUk=
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down

0 comments on commit abccd4c

Please sign in to comment.