-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description This Pull Request is a follow up to the [Pull Request](#731) adding a Maven Access Method. This Pull Request adds a corresponding input type, a blob access and corresponding helper methods in the elements package (and therefore makes several adjustments).
- Loading branch information
1 parent
ab463b2
commit 509ba89
Showing
163 changed files
with
4,145 additions
and
1,458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
cmds/ocm/commands/ocmcmds/common/inputs/types/maven/cli.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package maven | ||
|
||
import ( | ||
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/options" | ||
"github.com/open-component-model/ocm/pkg/cobrautils/flagsets" | ||
) | ||
|
||
func ConfigHandler() flagsets.ConfigOptionTypeSetHandler { | ||
return flagsets.NewConfigOptionTypeSetHandler( | ||
TYPE, AddConfig, | ||
options.URLOption, | ||
options.PathOption, | ||
options.GroupOption, | ||
options.ArtifactOption, | ||
options.VersionOption, | ||
// optional | ||
options.ClassifierOption, | ||
options.ExtensionOption, | ||
) | ||
} | ||
|
||
func AddConfig(opts flagsets.ConfigOptions, config flagsets.Config) error { | ||
flagsets.AddFieldByOptionP(opts, options.URLOption, config, "repoUrl") | ||
flagsets.AddFieldByOptionP(opts, options.PathOption, config, "path") | ||
flagsets.AddFieldByOptionP(opts, options.GroupOption, config, "groupId") | ||
flagsets.AddFieldByOptionP(opts, options.ArtifactOption, config, "artifactId") | ||
flagsets.AddFieldByOptionP(opts, options.VersionOption, config, "version") | ||
// optional | ||
flagsets.AddFieldByOptionP(opts, options.ClassifierOption, config, "classifier") | ||
flagsets.AddFieldByOptionP(opts, options.ExtensionOption, config, "extension") | ||
return nil | ||
} |
96 changes: 96 additions & 0 deletions
96
cmds/ocm/commands/ocmcmds/common/inputs/types/maven/input_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package maven_test | ||
|
||
import ( | ||
"crypto" | ||
"github.com/open-component-model/ocm/pkg/maven/maventest" | ||
|
||
. "github.com/mandelsoft/goutils/testutils" | ||
. "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/contexts/ocm/accessmethods/localblob" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/comparch" | ||
"github.com/open-component-model/ocm/pkg/mime" | ||
"github.com/open-component-model/ocm/pkg/utils/tarutils" | ||
) | ||
|
||
const ( | ||
ARCH = "test.ca" | ||
VERSION = "v1" | ||
) | ||
|
||
var _ = Describe("Test Environment", func() { | ||
var env *TestEnv | ||
|
||
BeforeEach(func() { | ||
env = NewTestEnv(TestData(), maventest.TestData("/maven/testdata")) | ||
|
||
Expect(env.Execute("create", "ca", "-ft", "directory", "test.de/x", VERSION, "--provider", "mandelsoft", "--file", ARCH)).To(Succeed()) | ||
}) | ||
|
||
AfterEach(func() { | ||
env.Cleanup() | ||
}) | ||
|
||
It("add maven from file system described by resources.yaml", func() { | ||
Expect(env.Execute("add", "resources", "--file", ARCH, "/testdata/resources1.yaml")).To(Succeed()) | ||
data, err := env.ReadFile(env.Join(ARCH, comparch.ComponentDescriptorFileName)) | ||
Expect(err).To(Succeed()) | ||
cd, err := compdesc.Decode(data) | ||
Expect(err).To(Succeed()) | ||
Expect(len(cd.Resources)).To(Equal(1)) | ||
access := Must(env.Context.OCMContext().AccessSpecForSpec(cd.Resources[0].Access)).(*localblob.AccessSpec) | ||
Expect(access.MediaType).To(Equal(mime.MIME_TGZ)) | ||
fi := Must(env.FileSystem().Stat(env.Join(ARCH, "blobs", access.LocalReference))) | ||
Expect(fi.Size()).To(Equal(int64(1570))) | ||
li := Must(tarutils.ListArchiveContent(env.Join(ARCH, "blobs", access.LocalReference), env.FileSystem())) | ||
Expect(li).To(ConsistOf( | ||
"sdk-modules-bom-5.7.0-random-content.json", | ||
"sdk-modules-bom-5.7.0-random-content.txt", | ||
"sdk-modules-bom-5.7.0-sources.jar", | ||
"sdk-modules-bom-5.7.0.jar", | ||
"sdk-modules-bom-5.7.0.pom")) | ||
Expect(cd.Resources[0].Digest.HashAlgorithm).To(Equal(crypto.SHA256.String())) | ||
Expect(cd.Resources[0].Digest.Value).To(Equal("16cfb5ced0ea7688dba14aeb0d3aa76ad46e4661bfcc556ffd7287de3b2f7152")) | ||
}) | ||
|
||
It("add maven from file system described by cli options", func() { | ||
meta := ` | ||
name: testdata | ||
type: mavenArtifact | ||
` | ||
Expect(env.Execute("add", "resources", "--file", ARCH, "--resource", meta, "--inputType", "maven", | ||
"--inputPath", "/maven/testdata/.m2/repository", "--groupId", "com.sap.cloud.sdk", "--artifactId", "sdk-modules-bom", | ||
"--inputVersion", "5.7.0", "--classifier", "", "--extension", "pom")).To(Succeed()) | ||
data, err := env.ReadFile(env.Join(ARCH, comparch.ComponentDescriptorFileName)) | ||
Expect(err).To(Succeed()) | ||
cd, err := compdesc.Decode(data) | ||
Expect(err).To(Succeed()) | ||
Expect(len(cd.Resources)).To(Equal(1)) | ||
access := Must(env.Context.OCMContext().AccessSpecForSpec(cd.Resources[0].Access)).(*localblob.AccessSpec) | ||
Expect(access.MediaType).To(Equal(mime.MIME_XML)) | ||
fi := Must(env.FileSystem().Stat(env.Join(ARCH, "blobs", access.LocalReference))) | ||
Expect(fi.Size()).To(Equal(int64(7153))) | ||
}) | ||
|
||
It("add maven file from file system described by cli options", func() { | ||
meta := ` | ||
name: testdata | ||
type: mavenArtifact | ||
` | ||
Expect(env.Execute("add", "resources", "--file", ARCH, "--resource", meta, "--inputType", "maven", | ||
"--inputPath", "/maven/testdata/.m2/repository", "--groupId", "com.sap.cloud.sdk", "--artifactId", "sdk-modules-bom", | ||
"--inputVersion", "5.7.0", "--extension", "pom")).To(Succeed()) | ||
data, err := env.ReadFile(env.Join(ARCH, comparch.ComponentDescriptorFileName)) | ||
Expect(err).To(Succeed()) | ||
cd, err := compdesc.Decode(data) | ||
Expect(err).To(Succeed()) | ||
Expect(len(cd.Resources)).To(Equal(1)) | ||
access := Must(env.Context.OCMContext().AccessSpecForSpec(cd.Resources[0].Access)).(*localblob.AccessSpec) | ||
Expect(access.MediaType).To(Equal(mime.MIME_TGZ)) | ||
fi := Must(env.FileSystem().Stat(env.Join(ARCH, "blobs", access.LocalReference))) | ||
Expect(fi.Size()).To(Equal(int64(1109))) | ||
}) | ||
}) |
99 changes: 99 additions & 0 deletions
99
cmds/ocm/commands/ocmcmds/common/inputs/types/maven/spec.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package maven | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
|
||
"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" | ||
mavenblob "github.com/open-component-model/ocm/pkg/blobaccess/maven" | ||
"github.com/open-component-model/ocm/pkg/contexts/datacontext/attrs/vfsattr" | ||
"github.com/open-component-model/ocm/pkg/maven" | ||
) | ||
|
||
type Spec struct { | ||
cpi.PathSpec `json:",inline"` | ||
// RepoUrl defines the url from which the artifact is downloaded. | ||
RepoUrl string `json:"repoUrl,omitempty"` | ||
|
||
maven.Coordinates `json:",inline"` | ||
} | ||
|
||
var _ inputs.InputSpec = (*Spec)(nil) | ||
|
||
func New(repoUrl, groupId, artifactId, version string, classifier, extension *string) *Spec { | ||
return &Spec{ | ||
PathSpec: cpi.NewPathSpec(TYPE, ""), | ||
RepoUrl: repoUrl, | ||
Coordinates: *maven.NewCoordinates(groupId, artifactId, version, | ||
maven.WithOptionalClassifier(classifier), | ||
maven.WithOptionalExtension(extension)), | ||
} | ||
} | ||
|
||
func NewForFilePath(filePath, groupId, artifactId, version string, classifier, extension *string) *Spec { | ||
return &Spec{ | ||
PathSpec: cpi.NewPathSpec(TYPE, filePath), | ||
RepoUrl: "", | ||
Coordinates: *maven.NewCoordinates(groupId, artifactId, version, | ||
maven.WithOptionalClassifier(classifier), | ||
maven.WithOptionalExtension(extension)), | ||
} | ||
} | ||
|
||
func (s *Spec) Validate(fldPath *field.Path, ctx inputs.Context, inputFilePath string) field.ErrorList { | ||
var allErrs field.ErrorList | ||
if s.RepoUrl == "" { | ||
allErrs = s.PathSpec.Validate(fldPath, ctx, inputFilePath) | ||
} else { | ||
if s.Path != "" { | ||
pathField := fldPath.Child("path") | ||
allErrs = append(allErrs, field.Forbidden(pathField, "only path or repoUrl can be specified, not both")) | ||
} | ||
} | ||
if s.ArtifactId == "" { | ||
pathField := fldPath.Child("artifactId") | ||
allErrs = append(allErrs, field.Invalid(pathField, s.ArtifactId, "no artifact id")) | ||
} | ||
if s.GroupId == "" { | ||
pathField := fldPath.Child("groupId") | ||
allErrs = append(allErrs, field.Invalid(pathField, s.GroupId, "no group id")) | ||
} | ||
if s.Version == "" { | ||
pathField := fldPath.Child("version") | ||
allErrs = append(allErrs, field.Invalid(pathField, s.GroupId, "no group id")) | ||
} | ||
|
||
return allErrs | ||
} | ||
|
||
func (s *Spec) GetBlob(ctx inputs.Context, info inputs.InputResourceInfo) (blobaccess.BlobAccess, string, error) { | ||
var repo *maven.Repository | ||
var err error | ||
|
||
fs := ctx.FileSystem() | ||
if s.Path != "" { | ||
inputInfo, inputPath, err := inputs.FileInfo(ctx, s.Path, info.InputFilePath) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
if !inputInfo.IsDir() { | ||
return nil, "", fmt.Errorf("maven file repository must be a directory") | ||
} | ||
repo = maven.NewFileRepository(inputPath, fs) | ||
} else { | ||
repo, err = maven.NewUrlRepository(s.RepoUrl, fs) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
} | ||
access, err := mavenblob.BlobAccessForMavenCoords(repo, &s.Coordinates, | ||
mavenblob.WithCredentialContext(ctx), | ||
mavenblob.WithLoggingContext(ctx), | ||
mavenblob.WithCachingFileSystem(vfsattr.Get(ctx)), | ||
) | ||
|
||
return access, "", err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.