From 294864fe433302571ba9ff9eb7c2dd475fa1c560 Mon Sep 17 00:00:00 2001 From: garethgeorge Date: Sat, 18 May 2024 19:09:58 -0700 Subject: [PATCH] fix: prompt for user action to set an instance ID on upgrade --- internal/config/validate.go | 7 ++++++- internal/config/validationutil/validationutil.go | 12 +++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/internal/config/validate.go b/internal/config/validate.go index 02f309ae..342c68dc 100644 --- a/internal/config/validate.go +++ b/internal/config/validate.go @@ -10,6 +10,7 @@ import ( "github.com/garethgeorge/backrest/internal/config/validationutil" "github.com/gitploy-io/cronexpr" "github.com/hashicorp/go-multierror" + "go.uber.org/zap" "google.golang.org/protobuf/proto" ) @@ -17,7 +18,11 @@ func ValidateConfig(c *v1.Config) error { var err error if e := validationutil.ValidateID(c.Instance, validationutil.IDMaxLen); e != nil { - err = multierror.Append(err, fmt.Errorf("instance ID %q invalid: %w", c.Instance, e)) + if errors.Is(e, validationutil.ErrEmpty) { + zap.L().Warn("ACTION REQUIRED: instance ID is empty, will be required in a future update. Please open the backrest UI to set a unique instance ID. Until fixed this warning (and related errors) will print periodically.") + } else { + err = multierror.Append(err, fmt.Errorf("instance ID %q invalid: %w", c.Instance, e)) + } } repos := make(map[string]*v1.Repo) diff --git a/internal/config/validationutil/validationutil.go b/internal/config/validationutil/validationutil.go index 3b712293..a8920ad6 100644 --- a/internal/config/validationutil/validationutil.go +++ b/internal/config/validationutil/validationutil.go @@ -12,6 +12,12 @@ var ( idRegex = regexp.MustCompile(`[a-zA-Z0-9_\-\.]*`) // matches a valid ID (including empty string) ) +var ( + ErrEmpty = errors.New("empty") + ErrTooLong = errors.New("too long") + ErrInvalidChars = errors.New("contains invalid characters") +) + func SanitizeID(id string) string { return sanitizeIDRegex.ReplaceAllString(id, "_") } @@ -21,13 +27,13 @@ func SanitizeID(id string) string { // The maxLen parameter is the maximum length of the ID. If maxLen is 0, the ID length is not checked. func ValidateID(id string, maxLen int) error { if !idRegex.MatchString(id) { - return errors.New("contains invalid characters") + return ErrInvalidChars } if len(id) == 0 { - return errors.New("empty") + return ErrEmpty } if maxLen > 0 && len(id) > maxLen { - return fmt.Errorf("too long (> %d chars)", maxLen) + return fmt.Errorf("(> %d chars): %w", maxLen, ErrTooLong) } return nil }