feat(quality): enhance logger

Warnings are flushed after the generation to help the user to find
issues.
Colors are better defined.
This commit is contained in:
2026-03-17 10:39:48 +01:00
parent 0b1a45319f
commit b40378ec23
3 changed files with 69 additions and 16 deletions

View File

@@ -216,6 +216,10 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) error {
// call helm update if needed // call helm update if needed
callHelmUpdate(config) callHelmUpdate(config)
// flush warnings after all conversion is complete
logger.FlushWarnings()
return nil return nil
} }

View File

@@ -4,6 +4,7 @@ package logger
import ( import (
"fmt" "fmt"
"os" "os"
"sync"
) )
// Icon is a unicode icon // Icon is a unicode icon
@@ -27,6 +28,18 @@ const (
const reset = "\033[0m" const reset = "\033[0m"
const (
ColorGreen = "\033[38;5;34m"
ColorRed = "\033[38;5;196m"
ColorOrange = "\033[38;5;214m"
ColorWarning = "\033[38;5;214m"
)
var (
warnings []string
mu sync.Mutex
)
// Print prints a message without icon. // Print prints a message without icon.
func Print(msg ...any) { func Print(msg ...any) {
fmt.Print(msg...) fmt.Print(msg...)
@@ -49,38 +62,38 @@ func Infof(format string, msg ...any) {
// Warn prints a warning message. // Warn prints a warning message.
func Warn(msg ...any) { func Warn(msg ...any) {
orange := "\033[38;5;214m" mu.Lock()
message(orange, IconWarning, msg...) defer mu.Unlock()
warning := fmt.Sprint(msg...)
warnings = append(warnings, warning)
} }
// Warnf prints a formatted warning message. // Warnf prints a formatted warning message.
func Warnf(format string, msg ...any) { func Warnf(format string, msg ...any) {
orange := "\033[38;5;214m" mu.Lock()
message(orange, IconWarning, fmt.Sprintf(format, msg...)) defer mu.Unlock()
warning := fmt.Sprintf(format, msg...)
warnings = append(warnings, warning)
} }
// Success prints a success message. // Success prints a success message.
func Success(msg ...any) { func Success(msg ...any) {
green := "\033[38;5;34m" message(ColorGreen, IconSuccess, msg...)
message(green, IconSuccess, msg...)
} }
// Successf prints a formatted success message. // Successf prints a formatted success message.
func Successf(format string, msg ...any) { func Successf(format string, msg ...any) {
green := "\033[38;5;34m" message(ColorGreen, IconSuccess, fmt.Sprintf(format, msg...))
message(green, IconSuccess, fmt.Sprintf(format, msg...))
} }
// Failure prints a failure message. // Failure prints a failure message.
func Failure(msg ...any) { func Failure(msg ...any) {
red := "\033[38;5;196m" message(ColorRed, IconFailure, msg...)
message(red, IconFailure, msg...)
} }
// Failuref prints a formatted failure message. // Failuref prints a formatted failure message.
func Failuref(format string, msg ...any) { func Failuref(format string, msg ...any) {
red := "\033[38;5;196m" message(ColorRed, IconFailure, fmt.Sprintf(format, msg...))
message(red, IconFailure, fmt.Sprintf(format, msg...))
} }
// Log prints a message with a custom icon. // Log prints a message with a custom icon.
@@ -106,12 +119,26 @@ func fatalf(red string, icon Icon, format string, msg ...any) {
// Fatal prints a fatal error message and exits with code 1. // Fatal prints a fatal error message and exits with code 1.
func Fatal(msg ...any) { func Fatal(msg ...any) {
red := "\033[38;5;196m" fatal(ColorRed, IconFailure, msg...)
fatal(red, IconFailure, msg...)
} }
// Fatalf prints a fatal error message with formatting and exits with code 1. // Fatalf prints a fatal error message with formatting and exits with code 1.
func Fatalf(format string, msg ...any) { func Fatalf(format string, msg ...any) {
red := "\033[38;5;196m" fatalf(ColorRed, IconFailure, format, msg...)
fatalf(red, IconFailure, format, msg...) }
// FlushWarnings prints all collected warnings at the end of the conversion.
func FlushWarnings() {
mu.Lock()
defer mu.Unlock()
if len(warnings) > 0 {
fmt.Println()
fmt.Print(ColorWarning, IconWarning, " The following issues may need attention:", reset)
fmt.Println()
for _, warning := range warnings {
fmt.Println(" •", warning)
}
fmt.Println()
warnings = nil
}
} }

View File

@@ -77,3 +77,25 @@ func TestLog(t *testing.T) {
}() }()
Log(IconInfo, "test log") Log(IconInfo, "test log")
} }
func TestWarningsCollection(t *testing.T) {
// Clear any existing warnings
warnings = nil
// Add some warnings
Warn("test warning 1")
Warnf("test warning 2: %s", "value")
// Check that warnings were collected
if len(warnings) != 2 {
t.Errorf("expected 2 warnings, got %d", len(warnings))
}
// Check the content of warnings
if warnings[0] != "test warning 1" {
t.Errorf("expected 'test warning 1', got '%s'", warnings[0])
}
if warnings[1] != "test warning 2: value" {
t.Errorf("expected 'test warning 2: value', got '%s'", warnings[1])
}
}