Skip to content

Commit

Permalink
refactor: remove tool base command (#142)
Browse files Browse the repository at this point in the history
fix #134
  • Loading branch information
sqin2019 authored Dec 7, 2023
1 parent 0a55224 commit 4f72706
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 148 deletions.
138 changes: 0 additions & 138 deletions pkg/cli/tool_base.go

This file was deleted.

112 changes: 107 additions & 5 deletions pkg/cli/tool_do.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,118 @@ import (
"context"
"fmt"

"github.com/posener/complete/v2/predict"

"github.com/abcxyz/access-on-demand/apis/v1alpha1"
"github.com/abcxyz/access-on-demand/pkg/handler"
"github.com/abcxyz/access-on-demand/pkg/requestutil"
"github.com/abcxyz/pkg/cli"
)

var _ cli.Command = (*ToolDoCommand)(nil)

// toolHandler interface that handles ToolRequest.
type toolHandler interface {
Do(context.Context, *v1alpha1.ToolRequest) error
}

// ToolDoCommand handles tool requests "do" commands.
type ToolDoCommand struct {
ToolBaseCommand
cli.BaseCommand

flagPath string

flagVerbose bool

// testHandler is used for testing only.
testHandler toolHandler
}

func (c *ToolDoCommand) Desc() string {
return `Execute the "do" commands in request YAML file at the given path, ` +
`this command only works where bash is available`
}

func (c *ToolDoCommand) Help() string {
return `
Usage: {{ COMMAND }} [options]
Execute commands in tool request YAML file at the given path:
{{ COMMAND }} -path "/path/to/file.yaml"
Execute commands in tool request YAML file at the given path in debug mode:
{{ COMMAND }} -path "/path/to/file.yaml" -debug
Execute commands in tool request YAML file and output commands executed:
{{ COMMAND }} -path "/path/to/file.yaml" -verbose
`
}

func (c *ToolDoCommand) Flags() *cli.FlagSet {
set := c.NewFlagSet()

// Command options
f := set.NewSection("COMMAND OPTIONS")

f.StringVar(&cli.StringVar{
Name: "path",
Target: &c.flagPath,
Example: "/path/to/file.yaml",
Predict: predict.Files("*"),
Usage: `The path of tool request file, in YAML format.`,
})

f.BoolVar(&cli.BoolVar{
Name: "verbose",
Target: &c.flagVerbose,
Default: false,
Usage: `Turn on verbose mode to print commands output. Note that outputs may contain sensitive information`,
})

return set
}

func (c *ToolDoCommand) Run(ctx context.Context, args []string) error {
req, h, err := c.setup(ctx, args)
if err != nil {
return fmt.Errorf("failed to setup command: %w", err)
f := c.Flags()
if err := f.Parse(args); err != nil {
return fmt.Errorf("failed to parse flags: %w", err)
}
args = f.Args()
if len(args) > 0 {
return fmt.Errorf("unexpected arguments: %q", args)
}

if err := h.Do(ctx, req); err != nil {
if c.flagPath == "" {
return fmt.Errorf("path is required")
}

// Read request from file path.
var req v1alpha1.ToolRequest
if err := requestutil.ReadRequestFromPath(c.flagPath, &req); err != nil {
return fmt.Errorf("failed to read %T: %w", &req, err)
}

if err := v1alpha1.ValidateToolRequest(&req); err != nil {
return fmt.Errorf("failed to validate %T: %w", &req, err)
}

var h toolHandler
// Use testhandler if it is for testing.
if c.testHandler != nil {
h = c.testHandler
} else {
opts := []handler.ToolHandlerOption{handler.WithStderr(c.Stderr())}
if c.flagVerbose {
printHeader(c.Stdout(), "Tool Commands Output")
opts = append(opts, handler.WithStdout(c.Stdout()))
}
h = handler.NewToolHandler(ctx, opts...)
}

if err := h.Do(ctx, &req); err != nil {
return fmt.Errorf(`failed to run "do" commands: %w`, err)
}

Expand All @@ -49,3 +139,15 @@ func (c *ToolDoCommand) Run(ctx context.Context, args []string) error {

return nil
}

func (c *ToolDoCommand) output(subcmds []string, tool string) error {
printHeader(c.Stdout(), "Successfully Completed Commands")
cmds := make([]string, 0, len(subcmds))
for _, sub := range subcmds {
cmds = append(cmds, fmt.Sprintf("%s %s", tool, sub))
}
if err := encodeYaml(c.Stdout(), cmds); err != nil {
return fmt.Errorf("failed to output executed commands: %w", err)
}
return nil
}
6 changes: 1 addition & 5 deletions pkg/cli/tool_do_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ do:

ctx := logging.WithLogger(context.Background(), logging.TestLogger(t))

cmd := ToolDoCommand{
ToolBaseCommand: ToolBaseCommand{
testHandler: tc.testHandler,
},
}
cmd := ToolDoCommand{testHandler: tc.testHandler}
_, stdout, _ := cmd.Pipe()

args := append([]string{}, tc.args...)
Expand Down

0 comments on commit 4f72706

Please sign in to comment.