Skip to content

Commit

Permalink
Merge pull request #96 from nikkelma/public-content-types
Browse files Browse the repository at this point in the history
Make content types pubic, expose configuration fields
  • Loading branch information
joshrwolf authored Dec 8, 2021
2 parents 61cbc6f + 40fb078 commit 352c014
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
9 changes: 8 additions & 1 deletion pkg/content/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
var _ artifact.OCI = (*Chart)(nil)

type Chart struct {
Repo string
Name string
Version string

path string

annotations map[string]string
Expand All @@ -41,7 +45,10 @@ func NewChart(name, repo, version string) (*Chart, error) {
}

return &Chart{
path: cp,
Repo: repo,
Name: name,
Version: version,
path: cp,
}, nil
}

Expand Down
31 changes: 16 additions & 15 deletions pkg/content/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
)

// interface guard
var _ artifact.OCI = (*file)(nil)
var _ artifact.OCI = (*File)(nil)

type File struct {
Ref string

type file struct {
ref string
client *getter.Client

computed bool
Expand All @@ -26,12 +27,12 @@ type file struct {
annotations map[string]string
}

func NewFile(ref string, opts ...Option) *file {
func NewFile(ref string, opts ...Option) *File {
client := getter.NewClient(getter.ClientOptions{})

f := &file{
f := &File{
client: client,
ref: ref,
Ref: ref,
}

for _, opt := range opts {
Expand All @@ -40,22 +41,22 @@ func NewFile(ref string, opts ...Option) *file {
return f
}

func (f *file) Name(ref string) string {
func (f *File) Name(ref string) string {
return f.client.Name(ref)
}

func (f *file) MediaType() string {
func (f *File) MediaType() string {
return consts.OCIManifestSchema1
}

func (f *file) RawConfig() ([]byte, error) {
func (f *File) RawConfig() ([]byte, error) {
if err := f.compute(); err != nil {
return nil, err
}
return f.config.Raw()
}

func (f *file) Layers() ([]gv1.Layer, error) {
func (f *File) Layers() ([]gv1.Layer, error) {
if err := f.compute(); err != nil {
return nil, err
}
Expand All @@ -64,20 +65,20 @@ func (f *file) Layers() ([]gv1.Layer, error) {
return layers, nil
}

func (f *file) Manifest() (*gv1.Manifest, error) {
func (f *File) Manifest() (*gv1.Manifest, error) {
if err := f.compute(); err != nil {
return nil, err
}
return f.manifest, nil
}

func (f *file) compute() error {
func (f *File) compute() error {
if f.computed {
return nil
}

ctx := context.Background()
blob, err := f.client.LayerFrom(ctx, f.ref)
blob, err := f.client.LayerFrom(ctx, f.Ref)
if err != nil {
return err
}
Expand All @@ -87,9 +88,9 @@ func (f *file) compute() error {
return err
}

cfg := f.client.Config(f.ref)
cfg := f.client.Config(f.Ref)
if cfg == nil {
cfg = f.client.Config(f.ref)
cfg = f.client.Config(f.Ref)
}

cfgDesc, err := partial.Descriptor(cfg)
Expand Down
13 changes: 6 additions & 7 deletions pkg/content/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Test_file_Config(t *testing.T) {

got := string(m.Config.MediaType)
if got != tt.want {
t.Errorf("Expected mediatype %s | got %s", got, tt.want)
t.Errorf("unxpected mediatype; got %s, want %s", got, tt.want)
}
})
}
Expand Down Expand Up @@ -96,27 +96,26 @@ func Test_file_Layers(t *testing.T) {
// TODO: Add directory test
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(tt.name, func(it *testing.T) {
f := file.NewFile(tt.ref, file.WithClient(mc))

layers, err := f.Layers()
if (err != nil) != tt.wantErr {
t.Errorf("Layers() error = %v, wantErr %v", err, tt.wantErr)
return
it.Fatalf("unexpected Layers() error: got %v, want %v", err, tt.wantErr)
}

rc, err := layers[0].Compressed()
if err != nil {
t.Fatal(err)
it.Fatal(err)
}

got, err := io.ReadAll(rc)
if err != nil {
t.Fatal(err)
it.Fatal(err)
}

if !bytes.Equal(got, tt.want) {
t.Errorf("Layers() got = %v, want %v", layers, tt.want)
it.Fatalf("unexpected Layers(): got %v, want %v", layers, tt.want)
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/content/file/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import (
"github.com/rancherfederal/hauler/pkg/artifact"
)

type Option func(*file)
type Option func(*File)

func WithClient(c *getter.Client) Option {
return func(f *file) {
return func(f *File) {
f.client = c
}
}

func WithConfig(obj interface{}, mediaType string) Option {
return func(f *file) {
return func(f *File) {
f.config = artifact.ToConfig(obj, artifact.WithConfigMediaType(mediaType))
}
}

func WithAnnotations(m map[string]string) Option {
return func(f *file) {
return func(f *File) {
f.annotations = m
}
}
14 changes: 8 additions & 6 deletions pkg/content/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ import (
"github.com/rancherfederal/hauler/pkg/artifact"
)

var _ artifact.OCI = (*image)(nil)
var _ artifact.OCI = (*Image)(nil)

func (i *image) MediaType() string {
func (i *Image) MediaType() string {
mt, err := i.Image.MediaType()
if err != nil {
return ""
}
return string(mt)
}

func (i *image) RawConfig() ([]byte, error) {
func (i *Image) RawConfig() ([]byte, error) {
return i.RawConfigFile()
}

type image struct {
type Image struct {
Ref string
gv1.Image
}

func NewImage(ref string, opts ...remote.Option) (*image, error) {
func NewImage(ref string, opts ...remote.Option) (*Image, error) {
r, err := name.ParseReference(ref)
if err != nil {
return nil, err
Expand All @@ -43,7 +44,8 @@ func NewImage(ref string, opts ...remote.Option) (*image, error) {
return nil, err
}

return &image{
return &Image{
Ref: ref,
Image: img,
}, nil
}

0 comments on commit 352c014

Please sign in to comment.