Skip to content

Commit

Permalink
Add better parsing and error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
spkane committed Jul 11, 2022
1 parent bd0f39e commit 6fc28da
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions util/progress/progressui/colors.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package progressui

import (
"fmt"
"encoding/csv"
"regexp"
"strings"

"github.com/morikuni/aec"
"github.com/sirupsen/logrus"
)

var rex = regexp.MustCompile(`(\w+)=(\w+)`)
Expand Down Expand Up @@ -35,12 +36,21 @@ var termColorMap = map[string]aec.ANSI{
}

func setUserDefinedTermColors(colorsEnv string) {
data := rex.FindAllStringSubmatch(colorsEnv, -1)
for _, kv := range data {
k := kv[1]
v := kv[2]
// debug
fmt.Print(k, "=", v)
csvReader := csv.NewReader(strings.NewReader(colorsEnv))
csvReader.Comma = ':' // Use colon instead of comma
fields, err := csvReader.Read()
if err != nil {
logrus.WithError(err).Warnf("Could not parse BUILDKIT_COLORS. Falling back to defaults.")
return
}
for _, field := range fields {
parts := strings.SplitN(field, "=", 2)
if len(parts) != 2 {
logrus.Warnf("Could not parse BUILDKIT_COLORS component: %v", parts)
continue
}
k := strings.ToLower(parts[0])
v := parts[1]
c, ok := termColorMap[strings.ToLower(v)]
if ok {
key := strings.ToLower(k)
Expand All @@ -53,7 +63,11 @@ func setUserDefinedTermColors(colorsEnv string) {
colorError = c
case "warning":
colorWarning = c
default:
logrus.Warnf("Unknown key found in BUILDKIT_COLORS (expected: run, cancel, error, or warning): %s", k)
}
} else {
logrus.Warnf("Unknown color value found in BUILDKIT_COLORS: %s=%s", k, v)
}
}
}

0 comments on commit 6fc28da

Please sign in to comment.