fix: prompt for user action to set an instance ID on upgrade

This commit is contained in:
garethgeorge
2024-05-18 19:09:58 -07:00
parent c83d080391
commit 294864fe43
2 changed files with 15 additions and 4 deletions
+6 -1
View File
@@ -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)
@@ -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
}