Skip to content

Commit

Permalink
Adding function to add a single attestor (#128)
Browse files Browse the repository at this point in the history
* adding function for adding a single attestor

Signed-off-by: chaosinthecrd <[email protected]>

* changing to GetXXX and creating separate attestor error

Signed-off-by: chaosinthecrd <[email protected]>

---------

Signed-off-by: chaosinthecrd <[email protected]>
  • Loading branch information
ChaosInTheCRD authored Jan 16, 2024
1 parent 404b654 commit 61576e0
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion attestation/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -86,14 +92,32 @@ func FactoryByName(name string) (registry.FactoryFunc[Attestor], bool) {
return registrationEntry.Factory, ok
}

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 GetAttestors(nameOrTypes)
}

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)
}
}

Expand Down

0 comments on commit 61576e0

Please sign in to comment.