Skip to content

Commit

Permalink
switch back, follow up later
Browse files Browse the repository at this point in the history
  • Loading branch information
sqin2019 committed Jun 28, 2023
1 parent 7016de7 commit a86b369
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
23 changes: 4 additions & 19 deletions pkg/cli/cli_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type CLIHandleCommand struct {
flagDebug bool

// Run Cleanup instead of Do if true.
flagCleanup bool
Cleanup bool

// testCLI is used for testing only.
testCLI string
Expand All @@ -50,21 +50,13 @@ func (c *CLIHandleCommand) Help() string {
return `
Usage: {{ COMMAND }} [options]
Handle "do" commands in the CLI request YAML file at the given path:
Handle CLI request YAML file at the given path:
{{ COMMAND }} -path "/path/to/file.yaml"
Handle "do" commands in the CLI request YAML file at the given path in debug mode:
Handle CLI request YAML file at the given path in debug mode:
{{ COMMAND }} -path "/path/to/file.yaml" -debug
Handle "cleanup" commands in the CLI request YAML file at the given path:
{{ COMMAND }} -path "/path/to/file.yaml" -cleanup
Handle "cleanup" commands in the CLI request YAML file at the given path in debug mode:
{{ COMMAND }} -path "/path/to/file.yaml" -cleanup -debug
`
}

Expand All @@ -82,13 +74,6 @@ func (c *CLIHandleCommand) Flags() *cli.FlagSet {
Usage: `The path of CLI request file, in YAML format.`,
})

f.BoolVar(&cli.BoolVar{
Name: "cleanup",
Target: &c.flagCleanup,
Default: false,
Usage: `Handle CLI request cleanup.`,
})

f.BoolVar(&cli.BoolVar{
Name: "debug",
Target: &c.flagDebug,
Expand Down Expand Up @@ -138,7 +123,7 @@ func (c *CLIHandleCommand) handle(ctx context.Context) error {
req.CLI = c.testCLI
}
var err error
if c.flagCleanup {
if c.Cleanup {
err = h.Cleanup(ctx, &req)
} else {
err = h.Do(ctx, &req)
Expand Down
11 changes: 8 additions & 3 deletions pkg/cli/cli_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ cleanup:
name string
args []string
testCLI string
cleanup bool
expOut string
expErr string
expStdErr string
Expand All @@ -82,14 +83,16 @@ Successfully completed commands`,
},
{
name: "success_cleanup",
args: []string{"-path", filepath.Join(dir, "valid.yaml"), "-cleanup"},
args: []string{"-path", filepath.Join(dir, "valid.yaml")},
testCLI: "echo",
cleanup: true,
expOut: `Successfully completed commands`,
},
{
name: "success_cleanup_with_debug",
args: []string{"-path", filepath.Join(dir, "valid.yaml"), "-debug", "-cleanup"},
args: []string{"-path", filepath.Join(dir, "valid.yaml"), "-debug"},
testCLI: "echo",
cleanup: true,
expOut: `
cleanup1
cleanup2
Expand Down Expand Up @@ -119,8 +122,9 @@ Successfully completed commands`,
},
{
name: "handler_cleanup_failure",
args: []string{"-path", filepath.Join(dir, "valid.yaml"), "-cleanup"},
args: []string{"-path", filepath.Join(dir, "valid.yaml")},
testCLI: "ls",
cleanup: true,
expErr: `failed to run command "cleanup1"`,
expStdErr: "ls: cannot access 'cleanup1': No such file or directory",
},
Expand All @@ -140,6 +144,7 @@ Successfully completed commands`,
ctx := logging.WithLogger(context.Background(), logging.TestLogger(t))

cmd := CLIHandleCommand{
Cleanup: tc.cleanup,
testCLI: tc.testCLI,
}
_, stdout, stderr := cmd.Pipe()
Expand Down
5 changes: 4 additions & 1 deletion pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ var rootCmd = func() cli.Command {
Name: "cli",
Description: "Perform operations related to the CLI request",
Commands: map[string]cli.CommandFactory{
"handle": func() cli.Command {
"do": func() cli.Command {
return &CLIHandleCommand{}
},
"cleanup": func() cli.Command {
return &CLIHandleCommand{Cleanup: true}
},
"validate": func() cli.Command {
return &CLIValidateCommand{}
},
Expand Down

0 comments on commit a86b369

Please sign in to comment.