Skip to content

Commit

Permalink
chore: adding more required functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Pritesh Bandi <[email protected]>
  • Loading branch information
priteshbandi committed Dec 23, 2023
1 parent c8c68d2 commit d5393d9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
11 changes: 8 additions & 3 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"os"
"reflect"
"strings"

"github.com/notaryproject/notation-plugin-framework-go/internal/slices"
"github.com/notaryproject/notation-plugin-framework-go/log"
Expand All @@ -26,17 +27,21 @@ type CLI struct {
}

// New creates a new CLI using given plugin
func New(executableName string, pl plugin.Plugin) *CLI {
func New(executableName string, pl plugin.Plugin) (*CLI, error) {
return NewWithLogger(executableName, pl, &discardLogger{})
}

// NewWithLogger creates a new CLI using given plugin and logger
func NewWithLogger(executableName string, pl plugin.Plugin, l log.Logger) *CLI {
func NewWithLogger(executableName string, pl plugin.Plugin, l log.Logger) (*CLI, error) {
if strings.HasPrefix(executableName, plugin.BinaryPrefix) {
return nil, fmt.Errorf("executable name must start with prefix: %s", plugin.BinaryPrefix)
}

return &CLI{
name: executableName,
pl: pl,
logger: l,
}
}, nil
}

// Execute is main controller that reads/validates commands, parses input, executes relevant plugin functions
Expand Down
8 changes: 4 additions & 4 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func TestMarshalResponse(t *testing.T) {
func TestMarshalResponseError(t *testing.T) {

_, err := cli.marshalResponse(nil, fmt.Errorf("expected error thrown"))
assertErr(t, err, plugin.CodeGeneric)
assertErr(t, err, plugin.ErrorCodeGeneric)

_, err = cli.marshalResponse(nil, plugin.NewValidationError("expected validation error thrown"))
assertErr(t, err, plugin.CodeValidation)
assertErr(t, err, plugin.ErrorCodeValidation)

_, err = cli.marshalResponse(make(chan int), nil)
assertErr(t, err, plugin.CodeGeneric)
assertErr(t, err, plugin.ErrorCodeGeneric)
}

func TestUnmarshalRequest(t *testing.T) {
Expand Down Expand Up @@ -129,7 +129,7 @@ func setupReader(content string) func() {
}
}

func assertErr(t *testing.T, err error, code plugin.Code) {
func assertErr(t *testing.T, err error, code plugin.ErrorCode) {
if plgErr, ok := err.(*plugin.Error); ok {
if reflect.DeepEqual(code, plgErr.ErrCode) {
return
Expand Down
9 changes: 7 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ func main() {
// Initialize plugin
plugin, err := NewExamplePlugin()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to initialize plugin")
_, _ = fmt.Fprintf(os.Stderr, "failed to initialize plugin: %v", err)
os.Exit(2)
}

// Create executable
cli.New("example", plugin).Execute(ctx, os.Args)
pluginCli, err := cli.New("notation-example", plugin)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to create executable: %v", err)
os.Exit(3)
}
pluginCli.Execute(ctx, os.Args)
}
34 changes: 18 additions & 16 deletions plugin/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"fmt"
)

type Code string
type ErrorCode string

const (
CodeValidation Code = "VALIDATION_ERROR"
CodeUnsupportedContractVersion Code = "UNSUPPORTED_CONTRACT_VERSION"
CodeAccessDenied Code = "ACCESS_DENIED"
CodeThrottled Code = "THROTTLED"
CodeGeneric Code = "ERROR"
ErrorCodeValidation ErrorCode = "VALIDATION_ERROR"
ErrorCodeUnsupportedContractVersion ErrorCode = "UNSUPPORTED_CONTRACT_VERSION"
ErrorCodeAccessDenied ErrorCode = "ACCESS_DENIED"
ErrorCodeTimeout ErrorCode = "TIMEOUT"
ErrorCodeThrottled ErrorCode = "THROTTLED"
ErrorCodeGeneric ErrorCode = "ERROR"
)

const (
Expand All @@ -23,39 +24,40 @@ const (
// Error is used when the signature associated is no longer
// valid.
type Error struct {
ErrCode Code `json:"errorCode"`
Msg string `json:"errorMessage"`
ErrCode ErrorCode `json:"errorCode"`
Message string `json:"errorMessage,omitempty"`
Metadata map[string]string `json:"errorMetadata,omitempty"`
}

func NewError(code Code, msg string) *Error {
func NewError(code ErrorCode, msg string) *Error {
return &Error{
ErrCode: code,
Msg: msg,
Message: msg,
}
}

func NewGenericError(msg string) *Error {
return NewError(CodeGeneric, msg)
return NewError(ErrorCodeGeneric, msg)
}

func NewGenericErrorf(format string, msg string) *Error {
return NewError(CodeGeneric, fmt.Sprintf(format, msg))
return NewError(ErrorCodeGeneric, fmt.Sprintf(format, msg))
}

func NewUnsupportedError(msg string) *Error {
return NewError(CodeValidation, msg+" is not supported")
return NewError(ErrorCodeValidation, msg+" is not supported")
}

func NewValidationError(msg string) *Error {
return NewError(CodeValidation, msg)
return NewError(ErrorCodeValidation, msg)
}

func NewValidationErrorf(format string, msg string) *Error {
return NewError(CodeValidation, fmt.Sprintf(format, msg))
return NewError(ErrorCodeValidation, fmt.Sprintf(format, msg))
}

func NewUnsupportedContractVersionError(version string) *Error {
return NewError(CodeUnsupportedContractVersion, fmt.Sprintf("%q is not a supported notary plugin contract version", version))
return NewError(ErrorCodeUnsupportedContractVersion, fmt.Sprintf("%q is not a supported notary plugin contract version", version))
}

func NewJSONParsingError(msg string) *Error {
Expand Down
11 changes: 11 additions & 0 deletions plugin/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
// and notation external plugin.
package plugin

// BinaryPrefix is the prefix required on all plugin binary names.
const BinaryPrefix = "notation-"

// ContractVersion is the <major>.<minor> version of the plugin contract.
const ContractVersion = "1.0"

// Capability is a feature available in the plugin contract.
type Capability string

Expand All @@ -26,6 +32,11 @@ const (
// Command is a CLI command available in the plugin contract.
type Command string

// Request defines a plugin request, which is always associated to a command.
type Request interface {
Command() Command
}

const (
// CommandGetMetadata is the name of the plugin command
// which must be supported by every plugin and returns the
Expand Down

0 comments on commit d5393d9

Please sign in to comment.