mirror of
https://github.com/hauler-dev/hauler.git
synced 2026-02-14 18:09:51 +00:00
move types to constants
This commit is contained in:
@@ -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,
|
||||
@@ -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
|
||||
}
|
||||
79
internal/mapper/mappers.go
Normal file
79
internal/mapper/mappers.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package k3s
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user