From 761c5aae03cbb4c2d25b72c181bb63c4079d6039 Mon Sep 17 00:00:00 2001 From: Uwe Krueger Date: Fri, 22 Nov 2024 11:05:07 +0100 Subject: [PATCH] provide dedicated decision factories --- api/ocm/plugin/ppi/utils.go | 41 +++++++++++- cmds/transferplugin/transferhandlers/demo.go | 65 ++++++++------------ 2 files changed, 65 insertions(+), 41 deletions(-) diff --git a/api/ocm/plugin/ppi/utils.go b/api/ocm/plugin/ppi/utils.go index c05c1d170c..beecb5f0c5 100644 --- a/api/ocm/plugin/ppi/utils.go +++ b/api/ocm/plugin/ppi/utils.go @@ -193,13 +193,17 @@ func ComponentVersionQuestionFunc(f func(p Plugin, question *ComponentVersionQue } } -func ComponentReferenceQuestionFunc(f func(p Plugin, question *ComponentReferenceQuestionArguments) (bool, error)) QuestionResultFunc { +type ComponentReferenceQuestionFunc = func(p Plugin, question *ComponentReferenceQuestionArguments) (bool, error) + +func ForComponentReferenceQuestion(f func(p Plugin, question *ComponentReferenceQuestionArguments) (bool, error)) QuestionResultFunc { return func(p Plugin, question QuestionArguments) (bool, error) { return f(p, question.(*ComponentReferenceQuestionArguments)) } } -func ArtifactQuestionFunc(f func(p Plugin, question *ArtifactQuestionArguments) (bool, error)) QuestionResultFunc { +type ArtifactQuestionFunc = func(p Plugin, question *ArtifactQuestionArguments) (bool, error) + +func ForArtifactQuestion(f ArtifactQuestionFunc) QuestionResultFunc { return func(p Plugin, question QuestionArguments) (bool, error) { return f(p, question.(*ArtifactQuestionArguments)) } @@ -212,7 +216,7 @@ type defaultDecisionHandler struct { // NewDecisionHandler provides a default decision handler based on its standard // fields and a handler function. -func NewDecisionHandler(q, desc string, h func(p Plugin, question QuestionArguments) (bool, error), labels ...string) DecisionHandler { +func NewDecisionHandler(q, desc string, h QuestionResultFunc, labels ...string) DecisionHandler { return &defaultDecisionHandler{ DecisionHandlerBase: NewDecisionHandlerBase(q, desc, labels...), handler: h, @@ -223,6 +227,37 @@ func (d *defaultDecisionHandler) DecideOn(p Plugin, question QuestionArguments) return d.handler(p, question) } +//////////////////////////////////////////////////////////////////////////////// +// specialized handler creation + +func NewTransferResourceDecision(desc string, h ArtifactQuestionFunc, labels ...string) DecisionHandler { + return NewDecisionHandler(Q_TRANSFER_RESOURCE, desc, ForArtifactQuestion(h)) +} + +func NewTransferSourceDecision(desc string, h ArtifactQuestionFunc, labels ...string) DecisionHandler { + return NewDecisionHandler(Q_TRANSFER_SOURCE, desc, ForArtifactQuestion(h)) +} + +func NewEnforceTransportDesision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler { + return NewDecisionHandler(Q_ENFORCE_TRANSPORT, desc, ForComponentReferenceQuestion(h)) +} + +func NewTransferversionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler { + return NewDecisionHandler(Q_TRANSFER_VERSION, desc, ForComponentReferenceQuestion(h)) +} + +func NewOverwriteVersionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler { + return NewDecisionHandler(Q_OVERWRITE_VERSION, desc, ForComponentReferenceQuestion(h)) +} + +func NewUpdateVersionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler { + return NewDecisionHandler(Q_UPDATE_VERSION, desc, ForComponentReferenceQuestion(h)) +} + +func NewOUpdateVersionDecision(desc string, h ComponentReferenceQuestionFunc, labels ...string) DecisionHandler { + return NewDecisionHandler(Q_UPDATE_VERSION, desc, ForComponentReferenceQuestion(h)) +} + //////////////////////////////////////////////////////////////////////////////// // Config is a generic structured config stored in a string map. diff --git a/cmds/transferplugin/transferhandlers/demo.go b/cmds/transferplugin/transferhandlers/demo.go index e7f621bd9e..c1a4880243 100644 --- a/cmds/transferplugin/transferhandlers/demo.go +++ b/cmds/transferplugin/transferhandlers/demo.go @@ -3,7 +3,6 @@ package transferhandlers import ( "slices" - "github.com/mandelsoft/goutils/general" "github.com/mandelsoft/goutils/optionutils" "ocm.software/ocm/api/ocm/plugin/ppi" "ocm.software/ocm/cmds/transferplugin/config" @@ -15,51 +14,41 @@ const ( func New() ppi.TransferHandler { h := ppi.NewTransferHandler(NAME, "enable value transport for dedicated external repositories") - h.RegisterDecision(NewDecision(ppi.Q_TRANSFER_RESOURCE, func(options *ppi.TransferOptions) bool { return optionutils.AsBool(options.ResourcesByValue) }, - `value transport only for dedicated access types and service hosts`)) - h.RegisterDecision(NewDecision(ppi.Q_TRANSFER_SOURCE, func(options *ppi.TransferOptions) bool { return optionutils.AsBool(options.SourcesByValue) }, - `value transport only for dedicated access types and service hosts`)) - return h -} -type DecisionHandler struct { - ppi.DecisionHandlerBase - optfunc func(opts *ppi.TransferOptions) bool -} + h.RegisterDecision(ppi.NewTransferResourceDecision(`value transport only for dedicated access types and service hosts`, + ForOptions(func(options *ppi.TransferOptions) bool { return optionutils.AsBool(options.ResourcesByValue) }))) -var _ ppi.DecisionHandler = (*DecisionHandler)(nil) - -func NewDecision(typ string, optfunc func(opts *ppi.TransferOptions) bool, desc ...string) ppi.DecisionHandler { - return &DecisionHandler{ - DecisionHandlerBase: ppi.NewDecisionHandlerBase(typ, general.Optional(desc...)), - optfunc: optfunc, - } + h.RegisterDecision(ppi.NewTransferSourceDecision(`value transport only for dedicated access types and service hosts`, + ForOptions(func(options *ppi.TransferOptions) bool { return optionutils.AsBool(options.SourcesByValue) }))) + return h } -func (d DecisionHandler) DecideOn(p ppi.Plugin, question ppi.QuestionArguments) (bool, error) { - q := question.(*ppi.ArtifactQuestionArguments) +type OptionFunc func(opts *ppi.TransferOptions) bool - var cfg *config.Config +func ForOptions(f OptionFunc) ppi.ArtifactQuestionFunc { + return func(p ppi.Plugin, question *ppi.ArtifactQuestionArguments) (bool, error) { + var cfg *config.Config - if q.Options.Special == nil { - c, err := p.GetConfig() - if c == nil || err != nil { - return false, err - } - cfg = c.(*config.Config) - } else { - c, err := config.GetConfig(*q.Options.Special) - if err != nil { - return false, err - } - if c != nil { + if question.Options.Special == nil { + c, err := p.GetConfig() + if c == nil || err != nil { + return false, err + } cfg = c.(*config.Config) + } else { + c, err := config.GetConfig(*question.Options.Special) + if err != nil { + return false, err + } + if c != nil { + cfg = c.(*config.Config) + } } - } - if list := cfg.TransferRepositories.Types[q.Artifact.AccessInfo.Kind]; list != nil { - host := q.Artifact.AccessInfo.Host - return slices.Contains(list, host), nil + if list := cfg.TransferRepositories.Types[question.Artifact.AccessInfo.Kind]; list != nil { + host := question.Artifact.AccessInfo.Host + return slices.Contains(list, host), nil + } + return f(&question.Options), nil } - return d.optfunc(&q.Options), nil }