Skip to content

Commit

Permalink
check fileRefs when manifest annotation is empty
Browse files Browse the repository at this point in the history
Signed-off-by: JasmineTang <jasminetang@microsoft.com>
  • Loading branch information
jasminetMSFT committed Aug 15, 2022
1 parent 6200ad9 commit 24987b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/oras/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Example - Attach file 'hi.txt' with type 'doc/example' to manifest 'hello:test'
`,
Args: cobra.MinimumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := opts.Pusher.ValidateEmpty(); err != nil {
if err := opts.Pusher.ValidateEmpty(args); err != nil {
return err
}
return opts.ReadPassword()
Expand Down
26 changes: 8 additions & 18 deletions cmd/oras/internal/option/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"os"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -27,9 +28,8 @@ import (
)

const (
annotationConfig = "$config"
annotationManifest = "$manifest"
validateEmptyErrorStr = "no blob and manifest annotation are provided"
annotationConfig = "$config"
annotationManifest = "$manifest"
)

// Pusher option struct.
Expand Down Expand Up @@ -67,19 +67,17 @@ func (opts *Pusher) LoadManifestAnnotations() (map[string]map[string]string, err
if err := decodeJSON(opts.ManifestAnnotations, &annotations); err != nil {
return nil, err
}
if err := validateEmptyManifestAnnotations(annotations); err != nil {
return nil, err
if len(annotations[annotationManifest]) == 0 && len(opts.FileRefs) == 0 {
return nil, fmt.Errorf("no blob and manifest annotation file %s is empty", opts.ManifestAnnotations)
}
}
return annotations, nil
}

// ValidateEmpty checks whether blobs or manifest annotation are empty.
func (opts *Pusher) ValidateEmpty() error {
if len(opts.FileRefs) == 0 {
if opts.ManifestAnnotations == "" {
return errors.New(validateEmptyErrorStr)
}
func (opts *Pusher) ValidateEmpty(args []string) error {
if len(args) == 1 && opts.ManifestAnnotations == "" {
return errors.New("no blob and manifest annotation are provided")
}
return nil
}
Expand All @@ -93,11 +91,3 @@ func decodeJSON(filename string, v interface{}) error {
defer file.Close()
return json.NewDecoder(file).Decode(v)
}

// validateEmptyManifestAnnotations check whether existing ManifestAnnotations contain no value.
func validateEmptyManifestAnnotations(annotations map[string]map[string]string) error {
if len(annotations[annotationConfig]) == 0 && len(annotations[annotationManifest]) == 0 {
return errors.New(validateEmptyErrorStr)
}
return nil
}

0 comments on commit 24987b6

Please sign in to comment.