Skip to content

Commit

Permalink
fix: bump of golangci-lint to v1.56.1 needed some code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz committed Feb 12, 2024
1 parent ca8edc3 commit 5fca1bf
Show file tree
Hide file tree
Showing 33 changed files with 48 additions and 50 deletions.
1 change: 0 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func doBuild(opts dib.BuildOpts) error {
if err := report.Generate(res, dibBuilder.Graph); err != nil {
return fmt.Errorf("cannot generate report: %w", err)
}

if err := res.CheckError(); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func initConfig() {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
// Non-blocking, because some command does not require config file, ie: docgen.
logger.Warnf("Unable read from config: %s", err)
logger.Warnf(err.Error())
return
}
logger.Infof("Using config file: %s", viper.ConfigFileUsed())
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
var versionCmd = &cobra.Command{
Use: "version",
Short: "print current dib version",
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
goVersion := "unknown"
buildInfo, available := debug.ReadBuildInfo()
if available {
Expand Down
2 changes: 1 addition & 1 deletion internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func SetLevel(level *string) {

switch *level {
case "debug":
Infof("debug mode enabled")
log.Level = LogLevelDebug
case "info":
log.Level = LogLevelInfo
Expand All @@ -71,6 +70,7 @@ func SetLevel(level *string) {
}

logger.Store(log)
Infof("Log level set to %s", log.Level)
}

// LogLevelStyle returns the style of the prefix for each log level.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dag/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func Test_WalkParallel_RunsAllNodes(t *testing.T) {
})

var length int
tracking.Range(func(k, v interface{}) bool {
tracking.Range(func(_, _ interface{}) bool {
length++
return true
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/dib/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func buildNode(
return fmt.Errorf("failed to create folder %s: %w", buildReportDir, err)
}

filePath := path.Join(buildReportDir, fmt.Sprintf("%s.txt", strings.ReplaceAll(img.ShortName, "/", "_")))
filePath := path.Join(buildReportDir, strings.ReplaceAll(img.ShortName, "/", "_")+".txt")
fileOutput, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create file %s: %w", filePath, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/dib/build_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dib_test

import (
"fmt"
"errors"
"os"
"path"
"testing"
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestRebuildGraph(t *testing.T) {
return graph
},
testRunners: []types.TestRunner{&mock.TestRunner{
ReturnedError: fmt.Errorf("mock test failed"),
ReturnedError: errors.New("mock test failed"),
}},
expBuildReports: []report.BuildReport{
{
Expand Down
1 change: 0 additions & 1 deletion pkg/dib/generate_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ func hashFiles(
}

humanReadableHash, err = humanhash.HumanizeUsing(hash.Sum(nil), humanizedHashWordLength, worldListToUse, "-")

if err != nil {
return "", fmt.Errorf("could not humanize hash: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/dib/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dib

import (
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -74,7 +75,8 @@ func ParseOutputOptions(output string) (FormatOpts, error) {
switch parsed[0] {
case GoTemplateFileFormat:
if len(parsed) == 1 {
return formatOpts, fmt.Errorf("you need to provide a path to template file when using \"go-template-file\" options")
return formatOpts, errors.New(
"you need to provide a path to template file when using \"go-template-file\" options")
}
formatOpts.Type = GoTemplateFileFormat
formatOpts.TemplatePath = parsed[1]
Expand Down
2 changes: 1 addition & 1 deletion pkg/dib/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func loadGitLabMeta(meta *ImageMetadata) {
meta.Authors = os.Getenv("GITLAB_USER_NAME")
meta.Revision = os.Getenv("CI_COMMIT_SHA")

repoURL := fmt.Sprintf("%s/-/blob", os.Getenv("CI_PROJECT_URL"))
repoURL := os.Getenv("CI_PROJECT_URL") + "/-/blob"
meta.repositoryRootURL = fmt.Sprintf("%s/%s", repoURL, os.Getenv("CI_DEFAULT_BRANCH"))
meta.repositoryCommitURL = fmt.Sprintf("%s/%s", repoURL, meta.Revision)
meta.repositoryRootPath = os.Getenv("CI_PROJECT_DIR")
Expand Down
6 changes: 3 additions & 3 deletions pkg/dib/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/require"
)

func Test_LabelsFromGitHubMetadata(t *testing.T) { //nolint:paralleltest
func Test_LabelsFromGitHubMetadata(t *testing.T) {
now := time.Now()
cwd, err := os.Getwd()
require.NoError(t, err)
Expand Down Expand Up @@ -60,7 +60,7 @@ func Test_LabelsFromGitHubMetadata(t *testing.T) { //nolint:paralleltest
assert.Equal(t, expected, actual)
}

func Test_LabelsFromGitLabMetadata(t *testing.T) { //nolint:paralleltest
func Test_LabelsFromGitLabMetadata(t *testing.T) {
now := time.Now()
cwd, err := os.Getwd()
require.NoError(t, err)
Expand Down Expand Up @@ -104,7 +104,7 @@ func Test_LabelsFromGitLabMetadata(t *testing.T) { //nolint:paralleltest
assert.Equal(t, expected, actual)
}

func Test_LabelsFromGitMetadata(t *testing.T) { //nolint:paralleltest
func Test_LabelsFromGitMetadata(t *testing.T) {
// Since we run our CI on GitHub, we need to reset this variable to make the test pass.
t.Setenv("GITHUB_REPOSITORY", "")

Expand Down
7 changes: 4 additions & 3 deletions pkg/exec/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func (e ShellExecutor) Execute(name string, args ...string) (string, error) {
cmd.Stdout = &stdout
cmd.Dir = e.Dir

logger.Debugf("Executing command: %s", cmd)
if err := cmd.Run(); err != nil {
return stderr.String(), fmt.Errorf("failed to execute command `%s`: %w", name, err)
return stderr.String(), fmt.Errorf("failed to execute command: %s: %w", cmd, err)
}

return stdout.String(), nil
Expand All @@ -53,9 +54,9 @@ func (e ShellExecutor) ExecuteWithWriters(stdout, stderr io.Writer, name string,
cmd.Stdout = stdout
cmd.Dir = e.Dir

logger.Debugf("cmd: %s", cmd.String())
logger.Debugf("Executing command: %s", cmd)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to execute command `%s`: %w", name, err)
return fmt.Errorf("failed to execute command: %s: %w", cmd, err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/goss/executor_dgoss.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewDGossExecutor() *DGossExecutor {
func (e DGossExecutor) Execute(_ context.Context, output io.Writer, opts types.RunTestOptions, args ...string) error {
shell := &exec.ShellExecutor{
Dir: opts.DockerContextPath,
Env: append(os.Environ(), fmt.Sprintf("GOSS_OPTS=%s", strings.Join(args, " "))),
Env: append(os.Environ(), "GOSS_OPTS="+strings.Join(args, " ")),
}

cmd := fmt.Sprintf("dgoss run --rm --tty --entrypoint='' %s sh", opts.ImageReference)
Expand Down
1 change: 0 additions & 1 deletion pkg/goss/executor_dgoss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func Test_DGossExecutor_NewDGossExecutorUsesDefaultShell(t *testing.T) {
assert.Equal(t, "/bin/bash", executor.Shell)
}

//nolint:paralleltest
func Test_DGossExecutor_NewDGossExecutorDetectsShellFromEnv(t *testing.T) {
t.Setenv("SHELL", "/path/to/shell")

Expand Down
2 changes: 1 addition & 1 deletion pkg/goss/executor_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (e KubernetesExecutor) Execute(ctx context.Context, output io.Writer, opts
}

watcher, err := e.clientSet.CoreV1().Pods(e.PodConfig.Namespace).Watch(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("app.kubernetes.io/instance=%s", pod.Name),
LabelSelector: "app.kubernetes.io/instance=" + pod.Name,
Watch: true,
})
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/goss/executor_kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ spec:
<-time.After(1 * time.Second)

// Check the created Pod
pod, err := clientSet.CoreV1().Pods("goss-ns").Get(context.Background(), "goss-pod", metav1.GetOptions{})
require.NoError(t, err)
pod, err := clientSet.CoreV1().Pods("goss-ns").
Get(context.Background(), "goss-pod", metav1.GetOptions{})
assert.NoError(t, err)

// Pod assertions
assert.Equal(t, expectedLabels, pod.Labels)
Expand Down
4 changes: 1 addition & 3 deletions pkg/kaniko/context_local.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package kaniko

import (
"fmt"

"github.com/radiofrance/dib/pkg/types"
)

Expand All @@ -17,5 +15,5 @@ func NewLocalContextProvider() *LocalContextProvider {
// PrepareContext has nothing to do because the build context already exists locally.
// It just returns the path of the existing local context, prefixed by the Kaniko `dir://` indicator.
func (c LocalContextProvider) PrepareContext(opts types.ImageBuilderOpts) (string, error) {
return fmt.Sprintf("dir://%s", opts.Context), nil
return "dir://" + opts.Context, nil
}
2 changes: 1 addition & 1 deletion pkg/kaniko/context_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewRemoteContextProvider(uploader FileUploader) *RemoteContextProvider {
func (c RemoteContextProvider) PrepareContext(opts types.ImageBuilderOpts) (string, error) {
tagParts := strings.Split(opts.Tags[0], ":")
shortName := path.Base(tagParts[0])
remoteDir := fmt.Sprintf("kaniko/%s", shortName)
remoteDir := "kaniko/" + shortName
filename := fmt.Sprintf("context-kaniko-%s-%s.tar.gz", shortName, tagParts[1])

tarGzPath := path.Join(opts.Context, filename)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kaniko/executor_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type DockerExecutor struct {
func NewDockerExecutor(exec exec.Executor, config ContainerConfig) *DockerExecutor {
dockerCfg := os.Getenv("DOCKER_CONFIG")
if dockerCfg == "" {
dockerCfg = fmt.Sprintf("%s/.docker", os.Getenv("HOME"))
dockerCfg = os.Getenv("HOME") + "/.docker"
}

return &DockerExecutor{
Expand Down
1 change: 0 additions & 1 deletion pkg/kaniko/executor_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/require"
)

//nolint:paralleltest
func Test_DockerExecutor_Execute(t *testing.T) {
t.Setenv("HOME", "/home/dib")

Expand Down
5 changes: 3 additions & 2 deletions pkg/kaniko/executor_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kaniko

import (
"context"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -33,7 +34,7 @@ func NewKubernetesExecutor(clientSet kubernetes.Interface, config k8sutils.PodCo
func (e KubernetesExecutor) Execute(ctx context.Context, output io.Writer, args []string) error {
logger.Infof("Building image with kaniko kubernetes executor")
if e.DockerConfigSecret == "" {
return fmt.Errorf("the DockerConfigSecret option is required")
return errors.New("the DockerConfigSecret option is required")
}

podName := e.PodConfig.Name
Expand Down Expand Up @@ -166,7 +167,7 @@ func (e KubernetesExecutor) Execute(ctx context.Context, output io.Writer, args
}

watcher, err := e.clientSet.CoreV1().Pods(e.PodConfig.Namespace).Watch(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("app.kubernetes.io/instance=%s", pod.Name),
LabelSelector: "app.kubernetes.io/instance=" + pod.Name,
Watch: true,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kaniko/executor_kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ spec:

// Check the created Pod
pod, err := clientSet.CoreV1().Pods("kaniko-ns").Get(context.Background(), "kaniko-pod", metav1.GetOptions{})
require.NoError(t, err)
assert.NoError(t, err)

// Pod assertions
assert.Equal(t, expectedLabels, pod.Labels)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func WaitPodReady(ctx context.Context, watcher watch.Interface) (chan struct{},
return
}
case <-time.After(1 * time.Hour):
errChan <- fmt.Errorf("timeout waiting for pod to run to completion")
errChan <- errors.New("timeout waiting for pod to run to completion")
return
case <-ctx.Done():
errChan <- fmt.Errorf("stop wating for pod: %w", ctx.Err())
Expand Down
5 changes: 3 additions & 2 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package report

import (
"errors"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -120,10 +121,10 @@ func (r Report) Print() {
func (r Report) CheckError() error {
for _, buildReport := range r.BuildReports {
if buildReport.BuildStatus == BuildStatusError {
return fmt.Errorf("one of the image build failed, see the report for more details")
return errors.New("one of the image build failed, see the report for more details")
}
if buildReport.TestsStatus == TestsStatusFailed {
return fmt.Errorf("some tests failed, see report for more details")
return errors.New("some tests failed, see report for more details")
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestReport_GetTrivyReportDir(t *testing.T) {
}
}

func TestReport_GetReportURL_Gitlab(t *testing.T) { //nolint:paralleltest
func TestReport_GetReportURL_Gitlab(t *testing.T) {
t.Setenv("CI_JOB_URL", "https://gitlab.com/example-repository/-/jobs/123456")
dibReport := report.Report{
Options: report.Options{
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Init(
RootDir: rootDir,
Name: generationDate.Format("20060102150405"),
GenerationDate: generationDate,
Version: fmt.Sprintf("v%s", version),
Version: "v" + version,
BuildOpts: buildOpts,
WithGraph: !disableGenerateGraph,
WithGoss: isTestRunnerEnabled(types.TestRunnerGoss, testRunners),
Expand Down
11 changes: 5 additions & 6 deletions pkg/report/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package report

import (
"fmt"
"html/template"
"os"
"path"
Expand Down Expand Up @@ -33,18 +32,18 @@ var templateFuncs = template.FuncMap{
func (r Report) renderTemplate(name string, reportOpts Options, reportData any) error {
// The order matter for inheritance
files := []string{
path.Join(templatesDir, "_layout.go.html"), // base layout
path.Join(templatesDir, "_nav.go.html"), // navbar
path.Join(templatesDir, "_functions.go.html"), // helpers & utils functions
path.Join(templatesDir, fmt.Sprintf("%s.go.html", name)), // report page
path.Join(templatesDir, "_layout.go.html"), // base layout
path.Join(templatesDir, "_nav.go.html"), // navbar
path.Join(templatesDir, "_functions.go.html"), // helpers & utils functions
path.Join(templatesDir, name+".go.html"), // report page
}

tpl, err := template.New("layout").Funcs(templateFuncs).ParseFS(templatesFS, files...)
if err != nil {
return err
}

writer, err := os.Create(fmt.Sprintf("%s.html", path.Join(r.GetRootDir(), name)))
writer, err := os.Create(path.Join(r.GetRootDir(), name) + ".html")
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/trivy/executor_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package trivy

import (
"context"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -36,7 +37,7 @@ func (e KubernetesExecutor) Execute(ctx context.Context, output io.Writer, args
logger.Debugf("Running container with args '%s'", strings.Join(args, " "))

if e.DockerConfigSecret == "" {
return fmt.Errorf("the DockerConfigSecret option is required")
return errors.New("the DockerConfigSecret option is required")
}

podName := e.PodConfig.Name
Expand Down Expand Up @@ -127,7 +128,7 @@ func (e KubernetesExecutor) Execute(ctx context.Context, output io.Writer, args
}

watcher, err := e.clientSet.CoreV1().Pods(e.PodConfig.Namespace).Watch(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("app.kubernetes.io/instance=%s", pod.Name),
LabelSelector: "app.kubernetes.io/instance=" + pod.Name,
Watch: true,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/trivy/executor_kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ spec:

// Check the created Pod
pod, err := clientSet.CoreV1().Pods("trivy-ns").Get(context.Background(), "trivy-pod", metav1.GetOptions{})
require.NoError(t, err)
assert.NoError(t, err)

// Pod assertions
assert.Equal(t, expectedLabels, pod.Labels)
Expand Down
Loading

0 comments on commit 5fca1bf

Please sign in to comment.