Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to dependency injection for the main CLI #6331

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cmd/tools/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package main
import (
"os"

"go.uber.org/zap"

"github.com/uber/cadence/tools/cli"
"github.com/uber/cadence/tools/common/commoncli"

Expand All @@ -35,6 +37,13 @@ import (
// Start using this CLI tool with command
// See cadence/tools/cli/README.md for usage
func main() {
app := cli.NewCliApp()
app := cli.NewCliApp(cli.NewClientFactory(must(zap.NewDevelopment())))
commoncli.ExitHandler(app.Run(os.Args))
}

func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
35 changes: 29 additions & 6 deletions tools/cli/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,28 @@
}
}

// may be better to do this inside an "ensure setup" in each method,
// but this reduces branches for testing.
func withDomainClient(c *cli.Context, admin bool, cb func(dc *domainCLIImpl) error) error {
dc, err := newDomainCLI(c, admin)
if err != nil {
return err

Check warning on line 376 in tools/cli/admin.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin.go#L376

Added line #L376 was not covered by tests
}
return cb(dc)
}

func newAdminDomainCommands() []*cli.Command {

return []*cli.Command{
{
Name: "register",
Aliases: []string{"re"},
Usage: "Register workflow domain",
Flags: adminRegisterDomainFlags,
Action: func(c *cli.Context) error {
return newDomainCLI(c, true).RegisterDomain(c)
return withDomainClient(c, true, func(dc *domainCLIImpl) error {
return dc.RegisterDomain(c)
})

Check warning on line 392 in tools/cli/admin.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin.go#L390-L392

Added lines #L390 - L392 were not covered by tests
},
},
{
Expand All @@ -385,7 +398,9 @@
Usage: "Update existing workflow domain",
Flags: adminUpdateDomainFlags,
Action: func(c *cli.Context) error {
return newDomainCLI(c, true).UpdateDomain(c)
return withDomainClient(c, true, func(dc *domainCLIImpl) error {
return dc.UpdateDomain(c)
})

Check warning on line 403 in tools/cli/admin.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin.go#L401-L403

Added lines #L401 - L403 were not covered by tests
},
},
{
Expand All @@ -394,7 +409,9 @@
Usage: "Deprecate existing workflow domain",
Flags: adminDeprecateDomainFlags,
Action: func(c *cli.Context) error {
return newDomainCLI(c, true).DeprecateDomain(c)
return withDomainClient(c, true, func(dc *domainCLIImpl) error {
return dc.DeprecateDomain(c)
})

Check warning on line 414 in tools/cli/admin.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin.go#L412-L414

Added lines #L412 - L414 were not covered by tests
},
},
{
Expand All @@ -403,7 +420,9 @@
Usage: "Describe existing workflow domain",
Flags: adminDescribeDomainFlags,
Action: func(c *cli.Context) error {
return newDomainCLI(c, true).DescribeDomain(c)
return withDomainClient(c, true, func(dc *domainCLIImpl) error {
return dc.DescribeDomain(c)
})

Check warning on line 425 in tools/cli/admin.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin.go#L423-L425

Added lines #L423 - L425 were not covered by tests
},
},
{
Expand Down Expand Up @@ -460,7 +479,9 @@
getFormatFlag(),
},
Action: func(c *cli.Context) error {
return newDomainCLI(c, false).ListDomains(c)
return withDomainClient(c, false, func(dc *domainCLIImpl) error {
return dc.ListDomains(c)
})

Check warning on line 484 in tools/cli/admin.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin.go#L482-L484

Added lines #L482 - L484 were not covered by tests
},
},
}
Expand Down Expand Up @@ -741,7 +762,9 @@
},
},
Action: func(c *cli.Context) error {
return newDomainCLI(c, false).FailoverDomains(c)
return withDomainClient(c, false, func(dc *domainCLIImpl) error {
return dc.FailoverDomains(c)
})

Check warning on line 767 in tools/cli/admin.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin.go#L765-L767

Added lines #L765 - L767 were not covered by tests
},
},
{
Expand Down
12 changes: 9 additions & 3 deletions tools/cli/admin_async_queue_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
)

func AdminGetAsyncWFConfig(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 38 in tools/cli/admin_async_queue_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_async_queue_commands.go#L36-L38

Added lines #L36 - L38 were not covered by tests
}

domainName := getRequiredOption(c, FlagDomain)

Expand All @@ -60,13 +63,16 @@
}

func AdminUpdateAsyncWFConfig(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 68 in tools/cli/admin_async_queue_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_async_queue_commands.go#L66-L68

Added lines #L66 - L68 were not covered by tests
}

domainName := getRequiredOption(c, FlagDomain)
asyncWFCfgJSON := getRequiredOption(c, FlagJSON)

var cfg types.AsyncWorkflowConfiguration
err := json.Unmarshal([]byte(asyncWFCfgJSON), &cfg)
err = json.Unmarshal([]byte(asyncWFCfgJSON), &cfg)

Check warning on line 75 in tools/cli/admin_async_queue_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_async_queue_commands.go#L75

Added line #L75 was not covered by tests
if err != nil {
return commoncli.Problem("Failed to parse async workflow config", err)
}
Expand Down
17 changes: 13 additions & 4 deletions tools/cli/admin_cluster_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
color.YellowString(key), color.YellowString(intValTypeToString(valType)))
promptFn(promptMsg)

adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 60 in tools/cli/admin_cluster_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_cluster_commands.go#L60

Added line #L60 was not covered by tests
}
ctx, cancel := newContext(c)
defer cancel()
request := &types.AddSearchAttributeRequest{
Expand All @@ -65,7 +68,7 @@
SecurityToken: c.String(FlagSecurityToken),
}

err := adminClient.AddSearchAttribute(ctx, request)
err = adminClient.AddSearchAttribute(ctx, request)
if err != nil {
return commoncli.Problem("Add search attribute failed.", err)
}
Expand All @@ -75,7 +78,10 @@

// AdminDescribeCluster is used to dump information about the cluster
func AdminDescribeCluster(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 83 in tools/cli/admin_cluster_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_cluster_commands.go#L81-L83

Added lines #L81 - L83 were not covered by tests
}

ctx, cancel := newContext(c)
defer cancel()
Expand All @@ -89,7 +95,10 @@
}

func AdminRebalanceStart(c *cli.Context) error {
client := getCadenceClient(c)
client, err := getCadenceClient(c)
if err != nil {
return err

Check warning on line 100 in tools/cli/admin_cluster_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_cluster_commands.go#L98-L100

Added lines #L98 - L100 were not covered by tests
}
tcCtx, cancel := newContext(c)
defer cancel()

Expand Down
62 changes: 46 additions & 16 deletions tools/cli/admin_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@
}

func describeMutableState(c *cli.Context) (*types.AdminDescribeWorkflowExecutionResponse, error) {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return nil, err

Check warning on line 170 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L170

Added line #L170 was not covered by tests
}

domain := getRequiredOption(c, FlagDomain)
wid := getRequiredOption(c, FlagWorkflowID)
Expand Down Expand Up @@ -196,7 +199,10 @@
workflowID := c.String(FlagWorkflowID)
runID := c.String(FlagRunID)
skipErrors := c.Bool(FlagSkipErrorMode)
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 204 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L202-L204

Added lines #L202 - L204 were not covered by tests
}

request := &types.AdminMaintainWorkflowRequest{
Domain: domainName,
Expand All @@ -209,7 +215,7 @@

ctx, cancel := newContext(c)
defer cancel()
_, err := adminClient.MaintainCorruptWorkflow(ctx, request)
_, err = adminClient.MaintainCorruptWorkflow(ctx, request)

Check warning on line 218 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L218

Added line #L218 was not covered by tests
if err != nil {
return commoncli.Problem("Operation AdminMaintainCorruptWorkflow failed.", err)
}
Expand All @@ -233,7 +239,10 @@
// useful if server is down somehow. However, we only support couple DB clients
// currently. If the server side hosts working, remote is a cleaner approach
if remote {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 244 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L242-L244

Added lines #L242 - L244 were not covered by tests
}
request := &types.AdminDeleteWorkflowRequest{
Domain: domain,
Execution: &types.WorkflowExecution{
Expand All @@ -242,7 +251,7 @@
},
SkipErrors: skipError,
}
_, err := adminClient.DeleteWorkflow(ctx, request)
_, err = adminClient.DeleteWorkflow(ctx, request)

Check warning on line 254 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L254

Added line #L254 was not covered by tests
if err != nil {
return commoncli.Problem("Operation AdminMaintainCorruptWorkflow failed.", err)
}
Expand Down Expand Up @@ -380,7 +389,10 @@

// AdminRemoveTask describes history host
func AdminRemoveTask(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 394 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L392-L394

Added lines #L392 - L394 were not covered by tests
}

shardID := getRequiredIntOption(c, FlagShardID)
taskID := getRequiredInt64Option(c, FlagTaskID)
Expand All @@ -402,7 +414,7 @@
ClusterName: clusterName,
}

err := adminClient.RemoveTask(ctx, req)
err = adminClient.RemoveTask(ctx, req)

Check warning on line 417 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L417

Added line #L417 was not covered by tests
if err != nil {
return commoncli.Problem("Remove task has failed", err)
}
Expand Down Expand Up @@ -461,7 +473,10 @@

// AdminCloseShard closes shard by shard id
func AdminCloseShard(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 478 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L476-L478

Added lines #L476 - L478 were not covered by tests
}
sid := getRequiredIntOption(c, FlagShardID)

ctx, cancel := newContext(c)
Expand All @@ -470,7 +485,7 @@
req := &types.CloseShardRequest{}
req.ShardID = int32(sid)

err := adminClient.CloseShard(ctx, req)
err = adminClient.CloseShard(ctx, req)

Check warning on line 488 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L488

Added line #L488 was not covered by tests
if err != nil {
return commoncli.Problem("Close shard task has failed", err)
}
Expand All @@ -484,7 +499,10 @@

// AdminDescribeShardDistribution describes shard distribution
func AdminDescribeShardDistribution(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 504 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L502-L504

Added lines #L502 - L504 were not covered by tests
}

ctx, cancel := newContext(c)
defer cancel()
Expand Down Expand Up @@ -529,7 +547,10 @@

// AdminDescribeHistoryHost describes history host
func AdminDescribeHistoryHost(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 552 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L550-L552

Added lines #L550 - L552 were not covered by tests
}

wid := c.String(FlagWorkflowID)
sid := c.Int(FlagShardID)
Expand Down Expand Up @@ -568,7 +589,10 @@

// AdminRefreshWorkflowTasks refreshes all the tasks of a workflow
func AdminRefreshWorkflowTasks(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 594 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L592-L594

Added lines #L592 - L594 were not covered by tests
}

domain := getRequiredOption(c, FlagDomain)
wid := getRequiredOption(c, FlagWorkflowID)
Expand All @@ -577,7 +601,7 @@
ctx, cancel := newContext(c)
defer cancel()

err := adminClient.RefreshWorkflowTasks(ctx, &types.RefreshWorkflowTasksRequest{
err = adminClient.RefreshWorkflowTasks(ctx, &types.RefreshWorkflowTasksRequest{

Check warning on line 604 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L604

Added line #L604 was not covered by tests
Domain: domain,
Execution: &types.WorkflowExecution{
WorkflowID: wid,
Expand All @@ -593,7 +617,10 @@

// AdminResetQueue resets task processing queue states
func AdminResetQueue(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 622 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L620-L622

Added lines #L620 - L622 were not covered by tests
}

shardID := getRequiredIntOption(c, FlagShardID)
clusterName := getRequiredOption(c, FlagCluster)
Expand All @@ -608,7 +635,7 @@
Type: common.Int32Ptr(int32(typeID)),
}

err := adminClient.ResetQueue(ctx, req)
err = adminClient.ResetQueue(ctx, req)

Check warning on line 638 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L638

Added line #L638 was not covered by tests
if err != nil {
return commoncli.Problem("Failed to reset queue", err)
}
Expand All @@ -618,7 +645,10 @@

// AdminDescribeQueue describes task processing queue states
func AdminDescribeQueue(c *cli.Context) error {
adminClient := cFactory.ServerAdminClient(c)
adminClient, err := getDeps(c).ServerAdminClient(c)
if err != nil {
return err

Check warning on line 650 in tools/cli/admin_commands.go

View check run for this annotation

Codecov / codecov/patch

tools/cli/admin_commands.go#L648-L650

Added lines #L648 - L650 were not covered by tests
}

shardID := getRequiredIntOption(c, FlagShardID)
clusterName := getRequiredOption(c, FlagCluster)
Expand Down
Loading
Loading