Skip to content

feat: add golangci-lint configuration with pre-commit hooks to lint code #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint_pr_title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
steps:
- uses: amannn/action-semantic-pull-request@e9fabac35e210fea40ca5b14c0da95a099eff26f # v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
18 changes: 15 additions & 3 deletions .github/workflows/pr_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ on:
- ready_for_review

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.59
ci:
name: Continuous integration
runs-on: ubuntu-latest
Expand All @@ -21,15 +33,15 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: "1.22"

- name: Download Go dependencies
run: go get ./... && go mod tidy

- name: Check if all dependencies were commited
shell: bash
run: |
if [ "$(git status --porcelain)" ]; then
if [ "$(git status --porcelain)" ]; then
git status
exit 1
else
Expand All @@ -51,7 +63,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5
with:
go-version: '1.21'
go-version: "1.22"

- name: Download Go dependencies
run: go get ./... && go mod tidy
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
*.swp

# IDE
.idea/
.idea/
43 changes: 43 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
linters:
enable:
- asasalint
- copyloopvar
- decorder
- dogsled
- errname
- exhaustive
- exportloopref
- fatcontext
- forcetypeassert
- gci
- ginkgolinter
- gofmt
- grouper
- importas
- makezero
- nakedret
- nilerr
- nilnil
- predeclared
- sloglint
#- wrapcheck # this one will be useful, but we have to fix a multitude of errors
#- unparam # when fixed, probably already covered by revive/gocritic
#- revive # too many errors, but useful, similar to gocritic, so probably choose one
#- prealloc # enable when can
#- nestif # enable once we clean up the code
#- gofumpt # fix all files before we switch to gofumpt
#- gocritic # too many failures for now
#- goconst # once we rewrite string literals to consts
#- gochecknoglobals # once we rewrite terraform codegen and drop global functions
#- funlen # couple of functions over 60 lines
#- err113 # disabled because of too many dynamic errors that don't wrap anything

linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/paloaltonetworks/pan-os-codegen)
issues:
exclude-dirs:
- generated/
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/golangci/golangci-lint/
rev: v1.59.1
hooks:
- id: golangci-lint
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The code have run login in `cmd/codegen` directory, to run it with default optio
go run cmd/codegen/main.go
```
This command can be parametrizes using options:
- `-t/-type` - operation type, default is to create both Terraform
- `-t/-type` - operation type, default is to create both Terraform
- `mktp` - create only Terraform provider
- `mksdk` - create only PAN-OS SDK
- `config` - specify path for the config file, default is `cmd/codegen/config.yaml`
Expand All @@ -47,4 +47,4 @@ example code:
go run cmd/codegen/main.go -t mksdk
cd ../generated/pango
PANOS_HOSTNAME='***' PANOS_USERNAME='***' PANOS_PASSWORD='***' go run example/main.go
```
```
2 changes: 1 addition & 1 deletion SUPPORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ This template/script/solution is released “as-is”, with no warranty and no s
supported and Palo Alto Networks may contribute its expertise at its discretion. Palo Alto Networks, including through
its Authorized Support Centers (ASC) partners and backline support options, will not provide technical support or help
in using or troubleshooting this template/script/solution. The underlying product used by this template/script/solution
will still be supported in accordance with the product’s applicable support policy and the customer’s entitlements.
will still be supported in accordance with the product’s applicable support policy and the customer’s entitlements.
2 changes: 1 addition & 1 deletion assets/pango/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/PaloAltoNetworks/pango

go 1.21.4
go 1.22

require (
github.com/onsi/ginkgo/v2 v2.19.0
Expand Down
2 changes: 1 addition & 1 deletion assets/terraform/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/PaloAltoNetworks/terraform-provider-panos

go 1.21.4
go 1.22

require (
github.com/PaloAltoNetworks/pango v0.10.3-0.20240408115758-216d8509e7cf
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
)

go 1.21.4
go 1.22.5
25 changes: 14 additions & 11 deletions pkg/commands/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package codegen
import (
"context"
"fmt"
"log"

"github.com/paloaltonetworks/pan-os-codegen/pkg/generate"
"github.com/paloaltonetworks/pan-os-codegen/pkg/load"
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"log"
)

type Command struct {
ctx context.Context
args []string
specs []string
config string
commandType properties.CommandType
templatePath string
}
Expand Down Expand Up @@ -42,7 +42,7 @@ func (c *Command) Setup() error {
if c.specs == nil {
c.specs, err = properties.GetNormalizations()
if err != nil {
return fmt.Errorf("error getting normalizations: %s", err)
return fmt.Errorf("error getting normalizations: %w", err)
}
}
return nil
Expand All @@ -58,12 +58,12 @@ func (c *Command) Execute() error {

content, err := load.File(configPath)
if err != nil {
return fmt.Errorf("error loading %s - %s", configPath, err)
return fmt.Errorf("error loading %s: %w", configPath, err)
}

config, err := properties.ParseConfig(content)
if err != nil {
return fmt.Errorf("error parsing %s - %s", configPath, err)
return fmt.Errorf("error parsing %s: %w", configPath, err)
}
var resourceList []string
var dataSourceList []string
Expand All @@ -72,16 +72,16 @@ func (c *Command) Execute() error {
log.Printf("Parsing %s...\n", specPath)
content, err := load.File(specPath)
if err != nil {
return fmt.Errorf("error loading %s - %s", specPath, err)
return fmt.Errorf("error loading %s: %w", specPath, err)
}

spec, err := properties.ParseSpec(content)
if err != nil {
return fmt.Errorf("error parsing %s - %s", specPath, err)
return fmt.Errorf("error parsing %s: %w", specPath, err)
}

if err = spec.Sanity(); err != nil {
return fmt.Errorf("%s sanity failed: %s", specPath, err)
return fmt.Errorf("%s sanity failed: %w", specPath, err)
}

if c.commandType == properties.CommandTypeTerraform {
Expand All @@ -90,7 +90,7 @@ func (c *Command) Execute() error {
terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
err = terraformGenerator.RenderTerraformProviderFile(newProviderObject, spec)
if err != nil {
return fmt.Errorf("error generating Terraform provider - %s", err)
return fmt.Errorf("error generating Terraform provider: %w", err)
}

resourceList = append(resourceList, newProviderObject.Resources...)
Expand All @@ -99,7 +99,7 @@ func (c *Command) Execute() error {
} else if c.commandType == properties.CommandTypeSDK {
generator := generate.NewCreator(config.Output.GoSdk, c.templatePath, spec)
if err = generator.RenderTemplate(); err != nil {
return fmt.Errorf("error rendering %s - %s", specPath, err)
return fmt.Errorf("error rendering %s: %w", specPath, err)
}
}

Expand All @@ -115,10 +115,13 @@ func (c *Command) Execute() error {

terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, providerSpec)
err = terraformGenerator.RenderTerraformProvider(newProviderObject, providerSpec, config.TerraformProviderConfig)
if err != nil {
return err
}
}

if err = generate.CopyAssets(config, c.commandType); err != nil {
return fmt.Errorf("error copying assets %s", err)
return fmt.Errorf("error copying assets %w", err)
}

log.Println("Generation complete.")
Expand Down
3 changes: 2 additions & 1 deletion pkg/content/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package content
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"strings"

"gopkg.in/yaml.v3"
)

func Unmarshal(v []byte, o interface{}) error {
Expand Down
3 changes: 2 additions & 1 deletion pkg/generate/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"strings"
"testing"

"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
)

func TestCreateFullFilePath(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/naming/names_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package naming

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCamelCase(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/properties/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package properties

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfig(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/translate/assignments_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package translate

import (
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"

"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
)

func TestSpecifyEntryAssignmentForFlatStructure(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/translate/funcs_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package translate

import (
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"

"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
)

func TestGenerateEntryXpath(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/translate/imports_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package translate

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRenderImports(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/translate/names_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package translate

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestPackageName(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/translate/structs_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package translate

import (
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"

"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
)

const sampleSpec = `name: 'Address'
Expand Down
5 changes: 3 additions & 2 deletions pkg/translate/terraform_provider/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package terraform_provider

import (
"fmt"
"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"log"
"strings"
"text/template"

"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
)

// Package-level function map to avoid repetition in each function
Expand Down
8 changes: 5 additions & 3 deletions pkg/translate/terraform_provider/struct_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package terraform_provider_test

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"github.com/paloaltonetworks/pan-os-codegen/pkg/translate/terraform_provider"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)

func TestParamToModel(t *testing.T) {
Expand Down
Loading