From 9d62fff0746700568f0e2fb74be7284ab31fd90c Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Mon, 22 Nov 2021 16:00:55 +0200 Subject: [PATCH 1/3] feat: recipe generate: load category and features --- cli/catalogue/generate.go | 6 +- pkg/catalogue/catalogue.go | 111 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/cli/catalogue/generate.go b/cli/catalogue/generate.go index d4490af4..f54c8674 100644 --- a/cli/catalogue/generate.go +++ b/cli/catalogue/generate.go @@ -165,6 +165,8 @@ A new catalogue copy can be published to the recipes repository by passing the logrus.Fatal(err) } + features, category, err := catalogue.GetRecipeFeaturesAndCategory(recipeMeta.Name) + catl[recipeMeta.Name] = catalogue.RecipeMeta{ Name: recipeMeta.Name, Repository: recipeMeta.CloneURL, @@ -173,8 +175,8 @@ A new catalogue copy can be published to the recipes repository by passing the Description: recipeMeta.Description, Website: recipeMeta.Website, Versions: versions, - // Category: ..., // FIXME: parse & load - // Features: ..., // FIXME: parse & load + Category: category, + Features: features, } catlBar.Add(1) } diff --git a/pkg/catalogue/catalogue.go b/pkg/catalogue/catalogue.go index b3a85700..22b2442d 100644 --- a/pkg/catalogue/catalogue.go +++ b/pkg/catalogue/catalogue.go @@ -45,6 +45,7 @@ type features struct { Image image `json:"image"` Status int `json:"status"` Tests string `json:"tests"` + SSO string `json:"sso"` } // tag represents a git tag. @@ -373,6 +374,116 @@ func ReadReposMetadata() (RepoCatalogue, error) { return reposMeta, nil } +func GetStringInBetween(str string, start string, end string) (result string) { + // GetStringInBetween returns empty string if no start or end string found + s := strings.Index(str, start) + if s == -1 { + return + } + s += len(start) + e := strings.Index(str[s:], end) + if e == -1 { + return + } + return str[s : s+e] +} + +func GetImageMetadata(imageRowString string) (image, error) { + img := image{} + + imgFields := strings.Split(imageRowString, ",") + + for i, elem := range imgFields { + imgFields[i] = strings.TrimSpace(elem) + } + + if len(imgFields) < 3 { + logrus.Warnf("Image string has incorrect format: %s", imageRowString) + return img, nil + } + + img.Rating = imgFields[1] + img.Source = imgFields[2] + + imgString := imgFields[0] + + img.Image = GetStringInBetween(imgString, "[", "]") + img.URL = GetStringInBetween(imgString, "(", ")") + + logrus.Debugf("Image: %s", img.Image) + logrus.Debugf("URL: %s", img.URL) + + return img, nil +} + +func GetRecipeFeaturesAndCategory(recipeName string) (features, string, error) { + feat := features{} + + var category string + + readmePath := path.Join(config.ABRA_DIR, "apps", recipeName, "README.md") + + logrus.Debugf("attempting to open '%s'", readmePath) + + readmeFS, err := ioutil.ReadFile(readmePath) + if err != nil { + return feat, category, err + } + + readmeLines := strings.Split( // Array item from lines + strings.ReplaceAll( // Remove \t tabs + GetStringInBetween( // Find text between delimiters + string(readmeFS), + "", "", + ), "\t", "", + ), + "\n") + + for _, val := range readmeLines { + if strings.Contains(val, "**Category**") { + category = strings.TrimSpace( + strings.TrimPrefix(val, "* **Category**:"), + ) + } + if strings.Contains(val, "**Backups**") { + feat.Backups = strings.TrimSpace( + strings.TrimPrefix(val, "* **Backups**:"), + ) + } + if strings.Contains(val, "**Email**") { + feat.Email = strings.TrimSpace( + strings.TrimPrefix(val, "* **Email**:"), + ) + } + if strings.Contains(val, "**SSO**") { + feat.SSO = strings.TrimSpace( + strings.TrimPrefix(val, "* **SSO**:"), + ) + } + if strings.Contains(val, "**Healthcheck**") { + feat.Healthcheck = strings.TrimSpace( + strings.TrimPrefix(val, "* **Healthcheck**:"), + ) + } + if strings.Contains(val, "**Tests**") { + feat.Tests = strings.TrimSpace( + strings.TrimPrefix(val, "* **Tests**:"), + ) + } + if strings.Contains(val, "**Image**") { + imageMetadata, err := GetImageMetadata(strings.TrimSpace( + strings.TrimPrefix(val, "* **Image**:"), + )) + if err != nil { + continue + } + feat.Image = imageMetadata + } + } + + return feat, category, nil +} + // GetRecipeVersions retrieves all recipe versions. func GetRecipeVersions(recipeName string) (RecipeVersions, error) { versions := RecipeVersions{} From 8635922b9f9723272bc2117f946d0b640848d142 Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Mon, 22 Nov 2021 16:11:32 +0200 Subject: [PATCH 2/3] fix: don't clobber recipe changes during `generate` Closes #255 --- cli/catalogue/generate.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cli/catalogue/generate.go b/cli/catalogue/generate.go index f54c8674..04101ac9 100644 --- a/cli/catalogue/generate.go +++ b/cli/catalogue/generate.go @@ -134,6 +134,15 @@ A new catalogue copy can be published to the recipes repository by passing the logrus.Fatal(err) } + isClean, err := gitPkg.IsClean(rm.Name) + if err != nil { + return + } + + if !isClean { + logrus.Fatalf("'%s' has locally unstaged changes", rm.Name) + } + if err := gitPkg.EnsureUpToDate(recipeDir); err != nil { logrus.Fatal(err) } From d0a30f6b7bedbbad5894bce152c5294320c5e5f1 Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Mon, 22 Nov 2021 20:29:27 +0200 Subject: [PATCH 3/3] refactor: code style / error handling improvements --- cli/catalogue/generate.go | 5 +++- pkg/catalogue/catalogue.go | 47 +++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/cli/catalogue/generate.go b/cli/catalogue/generate.go index 04101ac9..95233ec7 100644 --- a/cli/catalogue/generate.go +++ b/cli/catalogue/generate.go @@ -136,7 +136,7 @@ A new catalogue copy can be published to the recipes repository by passing the isClean, err := gitPkg.IsClean(rm.Name) if err != nil { - return + logrus.Fatal(err) } if !isClean { @@ -175,6 +175,9 @@ A new catalogue copy can be published to the recipes repository by passing the } features, category, err := catalogue.GetRecipeFeaturesAndCategory(recipeMeta.Name) + if err != nil { + logrus.Fatal(err) + } catl[recipeMeta.Name] = catalogue.RecipeMeta{ Name: recipeMeta.Name, diff --git a/pkg/catalogue/catalogue.go b/pkg/catalogue/catalogue.go index 22b2442d..233ec048 100644 --- a/pkg/catalogue/catalogue.go +++ b/pkg/catalogue/catalogue.go @@ -374,18 +374,18 @@ func ReadReposMetadata() (RepoCatalogue, error) { return reposMeta, nil } -func GetStringInBetween(str string, start string, end string) (result string) { +func GetStringInBetween(str, start, end string) (result string, err error) { // GetStringInBetween returns empty string if no start or end string found s := strings.Index(str, start) if s == -1 { - return + return "", fmt.Errorf("marker string '%s' not found", start) } s += len(start) e := strings.Index(str[s:], end) if e == -1 { - return + return "", fmt.Errorf("end marker '%s' not found", end) } - return str[s : s+e] + return str[s : s+e], nil } func GetImageMetadata(imageRowString string) (image, error) { @@ -394,11 +394,11 @@ func GetImageMetadata(imageRowString string) (image, error) { imgFields := strings.Split(imageRowString, ",") for i, elem := range imgFields { - imgFields[i] = strings.TrimSpace(elem) + imgFields[i] = strings.TrimSpace(elem) } if len(imgFields) < 3 { - logrus.Warnf("Image string has incorrect format: %s", imageRowString) + logrus.Warnf("image string has incorrect format: %s", imageRowString) return img, nil } @@ -407,11 +407,17 @@ func GetImageMetadata(imageRowString string) (image, error) { imgString := imgFields[0] - img.Image = GetStringInBetween(imgString, "[", "]") - img.URL = GetStringInBetween(imgString, "(", ")") + imageName, err := GetStringInBetween(imgString, "[", "]") + if err != nil { + logrus.Fatal(err) + } + img.Image = imageName - logrus.Debugf("Image: %s", img.Image) - logrus.Debugf("URL: %s", img.URL) + imageURL, err := GetStringInBetween(imgString, "(", ")") + if err != nil { + logrus.Fatal(err) + } + img.URL = imageURL return img, nil } @@ -430,14 +436,19 @@ func GetRecipeFeaturesAndCategory(recipeName string) (features, string, error) { return feat, category, err } - readmeLines := strings.Split( // Array item from lines - strings.ReplaceAll( // Remove \t tabs - GetStringInBetween( // Find text between delimiters - string(readmeFS), - "", "", - ), "\t", "", - ), - "\n") + readmeMetadata, err := GetStringInBetween( // Find text between delimiters + string(readmeFS), + "", "", + ) + if err != nil { + logrus.Fatal(err) + } + + readmeLines := strings.Split( // Array item from lines + strings.ReplaceAll( // Remove \t tabs + readmeMetadata, "\t", "", + ), + "\n") for _, val := range readmeLines { if strings.Contains(val, "**Category**") {