Skip to content

Commit

Permalink
chore(ux): improve error message for bad annotation format (#1243)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 authored Jan 19, 2024
1 parent d21c31f commit 0514a57
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions cmd/oras/internal/option/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/pflag"
"oras.land/oras-go/v2/content"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/fileref"
)

Expand All @@ -38,7 +39,7 @@ const (

var (
errAnnotationConflict = errors.New("`--annotation` and `--annotation-file` cannot be both specified")
errAnnotationFormat = errors.New("missing key in `--annotation` flag")
errAnnotationFormat = errors.New("annotation value doesn't match the required format")
errAnnotationDuplication = errors.New("duplicate annotation key")
errPathValidation = errors.New("absolute file path detected. If it's intentional, use --disable-path-validation flag to skip this check")
)
Expand Down Expand Up @@ -99,12 +100,12 @@ func (opts *Packer) LoadManifestAnnotations() (annotations map[string]map[string
}
if opts.AnnotationFilePath != "" {
if err = decodeJSON(opts.AnnotationFilePath, &annotations); err != nil {
errStr := err.Error()
errStr := err.Error()
docLink := " Please refer to the document at https://oras.land/docs/how_to_guides/manifest_annotations."
if !strings.HasSuffix(errStr, ".") {
docLink = "."+docLink
docLink = "." + docLink
}
return nil, fmt.Errorf("failed to load annotations from %s: %w" +
return nil, fmt.Errorf("failed to load annotations from %s: %w"+
docLink, opts.AnnotationFilePath, err)
}
}
Expand Down Expand Up @@ -133,7 +134,10 @@ func parseAnnotationFlags(flags []string, annotations map[string]map[string]stri
for _, anno := range flags {
key, val, success := strings.Cut(anno, "=")
if !success {
return fmt.Errorf("%w: %s", errAnnotationFormat, anno)
return &oerrors.Error{
Err: errAnnotationFormat,
Recommendation: `Please use the correct format in the flag: --annotation "key=value"`,
}
}
if _, ok := manifestAnnotations[key]; ok {
return fmt.Errorf("%w: %v, ", errAnnotationDuplication, key)
Expand Down

0 comments on commit 0514a57

Please sign in to comment.