Skip to content

Commit

Permalink
fix: prompt for user action to set an instance ID on upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed May 19, 2024
1 parent c83d080 commit 294864f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 6 additions & 1 deletion internal/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ 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"
)

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)
Expand Down
12 changes: 9 additions & 3 deletions internal/config/validationutil/validationutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "_")
}
Expand All @@ -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
}

0 comments on commit 294864f

Please sign in to comment.