refactor: code style / error handling improvements
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
3wc 2021-11-22 20:29:27 +02:00
parent 8635922b9f
commit d0a30f6b7b
2 changed files with 33 additions and 19 deletions

View File

@ -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,

View File

@ -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),
"<!-- metadata -->", "<!-- endmetadata -->",
), "\t", "",
),
"\n")
readmeMetadata, err := GetStringInBetween( // Find text between delimiters
string(readmeFS),
"<!-- metadata -->", "<!-- endmetadata -->",
)
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**") {