Skip to content

Commit

Permalink
chore: add all sorts of linters and pre-commit ...
Browse files Browse the repository at this point in the history
...hook

This adds a myriad of linters from golangci/golangci-lint[1] and a
pre-commit hook to execute them on `git commit`. Some lint checks needed
to be disabled to keep the PR not to big to merge.

To install pre-commit look at the Installation instructions[2] of
pre-commit project.

To install golangci-lint follow the Local Installation instructions[3].

[1] https://github.com/golangci/golangci-lint
[2] https://pre-commit.com/#install
[3] https://github.com/golangci/golangci-lint#local-installation
  • Loading branch information
zregvart committed Dec 5, 2018
1 parent 24e9d2f commit 144e04d
Show file tree
Hide file tree
Showing 59 changed files with 329 additions and 172 deletions.
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
linters-settings:
lll:
line-length: 150
linters:
enable-all: true
disable:
- dupl
- gochecknoinits
- gochecknoglobals
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- repo: git://github.com/dnephin/pre-commit-golang
sha: HEAD
exclude:
- vendor/.*
hooks:
- id: golangci-lint
3 changes: 2 additions & 1 deletion build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ test-integration: check-integration
check-integration:
go test ./... -tags=integration


lint:
golangci-lint run

.PHONY: build build-operator build-kamel build-embed-resources build-runtime dep codegen images images-build images-push test check test-integration check-integration clean release prepare-release cross-compile new-version git-tag increment-snapshot install-minishift
8 changes: 8 additions & 0 deletions docs/developers.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ In order to build the project, you need to comply with the following requirement
* **Operator SDK v0.0.7+**: used to build the operator and the Docker images. Instructions in the https://github.com/operator-framework/operator-sdk[Operator SDK website] (binary downloads available in the release page).
* **GNU Make**: used to define composite build actions. This should be already installed or available as package if you have a good OS (https://www.gnu.org/software/make/).

[[checks]]
== Running checks
Checks rely on `golangci-lint` being installed, to install it look at the https://github.com/golangci/golangci-lint#local-installation[Local Installation] instructions.

You can run checks via `make lint` or you can install a GIT pre-commit hook and have the checks run via https://pre-commit.com[pre-commit]; then make sure to install the pre-commit hooks after installing pre-commit by running

$ pre-commit install

[[checking-out]]
== Checking Out the Sources

Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/camel/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ const (
LanguageXML Language = "xml"
// LanguageKotlin --
LanguageKotlin Language = "kts"
// KamelPlatform --
KamelPlatform = "platform"
)

// A IntegrationTraitSpec contains the configuration of a trait
Expand Down
4 changes: 1 addition & 3 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ func (b *defaultBuilder) submit(request Request) {
}

r.Artifacts = make([]v1alpha1.Artifact, 0, len(c.Artifacts))
for _, artifact := range c.Artifacts {
r.Artifacts = append(r.Artifacts, artifact)
}
r.Artifacts = append(r.Artifacts, c.Artifacts...)

// update the cache
b.request.Store(request.Meta.Name, r)
Expand Down
13 changes: 7 additions & 6 deletions pkg/builder/builder_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,25 @@ func GenerateProject(ctx *Context) error {
deps.AddGAV("org.apache.camel.k", "camel-k-runtime-jvm", version.Version)

for _, d := range ctx.Request.Dependencies {
if strings.HasPrefix(d, "camel:") {
switch {
case strings.HasPrefix(d, "camel:"):
artifactID := strings.TrimPrefix(d, "camel:")

if !strings.HasPrefix(artifactID, "camel-") {
artifactID = "camel-" + artifactID
}

deps.AddGAV("org.apache.camel", artifactID, "")
} else if strings.HasPrefix(d, "mvn:") {
case strings.HasPrefix(d, "mvn:"):
mid := strings.TrimPrefix(d, "mvn:")
gav := strings.Replace(mid, "/", ":", -1)

deps.AddEncodedGAV(gav)
} else if strings.HasPrefix(d, "runtime:") {
case strings.HasPrefix(d, "runtime:"):
artifactID := strings.Replace(d, "runtime:", "camel-k-runtime-", 1)

deps.AddGAV("org.apache.camel.k", artifactID, version.Version)
} else {
default:
return fmt.Errorf("unknown dependency type: %s", d)
}
}
Expand Down Expand Up @@ -269,7 +270,7 @@ func ListPublishedImages(namespace string) ([]PublishedImage, error) {
if ctx.Status.Phase != v1alpha1.IntegrationContextPhaseReady || ctx.Labels == nil {
continue
}
if ctxType, present := ctx.Labels["camel.apache.org/context.type"]; !present || ctxType != "platform" {
if ctxType, present := ctx.Labels["camel.apache.org/context.type"]; !present || ctxType != v1alpha1.KamelPlatform {
continue
}

Expand All @@ -292,7 +293,7 @@ func FindBestImage(images []PublishedImage, entries []v1alpha1.Artifact) (*Publi
}

var bestImage PublishedImage
bestImageCommonLibs := make(map[string]bool, 0)
bestImageCommonLibs := make(map[string]bool)
bestImageSurplusLibs := 0
for _, image := range images {
common := make(map[string]bool)
Expand Down
8 changes: 7 additions & 1 deletion pkg/builder/kaniko/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/operator-framework/operator-sdk/pkg/sdk"
"github.com/pkg/errors"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -45,6 +46,7 @@ func Publisher(ctx *builder.Context) error {
return err
}

// #nosec G202
dockerFileContent := []byte(`
FROM ` + ctx.Image + `
ADD . /deployments
Expand Down Expand Up @@ -137,7 +139,11 @@ func Publisher(ctx *builder.Context) error {
},
}

sdk.Delete(&pod)
err = sdk.Delete(&pod)
if err != nil && !apierrors.IsNotFound(err) {
return errors.Wrap(err, "cannot delete kaniko builder pod")
}

err = sdk.Create(&pod)
if err != nil {
return errors.Wrap(err, "cannot create kaniko builder pod")
Expand Down
18 changes: 15 additions & 3 deletions pkg/builder/s2i/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

buildv1 "github.com/openshift/api/build/v1"
imagev1 "github.com/openshift/api/image/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/pkg/errors"
Expand Down Expand Up @@ -71,8 +72,12 @@ func Publisher(ctx *builder.Context) error {
},
}

sdk.Delete(&bc)
err := sdk.Create(&bc)
err := sdk.Delete(&bc)
if err != nil && !apierrors.IsNotFound(err) {
return errors.Wrap(err, "cannot delete build config")
}

err = sdk.Create(&bc)
if err != nil {
return errors.Wrap(err, "cannot create build config")
}
Expand All @@ -93,7 +98,11 @@ func Publisher(ctx *builder.Context) error {
},
}

sdk.Delete(&is)
err = sdk.Delete(&is)
if err != nil && !apierrors.IsNotFound(err) {
return errors.Wrap(err, "cannot delete image stream")
}

err = sdk.Create(&is)
if err != nil {
return errors.Wrap(err, "cannot create image stream")
Expand Down Expand Up @@ -149,6 +158,9 @@ func Publisher(ctx *builder.Context) error {
}
return false, nil
}, 5*time.Minute)
if err != nil {
return err
}

err = sdk.Get(&is)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions pkg/builder/springboot/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func GenerateProject(ctx *builder.Context) error {
//

for _, d := range ctx.Request.Dependencies {
if strings.HasPrefix(d, "camel:") {
switch {
case strings.HasPrefix(d, "camel:"):
if d == "camel:core" {
continue
}
Expand Down Expand Up @@ -135,12 +136,12 @@ func GenerateProject(ctx *builder.Context) error {
},
},
})
} else if strings.HasPrefix(d, "mvn:") {
case strings.HasPrefix(d, "mvn:"):
mid := strings.TrimPrefix(d, "mvn:")
gav := strings.Replace(mid, "/", ":", -1)

deps.AddEncodedGAV(gav)
} else if strings.HasPrefix(d, "runtime:") {
case strings.HasPrefix(d, "runtime:"):
if d == "runtime:jvm" {
// common
continue
Expand All @@ -153,7 +154,7 @@ func GenerateProject(ctx *builder.Context) error {
artifactID := strings.Replace(d, "runtime:", "camel-k-runtime-", 1)

deps.AddGAV("org.apache.camel.k", artifactID, version.Version)
} else {
default:
return fmt.Errorf("unknown dependency type: %s", d)
}
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/client/cmd/completion_bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package cmd

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -171,7 +172,10 @@ func newCmdCompletionBash(root *cobra.Command) *cobra.Command {
Short: "Generates bash completion scripts",
Long: bashCompletionCmdLongDescription,
Run: func(cmd *cobra.Command, args []string) {
root.GenBashCompletion(os.Stdout)
err := root.GenBashCompletion(os.Stdout)
if err != nil {
fmt.Print(err.Error())
}
},
}
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/client/cmd/completion_zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,7 +50,10 @@ func newCmdCompletionZsh(root *cobra.Command) *cobra.Command {
Short: "Generates zsh completion scripts",
Long: zshCompletionCmdLongDescription,
Run: func(cmd *cobra.Command, args []string) {
root.GenZshCompletion(os.Stdout)
err := root.GenZshCompletion(os.Stdout)
if err != nil {
fmt.Print(err.Error())
}
},
}
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/client/cmd/context_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (command *contextCreateCommand) run(cmd *cobra.Command, args []string) erro
// the integration context already exists, let's check that it is
// not a platform one which is supposed to be "read only"

if ctx.Labels["camel.apache.org/context.type"] == "platform" {
if ctx.Labels["camel.apache.org/context.type"] == v1alpha1.KamelPlatform {
fmt.Printf("integration context \"%s\" is not editable\n", ctx.Name)
return nil
}
Expand All @@ -103,11 +103,12 @@ func (command *contextCreateCommand) run(cmd *cobra.Command, args []string) erro
}

for _, item := range command.dependencies {
if strings.HasPrefix(item, "mvn:") {
switch {
case strings.HasPrefix(item, "mvn:"):
ctx.Spec.Dependencies = append(ctx.Spec.Dependencies, item)
} else if strings.HasPrefix(item, "file:") {
case strings.HasPrefix(item, "file:"):
ctx.Spec.Dependencies = append(ctx.Spec.Dependencies, item)
} else if strings.HasPrefix(item, "camel-") {
case strings.HasPrefix(item, "camel-"):
ctx.Spec.Dependencies = append(ctx.Spec.Dependencies, "camel:"+strings.TrimPrefix(item, "camel-"))
}
}
Expand Down Expand Up @@ -145,15 +146,15 @@ func (command *contextCreateCommand) run(cmd *cobra.Command, args []string) erro
clone := ctx.DeepCopy()
err = sdk.Get(clone)
if err != nil {
fmt.Printf(err.Error())
fmt.Print(err.Error())
return nil
}
ctx.ResourceVersion = clone.ResourceVersion
err = sdk.Update(&ctx)
}

if err != nil {
fmt.Printf(err.Error())
fmt.Print(err.Error())
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/client/cmd/context_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func newContextDeleteCmd(rootCmdOptions *RootCmdOptions) *cobra.Command {
Short: "Delete an Integration Context",
Long: `Delete an Integration Context.`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := impl.validate(cmd, args); err != nil {
if err := impl.validate(args); err != nil {
return err
}
if err := impl.run(cmd, args); err != nil {
if err := impl.run(args); err != nil {
fmt.Println(err.Error())
}

Expand All @@ -59,7 +59,7 @@ type contextDeleteCommand struct {
all bool
}

func (command *contextDeleteCommand) validate(cmd *cobra.Command, args []string) error {
func (command *contextDeleteCommand) validate(args []string) error {
if command.all && len(args) > 0 {
return errors.New("invalid combination: both all flag and named contexts are set")
}
Expand All @@ -70,7 +70,7 @@ func (command *contextDeleteCommand) validate(cmd *cobra.Command, args []string)
return nil
}

func (command *contextDeleteCommand) run(cmd *cobra.Command, args []string) error {
func (command *contextDeleteCommand) run(args []string) error {
names := args

if command.all {
Expand All @@ -82,7 +82,7 @@ func (command *contextDeleteCommand) run(cmd *cobra.Command, args []string) erro
names = make([]string, 0, len(ctxList.Items))
for _, item := range ctxList.Items {
// only include non platform contexts
if item.Labels["camel.apache.org/context.type"] != "platform" {
if item.Labels["camel.apache.org/context.type"] != v1alpha1.KamelPlatform {
names = append(names, item.Name)
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (command *contextDeleteCommand) delete(name string) error {

// check that it is not a platform one which is supposed to be "read only"
// thus not managed by the end user
if ctx.Labels["camel.apache.org/context.type"] == "platform" {
if ctx.Labels["camel.apache.org/context.type"] == v1alpha1.KamelPlatform {
// skip platform contexts while deleting all contexts
if command.all {
return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/cmd/context_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func newContextGetCmd(rootCmdOptions *RootCmdOptions) *cobra.Command {
if err := impl.validate(cmd, args); err != nil {
return err
}
if err := impl.run(cmd, args); err != nil {
if err := impl.run(); err != nil {
fmt.Println(err.Error())
}

Expand All @@ -49,7 +49,7 @@ func newContextGetCmd(rootCmdOptions *RootCmdOptions) *cobra.Command {
}

cmd.Flags().BoolVar(&impl.user, "user", true, "Includes user contexts")
cmd.Flags().BoolVar(&impl.platform, "platform", true, "Includes platform contexts")
cmd.Flags().BoolVar(&impl.platform, v1alpha1.KamelPlatform, true, "Includes platform contexts")

return &cmd
}
Expand All @@ -65,7 +65,7 @@ func (command *contextGetCommand) validate(cmd *cobra.Command, args []string) er

}

func (command *contextGetCommand) run(cmd *cobra.Command, args []string) error {
func (command *contextGetCommand) run() error {
ctxList := v1alpha1.NewIntegrationContextList()
if err := sdk.List(command.Namespace, &ctxList); err != nil {
return err
Expand All @@ -76,7 +76,7 @@ func (command *contextGetCommand) run(cmd *cobra.Command, args []string) error {
for _, ctx := range ctxList.Items {
t := ctx.Labels["camel.apache.org/context.type"]
u := command.user && t == "user"
p := command.platform && t == "platform"
p := command.platform && t == v1alpha1.KamelPlatform

if u || p {
fmt.Fprintf(w, "%s\t%s\t%s\n", ctx.Name, t, string(ctx.Status.Phase))
Expand Down
Loading

0 comments on commit 144e04d

Please sign in to comment.