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] 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**") {