|
| 1 | +package progressui |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/csv" |
| 5 | + "errors" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/morikuni/aec" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +var termColorMap = map[string]aec.ANSI{ |
| 14 | + "default": aec.DefaultF, |
| 15 | + |
| 16 | + "black": aec.BlackF, |
| 17 | + "blue": aec.BlueF, |
| 18 | + "cyan": aec.CyanF, |
| 19 | + "green": aec.GreenF, |
| 20 | + "magenta": aec.MagentaF, |
| 21 | + "red": aec.RedF, |
| 22 | + "white": aec.WhiteF, |
| 23 | + "yellow": aec.YellowF, |
| 24 | + |
| 25 | + "light-black": aec.LightBlackF, |
| 26 | + "light-blue": aec.LightBlueF, |
| 27 | + "light-cyan": aec.LightCyanF, |
| 28 | + "light-green": aec.LightGreenF, |
| 29 | + "light-magenta": aec.LightMagentaF, |
| 30 | + "light-red": aec.LightRedF, |
| 31 | + "light-white": aec.LightWhiteF, |
| 32 | + "light-yellow": aec.LightYellowF, |
| 33 | +} |
| 34 | + |
| 35 | +func setUserDefinedTermColors(colorsEnv string) { |
| 36 | + fields := readBuildkitColorsEnv(colorsEnv) |
| 37 | + if fields == nil { |
| 38 | + return |
| 39 | + } |
| 40 | + for _, field := range fields { |
| 41 | + parts := strings.SplitN(field, "=", 2) |
| 42 | + if len(parts) != 2 || strings.Contains(parts[1], "=") { |
| 43 | + err := errors.New("A valid entry must have exactly two fields") |
| 44 | + logrus.WithError(err).Warnf("Could not parse BUILDKIT_COLORS component: %s", field) |
| 45 | + continue |
| 46 | + } |
| 47 | + k := strings.ToLower(parts[0]) |
| 48 | + v := parts[1] |
| 49 | + if c, ok := termColorMap[strings.ToLower(v)]; ok { |
| 50 | + parseKeys(k, c) |
| 51 | + } else if strings.Contains(v, ",") { |
| 52 | + if c := readRGB(v); c != nil { |
| 53 | + parseKeys(k, c) |
| 54 | + } |
| 55 | + } else { |
| 56 | + err := errors.New("Colors must be a name from the pre-defined list or a valid 3-part RGB value") |
| 57 | + logrus.WithError(err).Warnf("Unknown color value found in BUILDKIT_COLORS: %s=%s", k, v) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func readBuildkitColorsEnv(colorsEnv string) []string { |
| 63 | + csvReader := csv.NewReader(strings.NewReader(colorsEnv)) |
| 64 | + csvReader.Comma = ':' |
| 65 | + fields, err := csvReader.Read() |
| 66 | + if err != nil { |
| 67 | + logrus.WithError(err).Warnf("Could not parse BUILDKIT_COLORS. Falling back to defaults.") |
| 68 | + return nil |
| 69 | + } |
| 70 | + return fields |
| 71 | +} |
| 72 | + |
| 73 | +func readRGB(v string) aec.ANSI { |
| 74 | + csvReader := csv.NewReader(strings.NewReader(v)) |
| 75 | + fields, err := csvReader.Read() |
| 76 | + if err != nil { |
| 77 | + logrus.WithError(err).Warnf("Could not parse value %s as valid comma-separated RGB color. Ignoring.", v) |
| 78 | + return nil |
| 79 | + } |
| 80 | + if len(fields) != 3 { |
| 81 | + err = errors.New("A valid RGB color must have three fields") |
| 82 | + logrus.WithError(err).Warnf("Could not parse value %s as valid RGB color. Ignoring.", v) |
| 83 | + return nil |
| 84 | + } |
| 85 | + ok := isValidRGB(fields) |
| 86 | + if ok { |
| 87 | + p1, _ := strconv.Atoi(fields[0]) |
| 88 | + p2, _ := strconv.Atoi(fields[1]) |
| 89 | + p3, _ := strconv.Atoi(fields[2]) |
| 90 | + c := aec.Color8BitF(aec.NewRGB8Bit(uint8(p1), uint8(p2), uint8(p3))) |
| 91 | + return c |
| 92 | + } |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func parseKeys(k string, c aec.ANSI) { |
| 97 | + key := strings.ToLower(k) |
| 98 | + switch key { |
| 99 | + case "run": |
| 100 | + colorRun = c |
| 101 | + case "cancel": |
| 102 | + colorCancel = c |
| 103 | + case "error": |
| 104 | + colorError = c |
| 105 | + case "warning": |
| 106 | + colorWarning = c |
| 107 | + default: |
| 108 | + logrus.Warnf("Unknown key found in BUILDKIT_COLORS (expected: run, cancel, error, or warning): %s", k) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func isValidRGB(s []string) bool { |
| 113 | + for _, n := range s { |
| 114 | + num, err := strconv.Atoi(n) |
| 115 | + if err != nil { |
| 116 | + logrus.Warnf("A field in BUILDKIT_COLORS appears to contain an RGB value that is not an integer: %s", strings.Join(s, ",")) |
| 117 | + return false |
| 118 | + } |
| 119 | + ok := isValidRGBValue(num) |
| 120 | + if ok { |
| 121 | + continue |
| 122 | + } else { |
| 123 | + logrus.Warnf("A field in BUILDKIT_COLORS appears to contain an RGB value that is not within the valid range of 0-255: %s", strings.Join(s, ",")) |
| 124 | + return false |
| 125 | + } |
| 126 | + } |
| 127 | + return true |
| 128 | +} |
| 129 | + |
| 130 | +func isValidRGBValue(i int) bool { |
| 131 | + if (i >= 0) && (i <= 255) { |
| 132 | + return true |
| 133 | + } |
| 134 | + return false |
| 135 | +} |
0 commit comments