From c11bb492aaca0c78c2c6eec54bec81f8b8f0cadc Mon Sep 17 00:00:00 2001 From: chaosinthecrd Date: Tue, 16 Jan 2024 17:03:07 +0000 Subject: [PATCH 1/2] adding function for adding a single attestor Signed-off-by: chaosinthecrd --- attestation/factory.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/attestation/factory.go b/attestation/factory.go index abc0bb4d..58c70889 100644 --- a/attestation/factory.go +++ b/attestation/factory.go @@ -86,7 +86,20 @@ func FactoryByName(name string) (registry.FactoryFunc[Attestor], bool) { return registrationEntry.Factory, ok } +func AddAttestor(nameOrType string) (Attestor, error) { + attestors, err := AddAttestors([]string{nameOrType}) + if err != nil { + return nil, err + } + return attestors[0], nil +} + +// Deprecated: use AddAttestors instead func Attestors(nameOrTypes []string) ([]Attestor, error) { + return AddAttestors(nameOrTypes) +} + +func AddAttestors(nameOrTypes []string) ([]Attestor, error) { attestors := make([]Attestor, 0) for _, nameOrType := range nameOrTypes { factory, ok := FactoryByName(nameOrType) From a8b6fce39ba6e2ece176d97bd2ea8c8b737cf3c6 Mon Sep 17 00:00:00 2001 From: chaosinthecrd Date: Tue, 16 Jan 2024 17:32:22 +0000 Subject: [PATCH 2/2] changing to GetXXX and creating separate attestor error Signed-off-by: chaosinthecrd --- attestation/factory.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/attestation/factory.go b/attestation/factory.go index 58c70889..a78bfabb 100644 --- a/attestation/factory.go +++ b/attestation/factory.go @@ -70,6 +70,12 @@ func (e ErrAttestationNotFound) Error() string { return fmt.Sprintf("attestation not found: %v", string(e)) } +type ErrAttestorNotFound string + +func (e ErrAttestorNotFound) Error() string { + return fmt.Sprintf("attestor not found: %v", string(e)) +} + func RegisterAttestation(name, predicateType string, run RunType, factoryFunc registry.FactoryFunc[Attestor], opts ...registry.Configurer) { registrationEntry := attestorRegistry.Register(name, factoryFunc, opts...) attestationsByType[predicateType] = registrationEntry @@ -86,27 +92,32 @@ func FactoryByName(name string) (registry.FactoryFunc[Attestor], bool) { return registrationEntry.Factory, ok } -func AddAttestor(nameOrType string) (Attestor, error) { - attestors, err := AddAttestors([]string{nameOrType}) +func GetAttestor(nameOrType string) (Attestor, error) { + attestors, err := GetAttestors([]string{nameOrType}) if err != nil { return nil, err } + + if len(attestors) == 0 { + return nil, ErrAttestorNotFound(nameOrType) + } + return attestors[0], nil } // Deprecated: use AddAttestors instead func Attestors(nameOrTypes []string) ([]Attestor, error) { - return AddAttestors(nameOrTypes) + return GetAttestors(nameOrTypes) } -func AddAttestors(nameOrTypes []string) ([]Attestor, error) { +func GetAttestors(nameOrTypes []string) ([]Attestor, error) { attestors := make([]Attestor, 0) for _, nameOrType := range nameOrTypes { factory, ok := FactoryByName(nameOrType) if !ok { factory, ok = FactoryByType(nameOrType) if !ok { - return nil, ErrAttestationNotFound(nameOrType) + return nil, ErrAttestorNotFound(nameOrType) } }