Skip to content

Commit

Permalink
CLOUDP-236398: Adding golangci and code-health action (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo authored Mar 8, 2024
1 parent e1e42ca commit d372e28
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 16 deletions.
17 changes: 17 additions & 0 deletions .github/actionlint-matcher.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"problemMatcher": [
{
"owner": "actionlint",
"pattern": [
{
"regexp": "^(?:\\x1b\\[\\d+m)?(.+?)(?:\\x1b\\[\\d+m)*:(?:\\x1b\\[\\d+m)*(\\d+)(?:\\x1b\\[\\d+m)*:(?:\\x1b\\[\\d+m)*(\\d+)(?:\\x1b\\[\\d+m)*: (?:\\x1b\\[\\d+m)*(.+?)(?:\\x1b\\[\\d+m)* \\[(.+?)\\]$",
"file": 1,
"line": 2,
"column": 3,
"message": 4,
"code": 5
}
]
}
]
}
60 changes: 60 additions & 0 deletions .github/workflows/code-health-tools.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: 'Code Health Tools'
on:
push:
branches:
- main
paths:
- 'tools/**'
pull_request: {}
workflow_dispatch: {}

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout CLI
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- name: Install Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491
with:
go-version-file: 'tools/cli/go.mod'
- name: Build CLI
run: |
pushd tools/cli
make build
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
with:
sparse-checkout: |
.github
tools
- name: Install Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491
with:
go-version-file: 'tools/cli/go.mod'
cache: false # see https://github.com/golangci/golangci-lint-action/issues/807
- name: golangci-lint
uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804
with:
version: v1.56.2
working-directory: tools/cli
- name: Checkout GitHub actions
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
with:
sparse-checkout: |
.github
- name: Download actionlint
id: get_actionlint
run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash)
shell: bash
- name: Check workflow files
run: |
echo "::add-matcher::.github/actionlint-matcher.json"
${{ steps.get_actionlint.outputs.executable }} -color
shell: bash
116 changes: 116 additions & 0 deletions tools/cli/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
linters-settings:
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
govet:
check-shadowing: true
enable:
- fieldalignment

revive:
# see https://github.com/mgechev/revive#available-rules for details.
ignore-generated-header: true
severity: warning
rules:
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: errorf
- name: exported
- name: indent-error-flow
- name: if-return
- name: increment-decrement
- name: var-naming
- name: var-declaration
- name: package-comments
- name: range
- name: receiver-naming
- name: time-naming
- name: unexported-return
- name: indent-error-flow
- name: errorf
- name: empty-block
- name: superfluous-else
- name: struct-tag
- name: unused-parameter
- name: unreachable-code
- name: redefines-builtin-id
misspell:
locale: US
lll:
line-length: 120
nestif:
# minimal complexity of if statements to report, 5 by default
min-complexity: 7
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks: [case, return]
funlen:
lines: 360
statements: 120
linters:
disable-all: true
enable:
- dogsled
- errcheck
- funlen
- gocritic
- gofmt
- goimports
- revive
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- nolintlint
- rowserrcheck
- exportloopref
- staticcheck
- stylecheck
- typecheck
- unconvert
- unused
- whitespace
- thelper
- testifylint
- exhaustive
- makezero
- noctx
- tenv
- prealloc
- predeclared
- whitespace

# don't enable:
# - deadcode
# - varcheck
# - structcheck
# - depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
# - gocyclo # we already have funlen lint
# - dupl # we have a lot of duplicate test cases
# - gochecknoinits # we need the init function for the provider
# - gochecknoglobals # we need some global variables
# - unparam # Forces to create global variables when one variable is repeated in different functions
# - goerr113 # It does not allow you to return an error, you need to save the error in a variable to do it
# - goconst
# - gocognit

run:
timeout: 10m
tests: true
modules-download-mode: readonly
15 changes: 14 additions & 1 deletion tools/cli/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html

GOLANGCI_VERSION=v1.56.2
SOURCE_FILES?=./cmd
BINARY_NAME=openapicli
VERSION=v0.0.1
Expand All @@ -20,8 +21,13 @@ deps: ## Download go module dependencies
go mod download
go mod tidy

.PHONY: devtools
devtools: ## Install dev tools
@echo "==> Installing dev tools..."
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin $(GOLANGCI_VERSION)

.PHONY: setup
setup: deps ## Set up dev env
setup: deps devtools ## Set up dev env

.PHONY: fmt
fmt: ### Format all go files with goimports and gofmt
Expand All @@ -39,6 +45,13 @@ build-debug:
@echo "==> Building openapicli binary for debugging"
go build -gcflags="$(DEBUG_FLAGS)" -ldflags "$(LINKER_FLAGS)" -o $(DESTINATION) $(SOURCE_FILES)

.PHONY: lint
lint: ## Run linter
golangci-lint run

.PHONY: fix-lint
fix-lint: ## Fix linting errors
golangci-lint run --fix

.PHONY: list
list: ## List all make targets
Expand Down
20 changes: 5 additions & 15 deletions tools/cli/internal/cli/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,15 @@ import (
)

type Opts struct {
Base *load.SpecInfo
outputPath string
Base *load.SpecInfo
}

func (o *Opts) Run(args []string) error {
func (o *Opts) Run(_ []string) error {
// To add in follow up PR: CLOUDP-225849
return nil
}

func (o *Opts) removeExternalReferences(paths []string, federated *load.SpecInfo) ([]byte, error) {
// To add in follow up PR: CLOUDP-225849
return nil, nil

}
func (o *Opts) saveFile(data []byte) error {
return nil
}

func (o *Opts) PreRunE(args []string) error {
func (o *Opts) PreRunE(_ []string) error {
// To Add in follow up PR: CLOUDP-225849
return nil
}
Expand All @@ -50,10 +40,10 @@ func Builder() *cobra.Command {
Use: "merge [base-spec] [spec-1] [spec-2] [spec-3] ... [spec-n]",
Short: "Merge Open API specifications into a base spec.",
Args: cobra.MinimumNArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(_ *cobra.Command, args []string) error {
return opts.PreRunE(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
return opts.Run(args)
},
}
Expand Down

0 comments on commit d372e28

Please sign in to comment.