Skip to content

Commit f414ade

Browse files
authored
cli: add default context for command line usage (#3479)
close #2949
1 parent ed65c43 commit f414ade

25 files changed

+41
-68
lines changed

cmd/cdc/cli/cli.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ func NewCmdCli() *cobra.Command {
3737

3838
// Binding the `cli` command flags.
3939
cf.AddFlags(cmds)
40+
ctx, cancel := context.WithCancel(context.Background())
4041
cmds.PersistentPreRun = func(cmd *cobra.Command, args []string) {
4142
// Here we will initialize the logging configuration and set the current default context.
4243
err := logger.InitLogger(&logger.Config{Level: cf.GetLogLevel()})
4344
if err != nil {
4445
cmd.Printf("init logger error %v\n", errors.Trace(err))
4546
os.Exit(1)
4647
}
47-
_, cancel := context.WithCancel(context.Background())
48-
defer cancel()
48+
cmd.SetContext(ctx)
4949
util.LogHTTPProxies()
5050
// A notify that complete immediately, it skips the second signal essentially.
5151
doneNotify := func() <-chan struct{} {
@@ -59,7 +59,7 @@ func NewCmdCli() *cobra.Command {
5959
}
6060

6161
// Construct the client construction factory.
62-
f := factory.NewFactory(cf)
62+
f := factory.NewFactory(ctx, cf)
6363

6464
// Add subcommands.
6565
cmds.AddCommand(newCmdChangefeed(f))

cmd/cdc/cli/cli_capture_list.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
package cli
1515

1616
import (
17-
"context"
18-
1917
"github.com/pingcap/ticdc/cmd/cdc/factory"
2018
"github.com/pingcap/ticdc/cmd/util"
2119
apiv2client "github.com/pingcap/ticdc/pkg/api/v2"
@@ -52,7 +50,7 @@ type capture struct {
5250

5351
// run runs the `cli capture list` command.
5452
func (o *listCaptureOptions) run(cmd *cobra.Command) error {
55-
ctx := context.Background()
53+
ctx := cmd.Context()
5654
raw, err := o.apiv2Client.Captures().List(ctx)
5755
if err != nil {
5856
return err

cmd/cdc/cli/cli_changefeed_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func newCmdCreateChangefeed(f factory.Factory) *cobra.Command {
352352
Short: "Create a new replication task (changefeed)",
353353
Args: cobra.NoArgs,
354354
Run: func(cmd *cobra.Command, args []string) {
355-
ctx := context.Background()
355+
ctx := cmd.Context()
356356
util.CheckErr(o.complete(f))
357357
util.CheckErr(o.validate(cmd))
358358
util.CheckErr(o.run(ctx, cmd))

cmd/cdc/cli/cli_changefeed_list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package cli
1515

1616
import (
17-
"context"
1817
"time"
1918

2019
"github.com/pingcap/ticdc/api/owner"
@@ -66,7 +65,7 @@ func (o *listChangefeedOptions) complete(f factory.Factory) error {
6665

6766
// run the `cli changefeed list` command.
6867
func (o *listChangefeedOptions) run(cmd *cobra.Command) error {
69-
ctx := context.Background()
68+
ctx := cmd.Context()
7069

7170
raw, err := o.apiClient.Changefeeds().List(ctx, o.keyspace, "all")
7271
if err != nil {

cmd/cdc/cli/cli_changefeed_merge_table.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
package cli
1515

1616
import (
17-
"context"
18-
1917
"github.com/pingcap/ticdc/cmd/cdc/factory"
2018
"github.com/pingcap/ticdc/cmd/util"
2119
apiv2client "github.com/pingcap/ticdc/pkg/api/v2"
@@ -61,7 +59,7 @@ func (o *mergeTableChangefeedOptions) complete(f factory.Factory) error {
6159
// run the `cli changefeed move table` command.
6260
// return success or error message.
6361
func (o *mergeTableChangefeedOptions) run(cmd *cobra.Command) error {
64-
ctx := context.Background()
62+
ctx := cmd.Context()
6563

6664
err := o.apiClientV2.Changefeeds().MergeTable(ctx, o.keyspace, o.changefeedID, o.tableId, o.mode)
6765
var errStr string

cmd/cdc/cli/cli_changefeed_move_split_table.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
package cli
1515

1616
import (
17-
"context"
18-
1917
"github.com/pingcap/ticdc/cmd/cdc/factory"
2018
"github.com/pingcap/ticdc/cmd/util"
2119
apiv2client "github.com/pingcap/ticdc/pkg/api/v2"
@@ -64,7 +62,7 @@ func (o *moveSplitTableChangefeedOptions) complete(f factory.Factory) error {
6462
// run the `cli changefeed move table` command.
6563
// return success or error message.
6664
func (o *moveSplitTableChangefeedOptions) run(cmd *cobra.Command) error {
67-
ctx := context.Background()
65+
ctx := cmd.Context()
6866

6967
err := o.apiClientV2.Changefeeds().MoveSplitTable(ctx, o.keyspace, o.changefeedID, o.tableId, o.targetNodeID, o.mode)
7068
var errStr string

cmd/cdc/cli/cli_changefeed_move_table.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
package cli
1515

1616
import (
17-
"context"
18-
1917
"github.com/pingcap/ticdc/cmd/cdc/factory"
2018
"github.com/pingcap/ticdc/cmd/util"
2119
apiv2client "github.com/pingcap/ticdc/pkg/api/v2"
@@ -69,7 +67,7 @@ type response struct {
6967
// run the `cli changefeed move table` command.
7068
// return success or error message.
7169
func (o *moveTableChangefeedOptions) run(cmd *cobra.Command) error {
72-
ctx := context.Background()
70+
ctx := cmd.Context()
7371

7472
err := o.apiClientV2.Changefeeds().MoveTable(ctx, o.keyspace, o.changefeedID, o.tableId, o.targetNodeID, o.mode)
7573
var errStr string

cmd/cdc/cli/cli_changefeed_pause.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
package cli
1515

1616
import (
17-
"context"
18-
1917
"github.com/pingcap/ticdc/cmd/cdc/factory"
2018
"github.com/pingcap/ticdc/cmd/util"
2119
apiv2client "github.com/pingcap/ticdc/pkg/api/v2"
@@ -55,8 +53,8 @@ func (o *pauseChangefeedOptions) complete(f factory.Factory) error {
5553
}
5654

5755
// run the `cli changefeed pause` command.
58-
func (o *pauseChangefeedOptions) run() error {
59-
ctx := context.Background()
56+
func (o *pauseChangefeedOptions) run(cmd *cobra.Command) error {
57+
ctx := cmd.Context()
6058
return o.apiClient.Changefeeds().Pause(ctx, o.keyspace, o.changefeedID)
6159
}
6260

@@ -70,7 +68,7 @@ func newCmdPauseChangefeed(f factory.Factory) *cobra.Command {
7068
Args: cobra.NoArgs,
7169
Run: func(cmd *cobra.Command, args []string) {
7270
util.CheckErr(o.complete(f))
73-
util.CheckErr(o.run())
71+
util.CheckErr(o.run(cmd))
7472
},
7573
}
7674

cmd/cdc/cli/cli_changefeed_pause_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ func TestChangefeedPauseCli(t *testing.T) {
3838
o.changefeedID = "abc"
3939
o.keyspace = "test"
4040
require.Nil(t, o.complete(f))
41-
require.NotNil(t, o.run())
41+
require.NotNil(t, o.run(cmd))
4242
}

cmd/cdc/cli/cli_changefeed_query.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
package cli
1515

1616
import (
17-
"context"
18-
1917
"github.com/pingcap/errors"
2018
v2 "github.com/pingcap/ticdc/api/v2"
2119
"github.com/pingcap/ticdc/cmd/cdc/factory"
@@ -82,7 +80,7 @@ func (o *queryChangefeedOptions) complete(f factory.Factory) error {
8280

8381
// run the `cli changefeed query` command.
8482
func (o *queryChangefeedOptions) run(cmd *cobra.Command) error {
85-
ctx := context.Background()
83+
ctx := cmd.Context()
8684
if o.simplified {
8785
infos, err := o.apiClientV2.Changefeeds().List(ctx, o.keyspace, "all")
8886
if err != nil {

0 commit comments

Comments
 (0)