Skip to content

Commit

Permalink
fix double completion of option sets
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Oct 25, 2024
1 parent d1a956d commit 230a198
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/ocm/extensions/attrs/ociuploadattr/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (a AttributeType) Decode(data []byte, unmarshaller runtime.Unmarshaler) (in
return &value, nil
}
if value.Ref == "" {
return nil, errors.ErrInvalidWrap(errors.Newf("missing repository or ref"), oci.KIND_OCI_REFERENCE, string(data))
return nil, errors.ErrInvalidWrap(errors.Newf("missing repository or ociRef"), oci.KIND_OCI_REFERENCE, string(data))
}
data = []byte(value.Ref)
}
Expand Down
22 changes: 16 additions & 6 deletions cmds/ocm/commands/ocmcmds/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/general"

clictx "ocm.software/ocm/api/cli"
"ocm.software/ocm/api/ocm"
Expand Down Expand Up @@ -66,20 +67,29 @@ func MapArgsToIdentityPattern(args ...string) (metav1.Identity, error) {

////////////////////////////////////////////////////////////////////////////////

// OptionWithSessionCompleter describes the interface for option objects requiring
// a completion with a session.
type OptionWithSessionCompleter interface {
CompleteWithSession(ctx clictx.OCM, session ocm.Session) error
}

func CompleteOptionsWithSession(ctx clictx.Context, session ocm.Session) options.OptionsProcessor {
// CompleteOptionsWithSession provides an options.OptionsProcessor completing
// options by passing a session object using the OptionWithSessionCompleter interface.
// If an optional argument true is given, it also tries the other standard completion
// methods possible for an options object.
func CompleteOptionsWithSession(ctx clictx.Context, session ocm.Session, all ...bool) options.OptionsProcessor {
otherCompleters := general.Optional(all...)
return func(opt options.Options) error {
if c, ok := opt.(OptionWithSessionCompleter); ok {
return c.CompleteWithSession(ctx.OCM(), session)
}
if c, ok := opt.(options.OptionWithCLIContextCompleter); ok {
return c.Configure(ctx)
}
if c, ok := opt.(options.SimpleOptionCompleter); ok {
return c.Complete()
if otherCompleters {
if c, ok := opt.(options.OptionWithCLIContextCompleter); ok {
return c.Configure(ctx)
}
if c, ok := opt.(options.SimpleOptionCompleter); ok {
return c.Complete()
}
}
return nil
}
Expand Down
10 changes: 10 additions & 0 deletions cmds/ocm/common/options/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ import (
"ocm.software/ocm/api/utils/out"
)

// OptionsProcessor is handler used to process all
// option found in a set of options.
type OptionsProcessor func(Options) error

// SimpleOptionCompleter describes the interface for an option object
// requirung completion without any further information.
type SimpleOptionCompleter interface {
Complete() error
}

// OptionWithOutputContextCompleter describes the interface for an option object
// requirung completion with an output context.
type OptionWithOutputContextCompleter interface {
Complete(ctx out.Context) error
}

// OptionWithCLIContextCompleter describes the interface for an option object
// requirung completion with a CLI context.
type OptionWithCLIContextCompleter interface {
Configure(ctx clictx.Context) error
}
Expand Down Expand Up @@ -144,6 +152,8 @@ func (s OptionSet) Get(proto interface{}) bool {
return false
}

// ProcessOnOptions processes all options found in the option set
// woth a given OptionsProcessor.
func (s OptionSet) ProcessOnOptions(f OptionsProcessor) error {
for _, n := range s {
var err error
Expand Down

0 comments on commit 230a198

Please sign in to comment.