From c7ae551e6fb51616004c6ae2e893687a0a4d895c Mon Sep 17 00:00:00 2001 From: Josh Wolf Date: Fri, 3 Dec 2021 14:00:20 -0700 Subject: [PATCH] move types to constants --- internal/mapper/{store.go => filestore.go} | 5 +- internal/mapper/mapper.go | 59 ----------- internal/mapper/mappers.go | 79 ++++++++++++++ pkg/collection/k3s/k3s_test.go | 1 - pkg/consts/{types.go => consts.go} | 0 pkg/content/file/reference.go | 114 --------------------- 6 files changed, 83 insertions(+), 175 deletions(-) rename internal/mapper/{store.go => filestore.go} (92%) delete mode 100644 internal/mapper/mapper.go create mode 100644 internal/mapper/mappers.go delete mode 100644 pkg/collection/k3s/k3s_test.go rename pkg/consts/{types.go => consts.go} (100%) delete mode 100644 pkg/content/file/reference.go diff --git a/internal/mapper/store.go b/internal/mapper/filestore.go similarity index 92% rename from internal/mapper/store.go rename to internal/mapper/filestore.go index cf7c517..5a8fab0 100644 --- a/internal/mapper/store.go +++ b/internal/mapper/filestore.go @@ -2,6 +2,7 @@ package mapper import ( "context" + "fmt" "io" "io/ioutil" "os" @@ -17,7 +18,9 @@ import ( "oras.land/oras-go/pkg/content" ) -func NewStore(root string, mapper map[string]Fn) *store { +// NewMapperFileStore creates a new file store that uses mapper functions for each detected descriptor. +// This extends content.File, and differs in that it allows much more functionality into how each descriptor is written. +func NewMapperFileStore(root string, mapper map[string]Fn) *store { fs := content.NewFile(root) return &store{ File: fs, diff --git a/internal/mapper/mapper.go b/internal/mapper/mapper.go deleted file mode 100644 index bfeff2e..0000000 --- a/internal/mapper/mapper.go +++ /dev/null @@ -1,59 +0,0 @@ -package mapper - -import ( - "fmt" - "os" - - "github.com/opencontainers/image-spec/specs-go/v1" - - "github.com/rancherfederal/hauler/pkg/consts" -) - -type Fn func(desc v1.Descriptor) (*os.File, error) - -type maps struct { -} - -func NewMapper() *maps { - return &maps{} -} - -type Mapper interface{} - -type image struct{} - -func (i *image) mapper() map[string]Fn { - m := make(map[string]Fn) - - manifestMapperFn := Fn(func(desc v1.Descriptor) (*os.File, error) { - return os.Create("manifest.json") - }) - - for _, l := range []string{consts.DockerManifestSchema2, consts.OCIManifestSchema1} { - m[l] = manifestMapperFn - } - - layerMapperFn := Fn(func(desc v1.Descriptor) (*os.File, error) { - n := fmt.Sprintf("%s.tar.gz", desc.Digest.String()) - return os.Create(n) - }) - - for _, l := range []string{consts.OCILayer, consts.DockerLayer} { - m[l] = layerMapperFn - } - - configMapperFn := Fn(func(desc v1.Descriptor) (*os.File, error) { - return os.Create("config.json") - }) - - for _, l := range []string{consts.DockerConfigJSON} { - m[l] = configMapperFn - } - - return m - -} - -func (i *image) identify() bool { - return false -} diff --git a/internal/mapper/mappers.go b/internal/mapper/mappers.go new file mode 100644 index 0000000..1f28cbc --- /dev/null +++ b/internal/mapper/mappers.go @@ -0,0 +1,79 @@ +package mapper + +import ( + "fmt" + "os" + + ocispec "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/pkg/errors" + + "github.com/rancherfederal/hauler/pkg/consts" +) + +type Fn func(desc ocispec.Descriptor) (*os.File, error) + +func Images() map[string]Fn { + m := make(map[string]Fn) + + manifestMapperFn := Fn(func(desc ocispec.Descriptor) (*os.File, error) { + return os.Create("manifest.json") + }) + + for _, l := range []string{consts.DockerManifestSchema2, consts.OCIManifestSchema1} { + m[l] = manifestMapperFn + } + + layerMapperFn := Fn(func(desc ocispec.Descriptor) (*os.File, error) { + n := fmt.Sprintf("%s.tar.gz", desc.Digest.String()) + return os.Create(n) + }) + + for _, l := range []string{consts.OCILayer, consts.DockerLayer} { + m[l] = layerMapperFn + } + + configMapperFn := Fn(func(desc ocispec.Descriptor) (*os.File, error) { + return os.Create("config.json") + }) + + for _, l := range []string{consts.DockerConfigJSON} { + m[l] = configMapperFn + } + + return m +} + +func Files() map[string]Fn { + m := make(map[string]Fn) + + blobMapperFn := Fn(func(desc ocispec.Descriptor) (*os.File, error) { + fmt.Println(desc.Annotations) + if _, ok := desc.Annotations[ocispec.AnnotationTitle]; !ok { + return nil, errors.Errorf("unkown file name") + } + return os.Create(desc.Annotations[ocispec.AnnotationTitle]) + }) + + m[consts.FileLayerMediaType] = blobMapperFn + return m +} + +func Chart() map[string]Fn { + m := make(map[string]Fn) + + chartMapperFn := Fn(func(desc ocispec.Descriptor) (*os.File, error) { + f := "chart.tar.gz" + if _, ok := desc.Annotations[ocispec.AnnotationTitle]; ok { + f = desc.Annotations[ocispec.AnnotationTitle] + } + return os.Create(f) + }) + + provMapperFn := Fn(func(desc ocispec.Descriptor) (*os.File, error) { + return os.Create("prov.json") + }) + + m[consts.ChartLayerMediaType] = chartMapperFn + m[consts.ProvLayerMediaType] = provMapperFn + return m +} diff --git a/pkg/collection/k3s/k3s_test.go b/pkg/collection/k3s/k3s_test.go deleted file mode 100644 index 678052d..0000000 --- a/pkg/collection/k3s/k3s_test.go +++ /dev/null @@ -1 +0,0 @@ -package k3s diff --git a/pkg/consts/types.go b/pkg/consts/consts.go similarity index 100% rename from pkg/consts/types.go rename to pkg/consts/consts.go diff --git a/pkg/content/file/reference.go b/pkg/content/file/reference.go deleted file mode 100644 index 9d0a68a..0000000 --- a/pkg/content/file/reference.go +++ /dev/null @@ -1,114 +0,0 @@ -package file - -import ( - "errors" - "io" - "net/http" - "net/url" - "os" - "path/filepath" - - ocispec "github.com/opencontainers/image-spec/specs-go/v1" - - "github.com/rancherfederal/hauler/internal/layer" -) - -// Locatable represents content that can be referenced by a -type Locatable interface { - Locate() string - - Name() string - - Annotate() map[string]string - - Open() layer.Opener -} - -var ( - _ Locatable = (*localFile)(nil) - _ Locatable = (*remoteFile)(nil) -) - -func NewLocatableLocalFile(path string) (*localFile, error) { - if _, err := os.Stat(path); err != nil { - return nil, err - } - abs, err := filepath.Abs(path) - if err != nil { - return nil, err - } - return &localFile{ - path: abs, - }, nil -} - -type localFile struct { - path string -} - -func (l localFile) Locate() string { - return l.path -} - -func (l localFile) Name() string { - return filepath.Base(l.path) -} - -func (l localFile) Annotate() map[string]string { - annotations := make(map[string]string) - annotations[ocispec.AnnotationTitle] = l.Name() // For oras FileStore to recognize - annotations[ocispec.AnnotationSource] = l.Locate() - return annotations -} - -func (l localFile) Open() layer.Opener { - return func() (io.ReadCloser, error) { - return os.Open(l.Locate()) - } -} - -func NewLocatableRemoteFile(ref string) (*remoteFile, error) { - u, err := url.Parse(ref) - if err != nil { - return nil, err - } - if u.Scheme != "https" && u.Scheme != "http" { - return nil, errors.New("not a valid remote file protocol (http or https)") - } - return &remoteFile{ - ref: ref, - }, err -} - -type remoteFile struct { - ref string - name string -} - -func (r remoteFile) Locate() string { - return r.ref -} - -func (r remoteFile) Name() string { - if r.name != "" { - return r.name - } - return filepath.Base(r.ref) -} - -func (r remoteFile) Annotate() map[string]string { - annotations := make(map[string]string) - annotations[ocispec.AnnotationTitle] = r.Name() - annotations[ocispec.AnnotationSource] = r.Locate() - return annotations -} - -func (r remoteFile) Open() layer.Opener { - return func() (io.ReadCloser, error) { - resp, err := http.Get(r.ref) - if err != nil { - return nil, err - } - return resp.Body, nil - } -}