Skip to content

Commit

Permalink
add npm resource access by blob
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Sep 2, 2024
1 parent c2becc8 commit a8aba36
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 1 deletion.
39 changes: 39 additions & 0 deletions api/ocm/elements/artifactblob/npmblob/access_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package npmblob_test

import (
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "ocm.software/ocm/api/helper/builder"
ocmutils "ocm.software/ocm/api/ocm/ocmutils"
"ocm.software/ocm/api/tech/npm/npmtest"

"ocm.software/ocm/api/ocm/elements"
me "ocm.software/ocm/api/ocm/elements/artifactblob/npmblob"
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
"ocm.software/ocm/api/ocm/extensions/repositories/composition"
)

var _ = Describe("blobaccess for npm", func() {
Context("npm filesystem repository", func() {
var env *Builder

BeforeEach(func() {
env = NewBuilder(npmtest.TestData())
})

AfterEach(func() {
MustBeSuccessful(env.Cleanup())
})

It("blobaccess for package", func() {
cv := composition.NewComponentVersion(env.OCMContext(), "acme.org/test", "1.0.0")
defer Close(cv)

a := me.ResourceAccess(env.OCMContext(), Must(elements.ResourceMeta("blob", resourcetypes.OCM_JSON, elements.WithLocalRelation())), "file://"+npmtest.NPMPATH, npmtest.PACKAGE, npmtest.VERSION, me.WithCachingFileSystem(env.FileSystem()))
Expect(a.ReferenceHint()).To(Equal(npmtest.PACKAGE + ":" + npmtest.VERSION))

Expect(len(Must(ocmutils.GetResourceData(a)))).To(Equal(npmtest.ARTIFACT_SIZE))
})
})
})
96 changes: 96 additions & 0 deletions api/ocm/elements/artifactblob/npmblob/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package npmblob

import (
"github.com/mandelsoft/goutils/optionutils"
"github.com/mandelsoft/logging"
"github.com/mandelsoft/vfs/pkg/vfs"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/datacontext"
"ocm.software/ocm/api/ocm/cpi"
"ocm.software/ocm/api/ocm/elements/artifactblob/api"
"ocm.software/ocm/api/tech/maven"
base "ocm.software/ocm/api/utils/blobaccess/npm"
)

type Option = optionutils.Option[*Options]

type Options struct {
api.Options
Blob base.Options
}

var (
_ api.GeneralOptionsProvider = (*Options)(nil)
_ Option = (*Options)(nil)
)

func (o *Options) ApplyTo(opts *Options) {
o.Options.ApplyTo(&opts.Options)
o.Blob.ApplyTo(&opts.Blob)
}

func (o *Options) Apply(opts ...Option) {
optionutils.ApplyOptions(o, opts...)
}

////////////////////////////////////////////////////////////////////////////////
// General Options

func WithHint(h string) Option {
return api.WrapHint[Options](h)
}

func WithHintForCoords(coords *maven.Coordinates) Option {
if coords.IsPackage() {
return WithHint(coords.GAV())
}
return optionutils.NoOption[*Options]{}
}

func WithGlobalAccess(a cpi.AccessSpec) Option {
return api.WrapGlobalAccess[Options](a)
}

////////////////////////////////////////////////////////////////////////////////
// Local Options

func mapBaseOption(opts *Options) *base.Options {
return &opts.Blob
}

func wrapBase(o base.Option) Option {
return optionutils.OptionWrapperFunc[*base.Options, *Options](o, mapBaseOption)
}

func WithCredentialContext(credctx credentials.ContextProvider) Option {
return wrapBase(base.WithCredentialContext(credctx))
}

func WithLoggingContext(logctx logging.ContextProvider) Option {
return wrapBase(base.WithLoggingContext(logctx))
}

func WithCachingContext(cachectx datacontext.Context) Option {
return wrapBase(base.WithCachingContext(cachectx))
}

func WithCachingFileSystem(fs vfs.FileSystem) Option {
return wrapBase(base.WithCachingFileSystem(fs))
}

func WithCachingPath(p string) Option {
return wrapBase(base.WithCachingPath(p))
}

func WithCredentials(c credentials.Credentials) Option {
return wrapBase(base.WithCredentials(c))
}

func WithPathFileSystem(fs vfs.FileSystem) Option {
return wrapBase(base.WithPathFileSystem(fs))
}

func WithDataContext(ctx datacontext.Context) Option {
return wrapBase(base.WithDataContext(ctx))
}
36 changes: 36 additions & 0 deletions api/ocm/elements/artifactblob/npmblob/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package npmblob

import (
"github.com/mandelsoft/goutils/generics"
"github.com/mandelsoft/goutils/optionutils"

"ocm.software/ocm/api/ocm"
"ocm.software/ocm/api/ocm/compdesc"
"ocm.software/ocm/api/ocm/cpi"
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
base "ocm.software/ocm/api/utils/blobaccess/npm"
common "ocm.software/ocm/api/utils/misc"
)

const TYPE = resourcetypes.NPM_PACKAGE

func Access[M any, P compdesc.ArtifactMetaPointer[M]](ctx ocm.Context, meta P, repo, pkg, version string, opts ...Option) cpi.ArtifactAccess[M] {
eff := optionutils.EvalOptions(optionutils.WithDefaults(opts, WithHint(common.NewNameVersion(pkg, version).String()), WithCredentialContext(ctx))...)

if meta.GetType() == "" {
meta.SetType(TYPE)
}

blobprov := base.Provider(repo, pkg, version, &eff.Blob)
accprov := cpi.NewAccessProviderForBlobAccessProvider(ctx, blobprov, eff.Hint, eff.Global)
// strange type cast is required by Go compiler, meta has the correct type.
return cpi.NewArtifactAccessForProvider(generics.Cast[*M](meta), accprov)
}

func ResourceAccess(ctx ocm.Context, meta *ocm.ResourceMeta, repo, pkg, version string, opts ...Option) cpi.ResourceAccess {
return Access(ctx, meta, repo, pkg, version, opts...)
}

func SourceAccess(ctx ocm.Context, meta *ocm.SourceMeta, repo, pkg, version string, opts ...Option) cpi.SourceAccess {
return Access(ctx, meta, repo, pkg, version, opts...)
}
13 changes: 13 additions & 0 deletions api/ocm/elements/artifactblob/npmblob/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package npmblob_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "NPM Blob Access Test Suite")
}
2 changes: 1 addition & 1 deletion api/ocm/extensions/accessmethods/ocm/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var _ = Describe("OCM access CLI Test Environment", func() {

Expect("\n" + fs.FlagUsages()).To(Equal(`
--accessComponent string component for access specification
--accessRepository string repository URL
--accessRepository string repository or registry URL
--accessVersion string version for access specification
--identityPath {<name>=<value>} identity path for specification
`))
Expand Down

0 comments on commit a8aba36

Please sign in to comment.