Skip to content

Commit

Permalink
restruct blobaccess (#804)
Browse files Browse the repository at this point in the history
* auto update vendor hash

* separate internal and external usage

* fix blobaccess doc.go

* add Validate description

---------

Co-authored-by: ocmbot[bot] <125909804+ocmbot[bot]@users.noreply.github.com>
  • Loading branch information
mandelsoft and ocmbot[bot] authored Jun 12, 2024
1 parent bbddd51 commit a8664a4
Show file tree
Hide file tree
Showing 196 changed files with 2,101 additions and 1,406 deletions.
8 changes: 4 additions & 4 deletions cmds/ocm/commands/common/options/formatoption/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package formatoption

import (
"fmt"
"sort"
"strings"

"github.com/mandelsoft/vfs/pkg/vfs"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/common/accessobj"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/utils"
)

func From(o options.OptionSetProvider) *Option {
Expand Down Expand Up @@ -67,11 +67,11 @@ func (o *Option) Usage() string {
The <code>--type</code> option accepts a file format for the
target archive to use. The following formats are supported:
`
list := utils.StringSlice{}
list := []string{}
for k := range accessobj.GetFormats() {
list.Add(k.String())
list = append(list, k.String())
}
list.Sort()
sort.Strings(list)
for _, k := range list {
s = s + "- " + k + "\n"
}
Expand Down
8 changes: 4 additions & 4 deletions cmds/ocm/commands/ocicmds/tags/show/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/sliceutils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand All @@ -16,7 +17,6 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/oci"
"github.com/open-component-model/ocm/pkg/out"
utils2 "github.com/open-component-model/ocm/pkg/utils"
)

var (
Expand Down Expand Up @@ -89,7 +89,7 @@ func (o *Command) Run() error {
}

versions := Versions{}
tags := utils2.StringSlice{}
tags := sliceutils.OrderedSlice[string]{}
repo := repooption.From(o)

var art oci.ArtifactAccess
Expand Down Expand Up @@ -127,7 +127,7 @@ func (o *Command) Run() error {
if err != nil {
return err
}
tags = utils2.StringSlice(list)
tags = sliceutils.OrderedSlice[string](list)
// determine version base set
if art != nil {
dig := art.Digest()
Expand All @@ -137,7 +137,7 @@ func (o *Command) Run() error {
return err
}
if a.Digest() != dig {
tags.Delete(i)
tags.DeleteIndex(i)
i--
} else {
v, err := semver.NewVersion(tags[i])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func (s *Spec) Validate(fldPath *field.Path, ctx inputs.Context, inputFilePath s
}

func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (blobaccess.BlobAccess, string, error) {
return s.ProcessBlob(ctx, blobaccess.DataAccessForBytes([]byte(s.Data)), ctx.FileSystem())
return s.ProcessBlob(ctx, blobaccess.DataAccessForData([]byte(s.Data)), ctx.FileSystem())
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (bloba
return nil, "", fmt.Errorf("resource type is dir but a file was provided")
}

access, err := dirtree.BlobAccessForDirTree(inputPath,
access, err := dirtree.BlobAccess(inputPath,
dirtree.WithMimeType(s.MediaType),
dirtree.WithFileSystem(fs),
dirtree.WithCompressWithGzip(s.Compress()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (bloba
if err != nil {
return nil, "", err
}
blob, version, err := dockerdaemon.BlobAccessForImageFromDockerDaemon(s.Path, dockerdaemon.WithVersion(info.ComponentVersion.GetVersion()), dockerdaemon.WithOrigin(info.ComponentVersion))
blob, version, err := dockerdaemon.BlobAccess(s.Path, dockerdaemon.WithVersion(info.ComponentVersion.GetVersion()), dockerdaemon.WithOrigin(info.ComponentVersion))
if err != nil {
return nil, "", err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

. "github.com/mandelsoft/goutils/finalizer"

"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs"
Expand Down Expand Up @@ -92,7 +91,7 @@ func (s *Spec) getVariant(ctx clictx.Context, finalize *Finalizer, variant strin
}

func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (blobaccess.BlobAccess, string, error) {
blob, err := dockermulti.BlobAccessForMultiImageFromDockerDaemon(
blob, err := dockermulti.BlobAccess(
dockermulti.WithContext(ctx),
dockermulti.WithPrinter(ctx.Printer()),
dockermulti.WithVariants(s.Variants...),
Expand Down
2 changes: 1 addition & 1 deletion cmds/ocm/commands/ocmcmds/common/inputs/types/helm/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (blob
override = false
}

blob, name, vers, err := helm.BlobAccessForHelmChart(path,
blob, name, vers, err := helm.BlobAccess(path,
helm.WithContext(ctx),
helm.WithFileSystem(ctx.FileSystem()),
helm.WithPrinter(ctx.Printer()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (bloba
return nil, "", err
}
}
access, err := mavenblob.BlobAccessForMavenCoords(repo, &s.Coordinates,
access, err := mavenblob.BlobAccessForCoords(repo, &s.Coordinates,
mavenblob.WithCredentialContext(ctx),
mavenblob.WithLoggingContext(ctx),
mavenblob.WithCachingFileSystem(vfsattr.Get(ctx)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/cpi"
"github.com/open-component-model/ocm/pkg/blobaccess"
ociartifact2 "github.com/open-component-model/ocm/pkg/blobaccess/ociartifact"
ociartifactblob "github.com/open-component-model/ocm/pkg/blobaccess/ociartifact"
"github.com/open-component-model/ocm/pkg/contexts/oci/grammar"
"github.com/open-component-model/ocm/pkg/contexts/oci/repositories/docker"
"github.com/open-component-model/ocm/pkg/contexts/oci/transfer/filters"
Expand Down Expand Up @@ -47,7 +47,7 @@ func (s *Spec) Validate(fldPath *field.Path, ctx inputs.Context, inputFilePath s
return allErrs
}

func (s *Spec) CreateFilter() ociartifact2.Option {
func (s *Spec) CreateFilter() ociartifactblob.Option {
var filter []filters.Filter

for _, v := range s.Platforms {
Expand All @@ -57,18 +57,18 @@ func (s *Spec) CreateFilter() ociartifact2.Option {
}
}
if len(filter) > 0 {
return ociartifact2.WithFilter(filters.Or(filter...))
return ociartifactblob.WithFilter(filters.Or(filter...))
}
return nil
}

func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (blobaccess.BlobAccess, string, error) {
filter := s.CreateFilter()
blob, version, err := ociartifact2.BlobAccessForOCIArtifact(s.Path,
blob, version, err := ociartifactblob.BlobAccess(s.Path,
filter,
ociartifact2.WithContext(ctx),
ociartifact2.WithPrinter(ctx.Printer()),
ociartifact2.WithVersion(info.ComponentVersion.GetVersion()),
ociartifactblob.WithContext(ctx),
ociartifactblob.WithPrinter(ctx.Printer()),
ociartifactblob.WithVersion(info.ComponentVersion.GetVersion()),
)
if err != nil {
return nil, "", err
Expand Down
2 changes: 1 addition & 1 deletion cmds/ocm/commands/ocmcmds/common/inputs/types/utf8/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (bloba
if err != nil {
return nil, "", err
}
return s.ProcessBlob(ctx, blobaccess.DataAccessForBytes(data), ctx.FileSystem())
return s.ProcessBlob(ctx, blobaccess.DataAccessForData(data), ctx.FileSystem())
}

func Prepare(raw []byte) (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmds/ocm/commands/ocmcmds/common/inputs/types/wget/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *Spec) Validate(fldPath *field.Path, ctx inputs.Context, inputFilePath s
}

func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (blobaccess.BlobAccess, string, error) {
access, err := wget.BlobAccessForWget(s.URL,
access, err := wget.BlobAccess(s.URL,
wget.WithCredentialContext(ctx),
wget.WithLoggingContext(ctx),
wget.WithMimeType(s.MimeType),
Expand Down
2 changes: 1 addition & 1 deletion cmds/ocm/commands/ocmcmds/sources/add/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/open-component-model/ocm/cmds/ocm/testhelper"

"github.com/open-component-model/ocm/pkg/blobaccess"

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/localblob"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
Expand Down
4 changes: 2 additions & 2 deletions cmds/ocm/commands/toicmds/package/bootstrap/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (o *Command) Complete(args []string) error {
if err != nil {
return errors.Wrapf(err, "failed reading credentials file %q", o.CredentialsFile)
}
o.Credentials = blobaccess.DataAccessForBytes(data, o.CredentialsFile)
o.Credentials = blobaccess.DataAccessForData(data, o.CredentialsFile)
}
if len(o.ParameterFile) == 0 {
if ok, _ := vfs.FileExists(o.FileSystem(), DEFAULT_PARAMETER_FILE); ok {
Expand All @@ -212,7 +212,7 @@ func (o *Command) Complete(args []string) error {
if err != nil {
return errors.Wrapf(err, "failed reading parameter file %q", o.ParameterFile)
}
o.Parameters = blobaccess.DataAccessForBytes(data, o.ParameterFile)
o.Parameters = blobaccess.DataAccessForData(data, o.ParameterFile)
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion cmds/ocm/pkg/output/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/sliceutils"
"github.com/mandelsoft/logging"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -143,7 +144,7 @@ func (o *Options) Configure(ctx clictx.Context) error {
o.Output = f(o)
}

var avail utils.StringSlice
var avail sliceutils.OrderedSlice[string]

var fields []string

Expand Down
2 changes: 1 addition & 1 deletion examples/lib/creds.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ consumer ids always feature the attribute `type`, describing the kind of
context.

For example, to describe the request for credentials for
an [OCI registry](../../pkg/contexts/oci/identity/identity.go) and repository,
an [OCI registry](../../pkg/contexts/credentials/builtin/oci/identity/identity.go) and repository,
the type value is `oci.CONSUMER_TYPE`. Additionally, the following
attributes are used to fully describe the usage context.

Expand Down
2 changes: 1 addition & 1 deletion examples/lib/ctf/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/finalizer"
"github.com/mandelsoft/vfs/pkg/memoryfs"

"github.com/open-component-model/ocm/pkg/blobaccess"

"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/common/accessobj"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/finalizer"

"github.com/open-component-model/ocm/pkg/blobaccess"

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/ociartifact"
Expand Down
2 changes: 1 addition & 1 deletion examples/lib/tour/03-working-with-credentials/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/finalizer"

"github.com/open-component-model/ocm/pkg/blobaccess"

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/finalizer"

"github.com/open-component-model/ocm/pkg/blobaccess"

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/ociartifact"
Expand Down
2 changes: 1 addition & 1 deletion examples/lib/tour/06-signing-component-versions/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/finalizer"
"github.com/open-component-model/ocm/pkg/blobaccess"

"github.com/open-component-model/ocm/examples/lib/helper"
"github.com/open-component-model/ocm/pkg/blobaccess"
"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/ociartifact"
Expand Down
2 changes: 1 addition & 1 deletion examples/lib/transfer1/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/mandelsoft/goutils/finalizer"
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/pkg/blobaccess"

"github.com/mandelsoft/goutils/errors"
"github.com/open-component-model/ocm/examples/lib/helper"
"github.com/open-component-model/ocm/pkg/blobaccess"
"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/common/accessobj"
Expand Down
73 changes: 73 additions & 0 deletions pkg/blobaccess/blobaccess/access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package blobaccess

import (
"bytes"
"io"

"github.com/mandelsoft/filepath/pkg/filepath"
"github.com/opencontainers/go-digest"

"github.com/open-component-model/ocm/pkg/blobaccess/bpi"
"github.com/open-component-model/ocm/pkg/iotools"
mimetypes "github.com/open-component-model/ocm/pkg/mime"
)

type bytesAccess struct {
_nopCloser
data []byte
origin string
}

func DataAccessForData(data []byte, origin ...string) bpi.DataSource {
path := ""
if len(origin) > 0 {
path = filepath.Join(origin...)
}
return &bytesAccess{data: data, origin: path}
}

func DataAccessForString(data string, origin ...string) bpi.DataSource {
return DataAccessForData([]byte(data), origin...)
}

func (a *bytesAccess) Get() ([]byte, error) {
return a.data, nil
}

func (a *bytesAccess) Reader() (io.ReadCloser, error) {
return iotools.ReadCloser(bytes.NewReader(a.data)), nil
}

func (a *bytesAccess) Origin() string {
return a.origin
}

////////////////////////////////////////////////////////////////////////////////

// ForString wraps a string into a BlobAccess, which does not need a close.
func ForString(mime string, data string) bpi.BlobAccess {
if mime == "" {
mime = mimetypes.MIME_TEXT
}
return ForData(mime, []byte(data))
}

func ProviderForString(mime, data string) bpi.BlobAccessProvider {
return bpi.BlobAccessProviderFunction(func() (bpi.BlobAccess, error) {
return ForString(mime, data), nil
})
}

// ForData wraps data into a BlobAccess, which does not need a close.
func ForData(mime string, data []byte) bpi.BlobAccess {
if mime == "" {
mime = mimetypes.MIME_OCTET
}
return bpi.ForStaticDataAccessAndMeta(mime, DataAccessForData(data), digest.FromBytes(data), int64(len(data)))
}

func ProviderForData(mime string, data []byte) bpi.BlobAccessProvider {
return bpi.BlobAccessProviderFunction(func() (bpi.BlobAccess, error) {
return ForData(mime, data), nil
})
}
Loading

0 comments on commit a8664a4

Please sign in to comment.