Skip to content

Commit

Permalink
move check into a type-specific method rather than delegating it to c…
Browse files Browse the repository at this point in the history
…aller

Signed-off-by: Bob Callaway <bcallaway@google.com>
  • Loading branch information
bobcallaway committed Aug 26, 2022
1 parent 4edb32f commit 2dd8f31
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 1 addition & 3 deletions cmd/rekor-cli/app/pflag_groups.go
Expand Up @@ -26,8 +26,6 @@ import (
"github.com/sigstore/rekor/pkg/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"golang.org/x/exp/slices"
)

// addFlagToCmd adds the specified command of a specified type to the command's flag set
Expand Down Expand Up @@ -188,7 +186,7 @@ func ParseTypeFlag(typeStr string) (string, string, error) {
case 1:
return typeStrings[0], "", nil
case 2:
if !slices.Contains(ti.SupportedVersions(), typeStrings[1]) {
if !ti.IsSupportedVersion(typeStrings[1]) {
return "", "", fmt.Errorf("type %v does not support version %v", typeStrings[0], typeStrings[1])
}
return typeStrings[0], typeStrings[1], nil
Expand Down
8 changes: 8 additions & 0 deletions pkg/types/types.go
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/log"
"golang.org/x/exp/slices"
)

// TypeMap stores mapping between type strings and entry constructors
Expand All @@ -42,6 +43,7 @@ type TypeImpl interface {
CreateProposedEntry(context.Context, string, ArtifactProperties) (models.ProposedEntry, error)
DefaultVersion() string
SupportedVersions() []string
IsSupportedVersion(string) bool
UnmarshalEntry(pe models.ProposedEntry) (EntryImpl, error)
}

Expand All @@ -62,10 +64,16 @@ func (rt *RekorType) VersionedUnmarshal(pe models.ProposedEntry, version string)
return entry, entry.Unmarshal(pe)
}

// SupportedVersions returns a list of versions of this type that can be currently entered into the log
func (rt *RekorType) SupportedVersions() []string {
return rt.VersionMap.SupportedVersions()
}

// IsSupportedVersion returns true if the version can be inserted into the log, and false if not
func (rt *RekorType) IsSupportedVersion(proposedVersion string) bool {
return slices.Contains(rt.SupportedVersions(), proposedVersion)
}

// ListImplementedTypes returns a list of all type strings currently known to
// be implemented
func ListImplementedTypes() []string {
Expand Down

0 comments on commit 2dd8f31

Please sign in to comment.