Skip to content

Commit

Permalink
refactored packer
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 19, 2024
1 parent 636547b commit a1fbe47
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 65 deletions.
32 changes: 5 additions & 27 deletions cmd/oras/root/manifest/index/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ type createOptions struct {
option.Common
option.Target
option.Pretty
option.Annotation

sources []string
extraRefs []string
rawAnnotations []string
indexAnnotations map[string]string
outputPath string
sources []string
extraRefs []string
outputPath string
}

func createCmd() *cobra.Command {
Expand Down Expand Up @@ -92,11 +91,6 @@ Example - Create an index and output the index to stdout, auto push will be disa
opts.RawReference = refs[0]
opts.extraRefs = refs[1:]
opts.sources = args[1:]
var err error
opts.indexAnnotations, err = parseAnnotations(opts.rawAnnotations)
if err != nil {
return err
}
return option.Parse(cmd, &opts)
},
Aliases: []string{"pack"},
Expand All @@ -105,7 +99,6 @@ Example - Create an index and output the index to stdout, auto push will be disa
},
}
cmd.Flags().StringVarP(&opts.outputPath, "output", "o", "", "file `path` to write the created index to, use - for stdout")
cmd.Flags().StringArrayVarP(&opts.rawAnnotations, "annotation", "a", nil, "index annotations")
option.ApplyFlags(&opts, cmd.Flags())
return oerrors.Command(cmd, &opts.Target)
}
Expand All @@ -126,7 +119,7 @@ func createIndex(cmd *cobra.Command, opts createOptions) error {
},
MediaType: ocispec.MediaTypeImageIndex,
Manifests: manifests,
Annotations: opts.indexAnnotations,
Annotations: opts.Annotations[option.AnnotationManifest],
}
indexBytes, err := json.Marshal(index)
if err != nil {
Expand Down Expand Up @@ -216,18 +209,3 @@ func pushIndex(ctx context.Context, target oras.Target, desc ocispec.Descriptor,
}
return printer.Println("Digest:", desc.Digest)
}

func parseAnnotations(input []string) (map[string]string, error) {
annotations := make(map[string]string)
for _, anno := range input {
key, val, success := strings.Cut(anno, "=")
if !success {
return nil, fmt.Errorf("annotation value doesn't match the required format of \"key=value\"")
}
if _, ok := annotations[key]; ok {
return nil, fmt.Errorf("duplicate annotation key: %v", key)
}
annotations[key] = val
}
return annotations, nil
}
75 changes: 37 additions & 38 deletions cmd/oras/root/manifest/index/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,45 @@ limitations under the License.
package index

import (
"reflect"
"testing"
)

func Test_parseAnnotations(t *testing.T) {
tests := []struct {
name string
input []string
annotations map[string]string
wantErr bool
wantAnnotations map[string]string
}{
{
name: "valid input",
input: []string{"a=b", "c=d", "e=f"},
wantErr: false,
wantAnnotations: map[string]string{"a": "b", "c": "d", "e": "f"},
},
{
name: "invalid input",
input: []string{"a=b", "c:d", "e=f"},
wantErr: true,
wantAnnotations: nil,
},
{
name: "duplicate key",
input: []string{"a=b", "c=d", "a=e"},
wantErr: true,
wantAnnotations: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
annotations, err := parseAnnotations(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("parseAnnotations() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(annotations, tt.wantAnnotations) {
t.Errorf("parseAnnotations() annotations = %v, want %v", tt.annotations, tt.wantAnnotations)
}
})
}
// tests := []struct {
// name string
// input []string
// annotations map[string]string
// wantErr bool
// wantAnnotations map[string]string
// }{
// {
// name: "valid input",
// input: []string{"a=b", "c=d", "e=f"},
// wantErr: false,
// wantAnnotations: map[string]string{"a": "b", "c": "d", "e": "f"},
// },
// {
// name: "invalid input",
// input: []string{"a=b", "c:d", "e=f"},
// wantErr: true,
// wantAnnotations: nil,
// },
// {
// name: "duplicate key",
// input: []string{"a=b", "c=d", "a=e"},
// wantErr: true,
// wantAnnotations: nil,
// },
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// annotations, err := parseAnnotations(tt.input)
// if (err != nil) != tt.wantErr {
// t.Errorf("parseAnnotations() error = %v, wantErr %v", err, tt.wantErr)
// }
// if !reflect.DeepEqual(annotations, tt.wantAnnotations) {
// t.Errorf("parseAnnotations() annotations = %v, want %v", tt.annotations, tt.wantAnnotations)
// }
// })
// }
}

0 comments on commit a1fbe47

Please sign in to comment.