Files
vpp-maglev/cmd/maglevc/color.go
Pim van Pelt 74448cf6d0 Guard pause/resume against disabled backends; clean up CLI errors
- PauseBackend and ResumeBackend return an error (not bool) when the
  backend is disabled, preventing an inconsistent state where the
  health state says "paused" but enabled=false.
- DisableBackend and EnableBackend now log uniform backend-transition
  lines with from/to instead of separate backend-disable/backend-enable
  messages.
- CLI errors strip gRPC boilerplate ("rpc error: code = ... desc = ")
  and display the server message in red (when color is enabled). Both
  the interactive shell and one-shot mode use the same formatError path.
2026-04-11 21:19:10 +02:00

42 lines
1.1 KiB
Go

// Copyright (c) 2026, Pim van Pelt <pim@ipng.ch>
package main
import "strings"
const (
ansiBlue = "\x1b[34m"
ansiRed = "\x1b[31m"
ansiReset = "\x1b[0m"
)
// colorEnabled is set by the -color flag in main.
var colorEnabled bool
// label wraps s in dark-blue ANSI when color output is enabled.
// Because every label receives the same fixed-length prefix/suffix, tabwriter
// alignment is preserved: the extra bytes are equal for all rows so relative
// widths remain correct.
func label(s string) string {
if !colorEnabled {
return s
}
return ansiBlue + s + ansiReset
}
// formatError returns a user-friendly error string. gRPC status errors are
// unwrapped to show only the server's message (no "rpc error: code = ..."
// boilerplate). The result is wrapped in red ANSI when color is enabled.
func formatError(err error) string {
msg := err.Error()
// google.golang.org/grpc/status errors format as:
// rpc error: code = <Code> desc = <message>
if i := strings.Index(msg, " desc = "); i >= 0 {
msg = msg[i+len(" desc = "):]
}
if colorEnabled {
return ansiRed + msg + ansiReset
}
return msg
}