From 230a19863196c9d254976cd88b559768d3669b58 Mon Sep 17 00:00:00 2001 From: Uwe Krueger Date: Fri, 25 Oct 2024 16:43:50 +0200 Subject: [PATCH] fix double completion of option sets --- .../extensions/attrs/ociuploadattr/attr.go | 2 +- cmds/ocm/commands/ocmcmds/common/utils.go | 22 ++++++++++++++----- cmds/ocm/common/options/interfaces.go | 10 +++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/api/ocm/extensions/attrs/ociuploadattr/attr.go b/api/ocm/extensions/attrs/ociuploadattr/attr.go index eb3a07e318..529c1472f2 100644 --- a/api/ocm/extensions/attrs/ociuploadattr/attr.go +++ b/api/ocm/extensions/attrs/ociuploadattr/attr.go @@ -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) } diff --git a/cmds/ocm/commands/ocmcmds/common/utils.go b/cmds/ocm/commands/ocmcmds/common/utils.go index 5537d634b1..b5324d114c 100644 --- a/cmds/ocm/commands/ocmcmds/common/utils.go +++ b/cmds/ocm/commands/ocmcmds/common/utils.go @@ -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" @@ -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 } diff --git a/cmds/ocm/common/options/interfaces.go b/cmds/ocm/common/options/interfaces.go index e48cbad36c..68caba0413 100644 --- a/cmds/ocm/common/options/interfaces.go +++ b/cmds/ocm/common/options/interfaces.go @@ -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 } @@ -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