You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2018/09/19 13:54:30 UTC
[camel-k] 05/09: fix golint findings for pkg/util/maven
This is an automated email from the ASF dual-hosted git repository.
nferraro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 5fa09384f69718825cc5acbbbdc7b2b53ca6d38a
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Wed Sep 19 12:07:52 2018 +0200
fix golint findings for pkg/util/maven
---
pkg/build/local/local_builder.go | 26 +++++++++++++-------------
pkg/build/local/local_builder_test.go | 14 +++++++-------
pkg/util/maven/maven.go | 31 +++++++++++++++++++------------
pkg/util/maven/maven_test.go | 28 ++++++++++++++--------------
pkg/util/maven/types.go | 33 +++++++++++++++++++++------------
5 files changed, 74 insertions(+), 58 deletions(-)
diff --git a/pkg/build/local/local_builder.go b/pkg/build/local/local_builder.go
index 0274c12..c341274 100644
--- a/pkg/build/local/local_builder.go
+++ b/pkg/build/local/local_builder.go
@@ -111,11 +111,11 @@ func (b *localBuilder) buildCycle(ctx context.Context) {
}
func (b *localBuilder) execute(source build.Request) (string, error) {
- project, err := generateProjectDefinition(source)
+ integration, err := generateIntegration(source)
if err != nil {
return "", err
}
- tarFileName, err := maven.Build(project)
+ tarFileName, err := maven.Build(integration)
if err != nil {
return "", err
}
@@ -254,24 +254,24 @@ func (b *localBuilder) publish(tarFile string, source build.Request) (string, er
return is.Status.DockerImageRepository + ":" + source.Identifier.Qualifier, nil
}
-func generateProjectDefinition(source build.Request) (maven.ProjectDefinition, error) {
- project := maven.ProjectDefinition{
+func generateIntegration(source build.Request) (maven.Integration, error) {
+ integration := maven.Integration{
Project: maven.Project{
XMLName: xml.Name{Local: "project"},
- XmlNs: "http://maven.apache.org/POM/4.0.0",
- XmlNsXsi: "http://www.w3.org/2001/XMLSchema-instance",
+ XMLNs: "http://maven.apache.org/POM/4.0.0",
+ XMLNsXsi: "http://www.w3.org/2001/XMLSchema-instance",
XsiSchemaLocation: "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd",
ModelVersion: "4.0.0",
- GroupId: "org.apache.camel.k.integration",
- ArtifactId: "camel-k-integration",
+ GroupID: "org.apache.camel.k.integration",
+ ArtifactID: "camel-k-integration",
Version: version.Version,
DependencyManagement: maven.DependencyManagement{
Dependencies: maven.Dependencies{
Dependencies: []maven.Dependency{
{
//TODO: camel version should be retrieved from an external source or provided as static version
- GroupId: "org.apache.camel",
- ArtifactId: "camel-bom",
+ GroupID: "org.apache.camel",
+ ArtifactID: "camel-bom",
Version: "2.22.1",
Type: "pom",
Scope: "import",
@@ -291,7 +291,7 @@ func generateProjectDefinition(source build.Request) (maven.ProjectDefinition, e
// set-up dependencies
//
- deps := &project.Project.Dependencies
+ deps := &integration.Project.Dependencies
deps.AddGAV("org.apache.camel.k", "camel-k-runtime-jvm", version.Version)
for _, d := range source.Dependencies {
@@ -309,9 +309,9 @@ func generateProjectDefinition(source build.Request) (maven.ProjectDefinition, e
deps.AddEncodedGAV(gav)
} else {
- return maven.ProjectDefinition{}, fmt.Errorf("unknown dependency type: %s", d)
+ return maven.Integration{}, fmt.Errorf("unknown dependency type: %s", d)
}
}
- return project, nil
+ return integration, nil
}
diff --git a/pkg/build/local/local_builder_test.go b/pkg/build/local/local_builder_test.go
index 790c124..c5fd33d 100644
--- a/pkg/build/local/local_builder_test.go
+++ b/pkg/build/local/local_builder_test.go
@@ -43,16 +43,16 @@ func TestProjectGeneration(t *testing.T) {
},
}
- prj, err := generateProjectDefinition(source)
+ prj, err := generateIntegration(source)
assert.Nil(t, err)
assert.NotNil(t, prj)
assert.Equal(t, len(prj.Project.Dependencies.Dependencies), 5)
- assert.Equal(t, prj.Project.Dependencies.Dependencies[0].ArtifactId, "camel-k-runtime-jvm")
- assert.Equal(t, prj.Project.Dependencies.Dependencies[1].ArtifactId, "camel-mail")
- assert.Equal(t, prj.Project.Dependencies.Dependencies[2].ArtifactId, "camel-netty4")
- assert.Equal(t, prj.Project.Dependencies.Dependencies[3].ArtifactId, "camel-servicenow")
+ assert.Equal(t, prj.Project.Dependencies.Dependencies[0].ArtifactID, "camel-k-runtime-jvm")
+ assert.Equal(t, prj.Project.Dependencies.Dependencies[1].ArtifactID, "camel-mail")
+ assert.Equal(t, prj.Project.Dependencies.Dependencies[2].ArtifactID, "camel-netty4")
+ assert.Equal(t, prj.Project.Dependencies.Dependencies[3].ArtifactID, "camel-servicenow")
assert.Equal(t, prj.Project.Dependencies.Dependencies[3].Version, "2.21.1")
- assert.Equal(t, prj.Project.Dependencies.Dependencies[4].ArtifactId, "camel-salesforce")
+ assert.Equal(t, prj.Project.Dependencies.Dependencies[4].ArtifactID, "camel-salesforce")
assert.Equal(t, prj.Project.Dependencies.Dependencies[4].Version, "")
}
@@ -75,6 +75,6 @@ func TestProjectGenerationWithFailure(t *testing.T) {
},
}
- _, err := generateProjectDefinition(source)
+ _, err := generateIntegration(source)
assert.NotNil(t, err)
}
diff --git a/pkg/util/maven/maven.go b/pkg/util/maven/maven.go
index 32e1317..c54ebd2 100644
--- a/pkg/util/maven/maven.go
+++ b/pkg/util/maven/maven.go
@@ -38,15 +38,15 @@ const (
artifactDirPrefix = "maven-bin-"
)
-// Takes a project description and returns a binary tar with the built artifacts
-func Build(project ProjectDefinition) (string, error) {
+// Build takes a project description and returns a binary tar with the built artifacts
+func Build(integration Integration) (string, error) {
buildDir, err := ioutil.TempDir("", buildDirPrefix)
if err != nil {
return "", errors.Wrap(err, "could not create temporary dir for maven source files")
}
defer os.RemoveAll(buildDir)
- err = createMavenStructure(buildDir, project)
+ err = createMavenStructure(buildDir, integration)
if err != nil {
return "", errors.Wrap(err, "could not write maven source files")
}
@@ -54,7 +54,7 @@ func Build(project ProjectDefinition) (string, error) {
if err != nil {
return "", err
}
- tarfile, err := createTar(buildDir, project)
+ tarfile, err := createTar(buildDir, integration)
if err != nil {
return "", err
}
@@ -90,13 +90,13 @@ func mavenExtraOptions() string {
return "-Dcamel.noop=true"
}
-func createTar(buildDir string, project ProjectDefinition) (string, error) {
+func createTar(buildDir string, integration Integration) (string, error) {
artifactDir, err := ioutil.TempDir("", artifactDirPrefix)
if err != nil {
return "", errors.Wrap(err, "could not create temporary dir for maven artifacts")
}
- tarFileName := path.Join(artifactDir, project.Project.ArtifactId+".tar")
+ tarFileName := path.Join(artifactDir, integration.Project.ArtifactID+".tar")
tarFile, err := os.Create(tarFileName)
if err != nil {
return "", errors.Wrap(err, "cannot create tar file "+tarFileName)
@@ -104,14 +104,14 @@ func createTar(buildDir string, project ProjectDefinition) (string, error) {
defer tarFile.Close()
writer := tar.NewWriter(tarFile)
- err = appendToTar(path.Join(buildDir, "target", project.Project.ArtifactId+"-"+project.Project.Version+".jar"), "", writer)
+ err = appendToTar(path.Join(buildDir, "target", integration.Project.ArtifactID+"-"+integration.Project.Version+".jar"), "", writer)
if err != nil {
return "", err
}
// Environment variables
- if project.Env != nil {
- err = writeFile(buildDir, "run-env.sh", envFileContent(project.Env))
+ if integration.Env != nil {
+ err = writeFile(buildDir, "run-env.sh", envFileContent(integration.Env))
if err != nil {
return "", err
}
@@ -169,7 +169,7 @@ func appendToTar(filePath string, tarPath string, writer *tar.Writer) error {
return nil
}
-func createMavenStructure(buildDir string, project ProjectDefinition) error {
+func createMavenStructure(buildDir string, project Integration) error {
pom, err := GeneratePomFileContent(project.Project)
if err != nil {
return err
@@ -236,6 +236,7 @@ func envFileContent(env map[string]string) string {
return content
}
+// GeneratePomFileContent generate a pom.xml file from the given project definition
func GeneratePomFileContent(project Project) (string, error) {
w := &bytes.Buffer{}
w.WriteString(xml.Header)
@@ -251,14 +252,20 @@ func GeneratePomFileContent(project Project) (string, error) {
return w.String(), nil
}
+// ParseGAV decode a maven artifact id to a dependency definition.
+//
+// The artifact id is in the form of:
+//
+// <groupId>:<artifactId>[:<packagingType>[:<classifier>]]:(<version>|'?')
+//
func ParseGAV(gav string) (Dependency, error) {
// <groupId>:<artifactId>[:<packagingType>[:<classifier>]]:(<version>|'?')
dep := Dependency{}
rex := regexp.MustCompile("([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?(:([^: ]+))?")
res := rex.FindStringSubmatch(gav)
- dep.GroupId = res[1]
- dep.ArtifactId = res[2]
+ dep.GroupID = res[1]
+ dep.ArtifactID = res[2]
dep.Type = "jar"
cnt := strings.Count(gav, ":")
diff --git a/pkg/util/maven/maven_test.go b/pkg/util/maven/maven_test.go
index 7e59e54..05cff20 100644
--- a/pkg/util/maven/maven_test.go
+++ b/pkg/util/maven/maven_test.go
@@ -53,19 +53,19 @@ const expectedPom = `<?xml version="1.0" encoding="UTF-8"?>
func TestPomGeneration(t *testing.T) {
project := Project{
XMLName: xml.Name{Local: "project"},
- XmlNs: "http://maven.apache.org/POM/4.0.0",
- XmlNsXsi: "http://www.w3.org/2001/XMLSchema-instance",
+ XMLNs: "http://maven.apache.org/POM/4.0.0",
+ XMLNsXsi: "http://www.w3.org/2001/XMLSchema-instance",
XsiSchemaLocation: "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd",
ModelVersion: "4.0.0",
- GroupId: "org.apache.camel.k.integration",
- ArtifactId: "camel-k-integration",
+ GroupID: "org.apache.camel.k.integration",
+ ArtifactID: "camel-k-integration",
Version: "1.0.0",
DependencyManagement: DependencyManagement{
Dependencies: Dependencies{
Dependencies: []Dependency{
{
- GroupId: "org.apache.camel",
- ArtifactId: "camel-bom",
+ GroupID: "org.apache.camel",
+ ArtifactID: "camel-bom",
Version: "2.22.1",
Type: "pom",
Scope: "import",
@@ -76,8 +76,8 @@ func TestPomGeneration(t *testing.T) {
Dependencies: Dependencies{
Dependencies: []Dependency{
{
- GroupId: "org.apache.camel.k",
- ArtifactId: "camel-k-runtime-jvm",
+ GroupID: "org.apache.camel.k",
+ ArtifactID: "camel-k-runtime-jvm",
Version: "1.0.0",
},
},
@@ -96,8 +96,8 @@ func TestParseSimpleGAV(t *testing.T) {
dep, err := ParseGAV("org.apache.camel:camel-core:2.21.1")
assert.Nil(t, err)
- assert.Equal(t, dep.GroupId, "org.apache.camel")
- assert.Equal(t, dep.ArtifactId, "camel-core")
+ assert.Equal(t, dep.GroupID, "org.apache.camel")
+ assert.Equal(t, dep.ArtifactID, "camel-core")
assert.Equal(t, dep.Version, "2.21.1")
assert.Equal(t, dep.Type, "jar")
assert.Equal(t, dep.Classifier, "")
@@ -107,8 +107,8 @@ func TestParseGAVWithType(t *testing.T) {
dep, err := ParseGAV("org.apache.camel:camel-core:war:2.21.1")
assert.Nil(t, err)
- assert.Equal(t, dep.GroupId, "org.apache.camel")
- assert.Equal(t, dep.ArtifactId, "camel-core")
+ assert.Equal(t, dep.GroupID, "org.apache.camel")
+ assert.Equal(t, dep.ArtifactID, "camel-core")
assert.Equal(t, dep.Version, "2.21.1")
assert.Equal(t, dep.Type, "war")
assert.Equal(t, dep.Classifier, "")
@@ -118,8 +118,8 @@ func TestParseGAVWithClassifierAndType(t *testing.T) {
dep, err := ParseGAV("org.apache.camel:camel-core:war:test:2.21.1")
assert.Nil(t, err)
- assert.Equal(t, dep.GroupId, "org.apache.camel")
- assert.Equal(t, dep.ArtifactId, "camel-core")
+ assert.Equal(t, dep.GroupID, "org.apache.camel")
+ assert.Equal(t, dep.ArtifactID, "camel-core")
assert.Equal(t, dep.Version, "2.21.1")
assert.Equal(t, dep.Type, "war")
assert.Equal(t, dep.Classifier, "test")
diff --git a/pkg/util/maven/types.go b/pkg/util/maven/types.go
index 5b1158e..db4b897 100644
--- a/pkg/util/maven/types.go
+++ b/pkg/util/maven/types.go
@@ -21,42 +21,49 @@ import (
"encoding/xml"
)
-type ProjectDefinition struct {
+// Integration --
+type Integration struct {
Project Project
JavaSources map[string]string
Resources map[string]string
Env map[string]string // TODO: should we deprecate it ? env are set on deployment
}
+// Project represent a maven project
type Project struct {
XMLName xml.Name
- XmlNs string `xml:"xmlns,attr"`
- XmlNsXsi string `xml:"xmlns:xsi,attr"`
+ XMLNs string `xml:"xmlns,attr"`
+ XMLNsXsi string `xml:"xmlns:xsi,attr"`
XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"`
ModelVersion string `xml:"modelVersion"`
- GroupId string `xml:"groupId"`
- ArtifactId string `xml:"artifactId"`
+ GroupID string `xml:"groupId"`
+ ArtifactID string `xml:"artifactId"`
Version string `xml:"version"`
DependencyManagement DependencyManagement `xml:"dependencyManagement"`
Dependencies Dependencies `xml:"dependencies"`
}
+// DependencyManagement represent maven's dependency management block
type DependencyManagement struct {
Dependencies Dependencies `xml:"dependencies"`
}
+// Dependencies --
type Dependencies struct {
Dependencies []Dependency `xml:"dependency"`
}
+// Add a dependency to maven's dependencies
func (deps *Dependencies) Add(dep Dependency) {
deps.Dependencies = append(deps.Dependencies, dep)
}
-func (deps *Dependencies) AddGAV(groupId string, artifactId string, version string) {
- deps.Add(NewDependency(groupId, artifactId, version))
+// AddGAV a dependency to maven's dependencies
+func (deps *Dependencies) AddGAV(groupID string, artifactID string, version string) {
+ deps.Add(NewDependency(groupID, artifactID, version))
}
+// AddEncodedGAV a dependency to maven's dependencies
func (deps *Dependencies) AddEncodedGAV(gav string) {
if d, err := ParseGAV(gav); err == nil {
// TODO: error handling
@@ -64,19 +71,21 @@ func (deps *Dependencies) AddEncodedGAV(gav string) {
}
}
+// Dependency represent a maven's dependency
type Dependency struct {
- GroupId string `xml:"groupId"`
- ArtifactId string `xml:"artifactId"`
+ GroupID string `xml:"groupId"`
+ ArtifactID string `xml:"artifactId"`
Version string `xml:"version,omitempty"`
Type string `xml:"type,omitempty"`
Classifier string `xml:"classifier,omitempty"`
Scope string `xml:"scope,omitempty"`
}
-func NewDependency(groupId string, artifactId string, version string) Dependency {
+// NewDependency create an new dependency from the given gav info
+func NewDependency(groupID string, artifactID string, version string) Dependency {
return Dependency{
- GroupId: groupId,
- ArtifactId: artifactId,
+ GroupID: groupID,
+ ArtifactID: artifactID,
Version: version,
Type: "jar",
Classifier: "",