Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Implement matchdevfiles func
Browse files Browse the repository at this point in the history
Signed-off-by: thepetk <thepetk@gmail.com>
  • Loading branch information
thepetk committed Jun 4, 2023
1 parent 1478ab5 commit 1136776
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions go/pkg/apis/recognizer/devfile_recognizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"regexp"
"strings"

"github.com/hashicorp/go-version"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"github.com/redhat-developer/alizer/go/pkg/utils"
)
Expand Down Expand Up @@ -95,12 +96,25 @@ func SelectDevFileUsingLanguagesFromTypes(languages []model.Language, devFileTyp
return devFilesIndexes[0], nil
}

func MatchDevfiles(path, url, minVersion, maxVersion string) ([]model.DevFileType, error) {
devFileTypesFromRegistry, err := downloadDevFileTypesFromRegistry(url, minVersion, maxVersion)
if err != nil {
return []model.DevFileType{}, err
}

return selectDevfiles(path, devFileTypesFromRegistry)
}

func SelectDevFilesFromRegistry(path string, url string) ([]model.DevFileType, error) {
devFileTypesFromRegistry, err := downloadDevFileTypesFromRegistry(url)
devFileTypesFromRegistry, err := downloadDevFileTypesFromRegistry(url, "", "")
if err != nil {
return []model.DevFileType{}, err
}

return selectDevfiles(path, devFileTypesFromRegistry)
}

func selectDevfiles(path string, devFileTypesFromRegistry []model.DevFileType) ([]model.DevFileType, error) {
indexes, err := SelectDevFilesFromTypes(path, devFileTypesFromRegistry)
if err != nil {
return []model.DevFileType{}, err
Expand All @@ -112,10 +126,11 @@ func SelectDevFilesFromRegistry(path string, url string) ([]model.DevFileType, e
}

return devFileTypes, nil

}

func SelectDevFileFromRegistry(path string, url string) (model.DevFileType, error) {
devFileTypes, err := downloadDevFileTypesFromRegistry(url)
devFileTypes, err := downloadDevFileTypesFromRegistry(url, "", "")
if err != nil {
return model.DevFileType{}, err
}
Expand All @@ -127,17 +142,36 @@ func SelectDevFileFromRegistry(path string, url string) (model.DevFileType, erro
return devFileTypes[index], nil
}

func downloadDevFileTypesFromRegistry(url string) ([]model.DevFileType, error) {
func getUrlWithVersions(url, minVersion, maxVersion string) string {
if minVersion != "" && maxVersion != "" {
minV, err := version.NewVersion(minVersion)
if err != nil {
return url
}
maxV, err := version.NewVersion(minVersion)
if err != nil {
return url
}
if maxV.LessThan(minV) {
return url
}
return fmt.Sprintf("%s?minSchemaVersion=%s&maxSchemaVersion=%s", url, minVersion, maxVersion)
} else if minVersion != "" {
return fmt.Sprintf("%s?minSchemaVersion=%s", url, minVersion)
} else if maxVersion != "" {
return fmt.Sprintf("%s?maxSchemaVersion=%s", url, maxVersion)
} else {
return url
}
}

func downloadDevFileTypesFromRegistry(url, minVersion, maxVersion string) ([]model.DevFileType, error) {
url = adaptUrl(url)
// Get the data
tmpUrl := appendIndexPath(url)
url = getUrlWithVersions(tmpUrl, minVersion, maxVersion)
resp, err := http.Get(url)
if err != nil {
// retry by appending index to url
url = appendIndexPath(url)
resp, err = http.Get(url)
if err != nil {
return []model.DevFileType{}, err
}
return []model.DevFileType{}, err
}
defer func() error {
if err := resp.Body.Close(); err != nil {
Expand Down Expand Up @@ -167,9 +201,9 @@ func downloadDevFileTypesFromRegistry(url string) ([]model.DevFileType, error) {

func appendIndexPath(url string) string {
if strings.HasSuffix(url, "/") {
return url + "index"
return url + "v2index"
}
return url + "/index"
return url + "/v2index"
}

func adaptUrl(url string) string {
Expand Down

0 comments on commit 1136776

Please sign in to comment.