Skip to content

Commit

Permalink
CR feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Pritesh Bandi <[email protected]>
  • Loading branch information
priteshbandi committed Aug 21, 2023
1 parent 76ea78e commit 02bc34d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
30 changes: 12 additions & 18 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ import (
"github.com/notaryproject/notation-plugin-framework-go/plugin"
)

type Cli struct {
syntax string
pl plugin.Plugin
type CLI struct {
name string
pl plugin.Plugin
}

// New creates a new Cli using given plugin
func New(pl plugin.Plugin) *Cli {
return &Cli{pl: pl}
// New creates a new CLI using given plugin
func New(executableName string, pl plugin.Plugin) *CLI {
return &CLI{
name: executableName,
pl: pl}
}

// Execute is main controller that reads/validates commands, parses input, executes relevant plugin functions
// and returns corresponding output.
func (c Cli) Execute(ctx context.Context, args []string) {
func (c CLI) Execute(ctx context.Context, args []string) {
c.validateArgs(ctx, args)

rescueStdOut := deferStdout()
Expand Down Expand Up @@ -92,27 +94,19 @@ func (c Cli) Execute(ctx context.Context, args []string) {
fmt.Println(op)
}

// printHelp prints help text for executable
func (c Cli) printHelp(ctx context.Context) {
md := getMetadata(ctx, c.pl)
args := getValidArgsString(md)

fmt.Printf("%s - %s\n Usage:%s %s", md.Name, md.Description, c.syntax, args)
}

// printVersion prints version of executable
func (c Cli) printVersion(ctx context.Context) {
func (c CLI) printVersion(ctx context.Context) {
md := getMetadata(ctx, c.pl)

fmt.Printf("%s - %s\nVersion: %s", md.Name, md.Description, md.Version)
os.Exit(0)
}

// validateArgs validate commands/arguments passed to executable.
func (c Cli) validateArgs(ctx context.Context, args []string) {
func (c CLI) validateArgs(ctx context.Context, args []string) {
md := getMetadata(ctx, c.pl)
if !(len(args) == 2 && slices.Contains(getValidArgs(md), args[1])) {
deliverError(fmt.Sprintf("Invalid command, valid choices are %s %s", c.syntax, getValidArgsString(md)))
deliverError(fmt.Sprintf("Invalid command, valid choices are: %s %s", c.name, getValidArgsString(md)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/priteshbandi/notation-plugin-base/exampleplugin
module github.com/priteshbandi/notation-plugin-base/example

go 1.20

require github.com/notaryproject/notation-plugin-framework-go v0.0.0-20230715005059-b8e1fc36cd93

replace github.com/notaryproject/notation-plugin-framework-go => ..
replace github.com/notaryproject/notation-plugin-framework-go => ../
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func main() {
}

// Create executable
cli.New(plugin).Execute(ctx, os.Args)
cli.New("example", plugin).Execute(ctx, os.Args)
}

0 comments on commit 02bc34d

Please sign in to comment.