Skip to content

Commit

Permalink
refined the code
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
  • Loading branch information
Xiaoxuan Wang committed Sep 20, 2024
1 parent 119acee commit 6b0349b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
16 changes: 10 additions & 6 deletions cmd/oras/internal/option/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ var (

// Packer option struct.
type Annotation struct {
ManifestAnnotationFlags []string // raw input of manifest annotation flags
Annotations map[string]map[string]string // parsed manifest and config annotations
// ManifestAnnotations contains raw input of manifest annotation "key=value" pairs
ManifestAnnotations []string

// Annotations contains parsed manifest and config annotations
Annotations map[string]map[string]string
}

// ApplyFlags applies flags to a command flag set.
func (opts *Annotation) ApplyFlags(fs *pflag.FlagSet) {
fs.StringArrayVarP(&opts.ManifestAnnotationFlags, "annotation", "a", nil, "manifest annotations")
fs.StringArrayVarP(&opts.ManifestAnnotations, "annotation", "a", nil, "manifest annotations")
}

func (opts *Annotation) Parse(*cobra.Command) error {
opts.Annotations = make(map[string]map[string]string)
manifestAnnotations := make(map[string]string)
for _, anno := range opts.ManifestAnnotationFlags {
for _, anno := range opts.ManifestAnnotations {
key, val, success := strings.Cut(anno, "=")
if !success {
return &oerrors.Error{
Expand All @@ -57,6 +59,8 @@ func (opts *Annotation) Parse(*cobra.Command) error {
}
manifestAnnotations[key] = val
}
opts.Annotations[AnnotationManifest] = manifestAnnotations
opts.Annotations = map[string]map[string]string{
AnnotationManifest: manifestAnnotations,
}
return nil
}
11 changes: 6 additions & 5 deletions cmd/oras/internal/option/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (opts *Packer) ExportManifest(ctx context.Context, fetcher content.Fetcher,
}
return os.WriteFile(opts.ManifestExportPath, manifestBytes, 0666)
}

func (opts *Packer) Parse(cmd *cobra.Command) error {
if !opts.PathValidationDisabled {
var failedPaths []string
Expand All @@ -95,22 +96,22 @@ func (opts *Packer) Parse(cmd *cobra.Command) error {
}

// parseAnnotations loads the manifest annotation map.
func (opts *Packer) parseAnnotations(cmd *cobra.Command) (err error) {
if opts.AnnotationFilePath != "" && len(opts.ManifestAnnotationFlags) != 0 {
func (opts *Packer) parseAnnotations(cmd *cobra.Command) error {
if opts.AnnotationFilePath != "" && len(opts.ManifestAnnotations) != 0 {
return errAnnotationConflict
}
if opts.AnnotationFilePath != "" {
if err = decodeJSON(opts.AnnotationFilePath, &opts.Annotations); err != nil {
if err := decodeJSON(opts.AnnotationFilePath, &opts.Annotations); err != nil {
return &oerrors.Error{
Err: fmt.Errorf(`invalid annotation json file: failed to load annotations from %s`, opts.AnnotationFilePath),
Recommendation: `Annotation file doesn't match the required format. Please refer to the document at https://oras.land/docs/how_to_guides/manifest_annotations`,
}
}
}
if len(opts.ManifestAnnotationFlags) != 0 {
if len(opts.ManifestAnnotations) != 0 {
return opts.Annotation.Parse(cmd)
}
return
return nil
}

// decodeJSON decodes a json file v to filename.
Expand Down
12 changes: 6 additions & 6 deletions cmd/oras/internal/option/packer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestPacker_FlagInit(t *testing.T) {
func TestPacker_parseAnnotations_err(t *testing.T) {
opts := Packer{
Annotation: Annotation{
ManifestAnnotationFlags: []string{"Key=Val"},
ManifestAnnotations: []string{"Key=Val"},
},
AnnotationFilePath: "this is not a file", // testFile,
}
Expand All @@ -57,7 +57,7 @@ func TestPacker_parseAnnotations_err(t *testing.T) {

opts = Packer{
Annotation: Annotation{
ManifestAnnotationFlags: []string{"KeyVal"},
ManifestAnnotations: []string{"KeyVal"},
},
}
if err := opts.parseAnnotations(nil); !errors.Is(err, errAnnotationFormat) {
Expand All @@ -66,7 +66,7 @@ func TestPacker_parseAnnotations_err(t *testing.T) {

opts = Packer{
Annotation: Annotation{
ManifestAnnotationFlags: []string{"Key=Val1", "Key=Val2"},
ManifestAnnotations: []string{"Key=Val1", "Key=Val2"},
},
}
if err := opts.parseAnnotations(nil); !errors.Is(err, errAnnotationDuplication) {
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestPacker_parseAnnotations_annotationFlag(t *testing.T) {
}
opts := Packer{
Annotation: Annotation{
ManifestAnnotationFlags: invalidFlag0,
ManifestAnnotations: invalidFlag0,
},
}
err := opts.parseAnnotations(nil)
Expand All @@ -115,7 +115,7 @@ func TestPacker_parseAnnotations_annotationFlag(t *testing.T) {
}
opts = Packer{
Annotation: Annotation{
ManifestAnnotationFlags: invalidFlag1,
ManifestAnnotations: invalidFlag1,
},
}
err = opts.parseAnnotations(nil)
Expand All @@ -131,7 +131,7 @@ func TestPacker_parseAnnotations_annotationFlag(t *testing.T) {
}
opts = Packer{
Annotation: Annotation{
ManifestAnnotationFlags: validFlag,
ManifestAnnotations: validFlag,
},
}
err = opts.parseAnnotations(nil)
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_runAttach_errType(t *testing.T) {
opts := &attachOptions{
Packer: option.Packer{
Annotation: option.Annotation{
ManifestAnnotationFlags: []string{"one", "two"},
ManifestAnnotations: []string{"one", "two"},
},
AnnotationFilePath: "/tmp/whatever",
},
Expand Down

0 comments on commit 6b0349b

Please sign in to comment.