From 49f7b5ea0ee11de089e89a8e44f3283bd95d698e Mon Sep 17 00:00:00 2001 From: Josh Wolf Date: Wed, 8 Dec 2021 09:25:27 -0700 Subject: [PATCH] add more public methods for building config files from any marshallable source --- pkg/artifact/config.go | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/pkg/artifact/config.go b/pkg/artifact/config.go index 87ed1c5..94eb69e 100644 --- a/pkg/artifact/config.go +++ b/pkg/artifact/config.go @@ -7,6 +7,8 @@ import ( "github.com/google/go-containerregistry/pkg/v1/partial" "github.com/google/go-containerregistry/pkg/v1/types" "k8s.io/apimachinery/pkg/util/json" + + "github.com/rancherfederal/hauler/pkg/consts" ) var _ partial.Describable = (*marshallableConfig)(nil) @@ -22,14 +24,22 @@ type Config interface { Size() (int64, error) } -type Marshallable interface { - MediaType() (types.MediaType, error) -} +type Marshallable interface{} + +type ConfigOption func(*marshallableConfig) // ToConfig takes anything that is marshallabe and converts it into a Config -func ToConfig(i Marshallable) Config { - return &marshallableConfig{ - Marshallable: i, +func ToConfig(i Marshallable, opts ...ConfigOption) Config { + mc := &marshallableConfig{Marshallable: i} + for _, o := range opts { + o(mc) + } + return mc +} + +func WithConfigMediaType(mediaType string) ConfigOption { + return func(config *marshallableConfig) { + config.mediaType = mediaType } } @@ -37,8 +47,15 @@ func ToConfig(i Marshallable) Config { type marshallableConfig struct { Marshallable - hash v1.Hash - size int64 + mediaType string +} + +func (c *marshallableConfig) MediaType() (types.MediaType, error) { + mt := c.mediaType + if mt == "" { + mt = consts.UnknownManifest + } + return types.MediaType(mt), nil } func (c *marshallableConfig) Raw() ([]byte, error) {