diff --git a/internal/command/release_create.go b/internal/command/release_create.go index be27666c..70de728d 100644 --- a/internal/command/release_create.go +++ b/internal/command/release_create.go @@ -7,10 +7,10 @@ import ( "io" "os" "strings" - "unicode" "github.com/simplesurance/baur/v3/internal/command/term" "github.com/simplesurance/baur/v3/internal/log" + "github.com/simplesurance/baur/v3/internal/validation" "github.com/simplesurance/baur/v3/pkg/baur" "github.com/simplesurance/baur/v3/pkg/storage" @@ -98,24 +98,12 @@ func newReleaseCreateCmd() *releaseCreateCmd { return &cmd } -func mustValidateReleaseName(releaseName string) { - for pos, r := range releaseName { - if (pos == 0 || pos == len(releaseName)-1) && unicode.IsSpace(r) { - fatalf("release name must not contain leading or trailing whitespaces, got: %q\n", - releaseName) - } - - if !unicode.IsPrint(r) { - fatalf("release name contains non-printable character: %q\n", r) - } - } -} - func (c *releaseCreateCmd) run(cmd *cobra.Command, args []string) { ctx := cmd.Context() releaseName := args[0] - mustValidateReleaseName(releaseName) + err := validation.StrID(releaseName) + exitOnErrf(err, "invalid release name: %q", releaseName) stdout.Printf("creating release: %s\n", term.Highlight(releaseName)) diff --git a/internal/validation/validation.go b/internal/validation/validation.go new file mode 100644 index 00000000..29668a2e --- /dev/null +++ b/internal/validation/validation.go @@ -0,0 +1,23 @@ +package validation + +import ( + "errors" + "fmt" + "unicode" +) + +// StrID ensures that id does not contain leading or trailing white spaces +// ([unicode.IsSpace] and only printable characters ([unicode.IsPrint]. +func StrID(id string) error { + for pos, r := range id { + if (pos == 0 || pos == len(id)-1) && unicode.IsSpace(r) { + return errors.New("contains leading or trailing white spaces") + } + + if !unicode.IsPrint(r) { + return fmt.Errorf("contains non-printable character: %x", r) + } + } + + return nil +} diff --git a/pkg/cfg/validation.go b/pkg/cfg/validation.go index 436e4d9e..0b430c29 100644 --- a/pkg/cfg/validation.go +++ b/pkg/cfg/validation.go @@ -4,6 +4,8 @@ import ( "errors" "fmt" "strings" + + "github.com/simplesurance/baur/v3/internal/validation" ) var forbiddenNameRunes = [...]rune{ @@ -23,5 +25,5 @@ func validateTaskOrAppName(name string) error { } } - return nil + return validation.StrID(name) }