Skip to content

Commit

Permalink
Initial pr for golint (#1304)
Browse files Browse the repository at this point in the history
* Initial pr for golint

* more linting fixes

* lint fixes for tests, gocognit and gocritic

* goheader, gci and imprtas linters

* jujuparams lint

* missing uuids

* All linting issues solved

* fix errs import

* test fixes

---------

Co-authored-by: Ales Stimec <[email protected]>
  • Loading branch information
ale8k and alesstimec authored Aug 9, 2024
1 parent 560b7de commit 36c15a7
Show file tree
Hide file tree
Showing 290 changed files with 949 additions and 764 deletions.
90 changes: 90 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Golangci-lint configuration.
#
# If a line has a comment, it means it has been changed from the default.
# This helps us understand what we're tweaking and why.

run:
timeout: "5m" # Allow at least 5 minutes
issues-exit-code: 1
tests: true
allow-parallel-runners: false
allow-serial-runners: false
# go: "1.17" # Do not set a go limit

issues:
exclude-use-default: true
exclude-case-sensitive: false
exclude-dirs-use-default: true
max-issues-per-linter: 50
max-same-issues: 3
new: false
fix: true
whole-files: false

output:
print-issued-lines: true
print-linter-name: true
uniq-by-line: true
# path-prefix: # Not needed
show-stats: false
sort-results: true

linters:
disable-all: true
enable:
# The following linters are enabled by default
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused

# The following linters are additional

# Bug based linters
- gosec
- sqlclosecheck
- reassign
- nilerr
- durationcheck
- bodyclose
# - contextcheck # Issue right now

# Style based linters
- promlinter
- gocritic
# - gocognit # To be fixed
- goheader
- importas
- gci

linters-settings:
gosec:
exclude-generated: false
severity: low
confidence: low
excludes:
- G601 # Implicit memory aliasing in for loop. Fixed in Go1.22+, as such exclude.
gocognit:
min-complexity: 30
goheader:
template: |-
Copyright 2024 Canonical.
importas:
no-unaliased: false
no-extra-aliases: false
alias:
- pkg: github.com/juju/juju/rpc/params
alias: jujuparams
- pkg: github.com/canonical/jimm/v3/internal/openfga/names
alias: ofganames
- pkg: github.com/frankban/quicktest
alias: qt
gci:
skip-generated: true
custom-order: true
sections:
- standard
- default
- localmodule
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast"
],
"go.lintOnSave": "workspace",
}
2 changes: 1 addition & 1 deletion cmd/jaas/cmd/addserviceaccount.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

Expand Down
2 changes: 1 addition & 1 deletion cmd/jaas/cmd/addserviceaccount_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd_test

Expand Down
2 changes: 1 addition & 1 deletion cmd/jaas/cmd/export_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

Expand Down
20 changes: 16 additions & 4 deletions cmd/jaas/cmd/grant.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright 2024 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

import (
"fmt"

"github.com/juju/cmd/v3"
"github.com/juju/gnuflag"
jujuapi "github.com/juju/juju/api"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/modelcmd"
Expand Down Expand Up @@ -57,6 +56,14 @@ func (c *grantCommand) Info() *cmd.Info {
})
}

// Init implements the cmd.Command interface.
func (c *grantCommand) SetFlags(f *gnuflag.FlagSet) {
c.CommandBase.SetFlags(f)
c.out.AddFlags(f, "smart", map[string]cmd.Formatter{
"smart": cmd.FormatSmart,
})
}

// Init implements the cmd.Command interface.
func (c *grantCommand) Init(args []string) error {
if len(args) < 1 {
Expand Down Expand Up @@ -92,6 +99,11 @@ func (c *grantCommand) Run(ctxt *cmd.Context) error {
if err != nil {
return errors.E(err)
}
fmt.Fprintln(ctxt.Stdout, "access granted")
err = c.out.Write(ctxt, "access granted")
if err != nil {
return errors.E(err)
}

// fmt.Fprintf(ctxt.Stdout, "access granted")
return nil
}
2 changes: 1 addition & 1 deletion cmd/jaas/cmd/grant_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd_test

Expand Down
3 changes: 2 additions & 1 deletion cmd/jaas/cmd/listserviceaccountcredentials.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

Expand Down Expand Up @@ -31,6 +31,7 @@ This command only shows credentials uploaded to the controller that belong to th
Client-side credentials should be managed via the juju credentials command.
`
//nolint:gosec // Believes credentials are exposed but aren't.
listServiceAccountCredentialsExamples = `
juju list-service-account-credentials <client-id>
juju list-service-account-credentials <client-id> --show-secrets
Expand Down
2 changes: 1 addition & 1 deletion cmd/jaas/cmd/listserviceaccountcredentials_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd_test

Expand Down
2 changes: 1 addition & 1 deletion cmd/jaas/cmd/package_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd_test

Expand Down
5 changes: 2 additions & 3 deletions cmd/jaas/cmd/updatecredentials.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

Expand All @@ -11,10 +11,9 @@ import (
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/jujuclient"
"github.com/juju/names/v5"

"github.com/juju/juju/rpc/params"
jujuparams "github.com/juju/juju/rpc/params"
"github.com/juju/names/v5"

"github.com/canonical/jimm/v3/internal/errors"
"github.com/canonical/jimm/v3/pkg/api"
Expand Down
6 changes: 3 additions & 3 deletions cmd/jaas/cmd/updatecredentials_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd_test

Expand All @@ -7,6 +7,8 @@ import (
"fmt"

"github.com/juju/cmd/v3/cmdtesting"
jujucloud "github.com/juju/juju/cloud"
"github.com/juju/juju/rpc/params"
"github.com/juju/names/v5"
gc "gopkg.in/check.v1"

Expand All @@ -18,8 +20,6 @@ import (
"github.com/canonical/jimm/v3/internal/openfga"
ofganames "github.com/canonical/jimm/v3/internal/openfga/names"
jimmnames "github.com/canonical/jimm/v3/pkg/names"
jujucloud "github.com/juju/juju/cloud"
"github.com/juju/juju/rpc/params"
)

type updateCredentialsSuite struct {
Expand Down
5 changes: 3 additions & 2 deletions cmd/jaas/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Canonical Ltd.
// Copyright 2024 Canonical.

package main

Expand All @@ -7,8 +7,9 @@ import (
"os"
"strings"

"github.com/canonical/jimm/v3/cmd/jaas/cmd"
jujucmd "github.com/juju/cmd/v3"

"github.com/canonical/jimm/v3/cmd/jaas/cmd"
)

var jaasDoc = `
Expand Down
6 changes: 3 additions & 3 deletions cmd/jimmctl/cmd/addcloudtocontroller.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2021 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

import (
"fmt"
"io/ioutil"
"os"

"github.com/juju/cmd/v3"
"github.com/juju/gnuflag"
Expand Down Expand Up @@ -194,7 +194,7 @@ type cloudToCommandAdapter struct {

// ReadCloudData implements CloudMetadataStore.ReadCloudData.
func (cloudToCommandAdapter) ReadCloudData(path string) ([]byte, error) {
return ioutil.ReadFile(path)
return os.ReadFile(path)
}

// ParseOneCloud implements CloudMetadataStore.ParseOneCloud.
Expand Down
11 changes: 6 additions & 5 deletions cmd/jimmctl/cmd/addcloudtocontroller_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright 2021 Canonical Ltd.
// Copyright 2024 Canonical.
package cmd_test

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -187,7 +186,8 @@ clouds:
err = s.JIMM.Database.GetCloud(context.Background(), &cloud)
c.Assert(err, gc.IsNil)
controller := dbmodel.Controller{Name: "controller-1"}
s.JIMM.Database.GetController(context.Background(), &controller)
err = s.JIMM.Database.GetController(context.Background(), &controller)
c.Assert(err, gc.IsNil)
c.Assert(controller.CloudRegions[test.expectedIndex].CloudRegion.CloudName, gc.Equals, test.expectedCloudName)
}
cleanupFunc()
Expand All @@ -197,11 +197,12 @@ clouds:
}

func writeTempFile(c *gc.C, content string) (string, func()) {
dir, err := ioutil.TempDir("", "add-cloud-to-controller-test")
dir, err := os.MkdirTemp("", "add-cloud-to-controller-test")
c.Assert(err, gc.Equals, nil)

tmpfn := filepath.Join(dir, "tmp.yaml")
err = ioutil.WriteFile(tmpfn, []byte(content), 0666)

err = os.WriteFile(tmpfn, []byte(content), 0600)
c.Assert(err, gc.Equals, nil)
return tmpfn, func() {
os.RemoveAll(dir)
Expand Down
2 changes: 1 addition & 1 deletion cmd/jimmctl/cmd/addcontroller.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

Expand Down
7 changes: 3 additions & 4 deletions cmd/jimmctl/cmd/addcontroller_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright 2021 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd_test

import (
"context"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -110,11 +109,11 @@ func writeYAMLTempFile(c *gc.C, payload interface{}) (string, string) {
data, err := yaml.Marshal(payload)
c.Assert(err, gc.Equals, nil)

dir, err := ioutil.TempDir("", "add-controller-test")
dir, err := os.MkdirTemp("", "add-controller-test")
c.Assert(err, gc.Equals, nil)

tmpfn := filepath.Join(dir, "tmp.yaml")
err = ioutil.WriteFile(tmpfn, data, 0666)
err = os.WriteFile(tmpfn, data, 0600)
c.Assert(err, gc.Equals, nil)
return dir, tmpfn
}
2 changes: 1 addition & 1 deletion cmd/jimmctl/cmd/auth.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

Expand Down
4 changes: 2 additions & 2 deletions cmd/jimmctl/cmd/controllerinfo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

Expand Down Expand Up @@ -123,7 +123,7 @@ func (c *controllerInfoCommand) Run(ctxt *cmd.Context) error {
if err != nil {
return errors.Mask(err)
}
err = os.WriteFile(c.file.Path, data, 0666)
err = os.WriteFile(c.file.Path, data, 0600)
if err != nil {
return errors.Mask(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/jimmctl/cmd/controllerinfo_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd_test

Expand Down
2 changes: 1 addition & 1 deletion cmd/jimmctl/cmd/crossmodelquery.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd

Expand Down
9 changes: 5 additions & 4 deletions cmd/jimmctl/cmd/crossmodelquery_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// Copyright 2023 Canonical Ltd.
// Copyright 2024 Canonical.

package cmd_test

import (
"encoding/json"

"github.com/canonical/jimm/v3/cmd/jimmctl/cmd"
"github.com/canonical/jimm/v3/internal/cmdtest"
"github.com/canonical/jimm/v3/internal/jimmtest"
"github.com/juju/cmd/v3/cmdtesting"
jujuparams "github.com/juju/juju/rpc/params"
"github.com/juju/juju/testing/factory"
"github.com/juju/names/v5"
gc "gopkg.in/check.v1"

"github.com/canonical/jimm/v3/cmd/jimmctl/cmd"
"github.com/canonical/jimm/v3/internal/cmdtest"
"github.com/canonical/jimm/v3/internal/jimmtest"
)

type crossModelQuerySuite struct {
Expand Down
Loading

0 comments on commit 36c15a7

Please sign in to comment.