Skip to content
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

Add linting step to CI #77

Merged
merged 12 commits into from
Nov 4, 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
25 changes: 25 additions & 0 deletions .github/actions/lint-terraform-svchost/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

name: Lint
description: Lints terraform-svchost
runs:
using: composite
steps:
- name: Setup Go Environment
uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
with:
go-version-file: "go.mod"
cache: true

- run: make fmtcheck
shell: bash

- name: Install golangci-lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/0b5709648c8ba9780e821faf16c5c2bb3262ce3e/install.sh | sh -s -- -b $(go env GOPATH)/bin $GOLANGCILINT_VERSION
shell: bash
env:
GOLANGCILINT_VERSION: v1.55.2

- run: make lint
shell: bash
11 changes: 9 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ on:
- main

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- uses: ./.github/actions/lint-terraform-svchost

copywrite:
name: Run Header Copyright
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1

- name: Install Copywrite
id: install
Expand All @@ -31,7 +38,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1

- name: Set up Go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
Expand Down
62 changes: 62 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

run:
timeout: 5m
linters:
# This set of linters are enabled by default: deadcode, errcheck, gosimple, govet, ineffasign, staticcheck, struccheck, typecheck, unused, varcheck
enable:
# List of all linters: https://golangci-lint.run/usage/linters/
- whitespace #https://github.com/ultraware/whitespace
# - noctx #https://github.com/sonatard/noctx
- nilerr #https://github.com/gostaticanalysis/nilerr
- nestif #https://github.com/nakabonne/nestif
- exportloopref #https://github.com/kyoh86/exportloopref
- bodyclose #https://github.com/timakin/bodyclose
- goconst #https://github.com/jgautheron/goconst
- gofmt
- errcheck #https://github.com/kisielk/errcheck
- stylecheck #https://github.com/dominikh/go-tools/tree/master/stylecheck
- revive #golint is deprecated and golangci-lint recommends to use revive instead https://github.com/mgechev/revive
#other deprecated lint libraries: maligned, scopelint, interfacer
- gocritic #https://github.com/go-critic/go-critic
- unparam #https://github.com/mvdan/unparam
- misspell #https://github.com/client9/misspell
issues:
exclude-rules:
- path: _test\.go
linters:
- unused
- deadcode
- unparam
- errcheck
linters-settings:
errcheck:
# https://github.com/kisielk/errcheck#excluding-functions
check-type-assertions: true
check-blank: true
goconst:
min-len: 20
min-occurrences: 5
ignore-calls: false
ignore-tests: true
gocritic:
enabled-tags:
- diagnostic
- opinionated
- performance
disabled-checks:
- unnamedResult
- hugeParam
- singleCaseSwitch
- ifElseChain
- sloppyTestFuncName
revive:
# see https://github.com/mgechev/revive#available-rules for details.
ignore-generated-header: false #recommended in their configuration
severity: warning
rules:
- name: indent-error-flow #Prevents redundant else statements
severity: warning
- name: useless-break
severity: warning
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
TEST?=$$(go list ./... |grep -v 'vendor')
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
PKG_NAME=tfe

fmt:
gofmt -w $(GOFMT_FILES)

lint:
@golangci-lint run ; if [ $$? -ne 0 ]; then \
echo ""; \
echo "golangci-lint found some code style issues."; \
exit 1; \
fi

fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"

.PHONY: fmt lint fmtcheck

12 changes: 4 additions & 8 deletions auth/helper_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"os/exec"
"path/filepath"

svchost "github.com/hashicorp/terraform-svchost"
ctyjson "github.com/zclconf/go-cty/cty/json"

"github.com/hashicorp/terraform-svchost"
)

type helperProgramCredentialsSource struct {
Expand Down Expand Up @@ -49,8 +48,7 @@ func HelperProgramCredentialsSource(executable string, args ...string) Credentia
func (s *helperProgramCredentialsSource) ForHost(host svchost.Hostname) (HostCredentials, error) {
args := make([]string, len(s.args), len(s.args)+2)
copy(args, s.args)
args = append(args, "get")
args = append(args, string(host))
args = append(args, "get", string(host))

outBuf := bytes.Buffer{}
errBuf := bytes.Buffer{}
Expand Down Expand Up @@ -86,8 +84,7 @@ func (s *helperProgramCredentialsSource) ForHost(host svchost.Hostname) (HostCre
func (s *helperProgramCredentialsSource) StoreForHost(host svchost.Hostname, credentials HostCredentialsWritable) error {
args := make([]string, len(s.args), len(s.args)+2)
copy(args, s.args)
args = append(args, "store")
args = append(args, string(host))
args = append(args, "store", string(host))

toStore := credentials.ToStore()
toStoreRaw, err := ctyjson.Marshal(toStore, toStore.Type())
Expand Down Expand Up @@ -123,8 +120,7 @@ func (s *helperProgramCredentialsSource) StoreForHost(host svchost.Hostname, cre
func (s *helperProgramCredentialsSource) ForgetForHost(host svchost.Hostname) error {
args := make([]string, len(s.args), len(s.args)+2)
copy(args, s.args)
args = append(args, "forget")
args = append(args, string(host))
args = append(args, "forget", string(host))

errBuf := bytes.Buffer{}

Expand Down
4 changes: 2 additions & 2 deletions auth/helper_program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"testing"

"github.com/hashicorp/terraform-svchost"
svchost "github.com/hashicorp/terraform-svchost"
)

func TestHelperProgramCredentialsSource(t *testing.T) {
Expand All @@ -16,7 +16,7 @@ func TestHelperProgramCredentialsSource(t *testing.T) {
t.Fatal(err)
}

program := filepath.Join(wd, "testdata/test-helper")
program := filepath.Join(wd, "testdata", "test-helper")
t.Logf("testing with helper at %s", program)

src := HelperProgramCredentialsSource(program)
Expand Down
4 changes: 2 additions & 2 deletions auth/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package auth
import (
"testing"

"github.com/hashicorp/terraform-svchost"
svchost "github.com/hashicorp/terraform-svchost"
)

func TestStaticCredentialsSource(t *testing.T) {
src := StaticCredentialsSource(map[svchost.Hostname]map[string]interface{}{
svchost.Hostname("example.com"): map[string]interface{}{
svchost.Hostname("example.com"): {
"token": "abc123",
},
})
Expand Down
6 changes: 3 additions & 3 deletions disco/disco.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
)

// httpTransport is overridden during tests, to skip TLS verification.
var httpTransport = defaultHttpTransport()
var httpTransport = defaultHTTPTransport()

// Disco is the main type in this package, which allows discovery on given
// hostnames and caches the results by hostname to avoid repeated requests
Expand All @@ -62,8 +62,8 @@ type ErrServiceDiscoveryNetworkRequest struct {
}

func (e ErrServiceDiscoveryNetworkRequest) Error() string {
wrapped_error := fmt.Errorf("failed to request discovery document: %w", e.err)
return wrapped_error.Error()
wrappedError := fmt.Errorf("failed to request discovery document: %w", e.err)
return wrappedError.Error()
}

// New returns a new initialized discovery object.
Expand Down
Loading
Loading