Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions pkg/provider/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,48 @@ type ProviderVersionCheckResult struct {
Error string `json:"error,omitempty"`
}

type sourceKind int
type SourceKind int

const (
sourceUnknown sourceKind = iota
sourceGitHub
sourceManifestURL
sourceLocal
SourceUnknown SourceKind = iota
SourceGitHub
SourceManifestURL
SourceLocal
)

// ClassifyVersionSource categorises a canonical source string into its source kind.
func ClassifyVersionSource(canonical string) sourceKind {
func ClassifyVersionSource(canonical string) SourceKind {
// Strip @version suffix if present; only the leftmost @ counts as a tag separator.
bare := canonical
if before, _, ok := strings.Cut(canonical, "@"); ok {
bare = before
}
switch {
case strings.HasPrefix(bare, "github.com/"):
return sourceGitHub
return SourceGitHub
case strings.HasPrefix(bare, "https://"), strings.HasPrefix(bare, "http://"):
return sourceManifestURL
return SourceManifestURL
case strings.HasPrefix(bare, "/"),
strings.HasPrefix(bare, "./"),
strings.HasPrefix(bare, "../"):
return sourceLocal
return SourceLocal
default:
return sourceUnknown
return SourceUnknown
}
}

// ListVersionsForSource dispatches to the appropriate lister based on source shape.
func ListVersionsForSource(source string, opts ListVersionsOptions) ([]ProviderVersion, error) {
switch ClassifyVersionSource(source) {
case sourceGitHub:
case SourceGitHub:
org, repo, ok := parseGitHubSourcePath(source)
if !ok {
return nil, fmt.Errorf("invalid github source: %s", source)
}
return ListGitHubReleases(GithubAPIBaseURL, org, repo, opts.IncludePrerelease)
case sourceManifestURL:
case SourceManifestURL:
return ListManifestVersions(source, opts.IncludePrerelease)
case sourceLocal, sourceUnknown:
case SourceLocal, SourceUnknown:
return nil, ErrVersionListUnsupported
}
return nil, ErrVersionListUnsupported
Expand Down
14 changes: 7 additions & 7 deletions pkg/provider/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func TestProviderVersionFields(t *testing.T) {
func TestClassifyVersionSource(t *testing.T) {
cases := []struct {
in string
kind sourceKind
kind SourceKind
}{
{"github.com/devsy-org/devsy-provider-aws@v1.2.0", sourceGitHub},
{"github.com/devsy-org/devsy-provider-aws", sourceGitHub},
{"https://example.com/foo/provider.yaml", sourceManifestURL},
{"https://example.com/foo/provider.yaml@v1.0.0", sourceManifestURL},
{"/abs/path/provider.yaml", sourceLocal},
{"./relative/provider.yaml", sourceLocal},
{"github.com/devsy-org/devsy-provider-aws@v1.2.0", SourceGitHub},
{"github.com/devsy-org/devsy-provider-aws", SourceGitHub},
{"https://example.com/foo/provider.yaml", SourceManifestURL},
{"https://example.com/foo/provider.yaml@v1.0.0", SourceManifestURL},
{"/abs/path/provider.yaml", SourceLocal},
{"./relative/provider.yaml", SourceLocal},
}
for _, c := range cases {
t.Run(c.in, func(t *testing.T) {
Expand Down
Loading