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

refactor: support golangci-lint #130

Merged
merged 3 commits into from
Aug 21, 2023
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
41 changes: 27 additions & 14 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
name: CI

on: [push, pull_request]
on: [
push,
pull_request
]

env:
GO_VERSION: "1.21"

permissions:
contents: read

jobs:
typos-check:
Expand All @@ -11,8 +20,9 @@ jobs:
uses: actions/checkout@v3
- name: Check spelling with custom config file
uses: crate-ci/[email protected]
linter:
name: linter

lint:
name: lint
runs-on: ubuntu-latest
steps:
- name: Checkout sources
Expand All @@ -21,29 +31,32 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: "1.18.4"
go-version: ${{ env.GO_VERSION }}

- name: golangci-lint
uses: golangci/golangci-lint-action@v3.3.0
uses: golangci/golangci-lint-action@v3
with:
version: v1.50.0
args: --timeout 10m0s
version: v1.54
# '-v' flag is required to show the output of golangci-lint.
args: -v

unit-test:
name: coverage-test
runs-on: ubuntu-latest
needs: [ linter ]
needs: [ lint ]
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: "1.18.4"
go-version: ${{ env.GO_VERSION }}

- name: Unit test
run: |
make coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
with:
Expand All @@ -52,19 +65,19 @@ jobs:
files: ./coverage.xml
name: codecov-gtctl
verbose: true
e2e-test:
name: e2e-test
e2e:
name: e2e
runs-on: ubuntu-latest
needs: [ linter ]
needs: [ lint ]
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: "1.18.4"
go-version: ${{ env.GO_VERSION }}

- name: e2e test of basic cluster
- name: Run e2e
run: |
make e2e
19 changes: 14 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
name: Release

on:
release:
types: [created]
push:
tags:
- "v*"

env:
GO_VERSION: "1.21"

jobs:
build:
Expand Down Expand Up @@ -35,7 +39,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: "1.18.4"
go-version: ${{ env.GO_VERSION }}

- name: Build project
run: |
Expand Down Expand Up @@ -76,8 +80,13 @@ jobs:
uses: actions/download-artifact@v3

- name: Publish release
uses: softprops/action-gh-release@v1
uses: ncipollo/release-action@v1
with:
name: "Release ${{ github.ref_name }}"
files: |
prerelease: false
make_release: true
generateReleaseNotes: true
allowUpdates: true
tag: ${{ github.ref_name }}
artifacts: |
**/gtctl-*
95 changes: 95 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Options for analysis running.
run:
# Timeout for analysis, e.g. 30s, 5m.
# Default: 1m
timeout: 10m

# The default concurrency value is the number of available CPU.
concurrency: 4

# Which dirs to skip: issues from them won't be reported.
# Can use regexp here: `generated.*`, regexp is applied on full path,
# including the path prefix if one is set.
# Default value is empty list,
# but default dirs are skipped independently of this option's value (see skip-dirs-use-default).
# "/" will be replaced by current OS file path separator to properly work on Windows.
skip-dirs:
- bin
- docs
- examples
- hack

# output configuration options.
output:
# Format: colored-line-number|line-number|json|colored-tab|tab|checkstyle|code-climate|junit-xml|github-actions|teamcity
#
# Multiple can be specified by separating them by comma, output can be provided
# for each of them by separating format name and path by colon symbol.
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Example: "checkstyle:report.xml,json:stdout,colored-line-number"
#
# Default: colored-line-number
format: colored-line-number

# Print lines of code with issue.
# Default: true
print-issued-lines: true

# Print linter name in the end of issue text.
# Default: true
print-linter-lines: true

linters:
# Disable all linters.
disable-all: true

# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default
enable:
# Errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases.
- errcheck

# Linter for Go source code that specializes in simplifying code.
- gosimple

# Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string.
- govet

# Detects when assignments to existing variables are not used.
- ineffassign

# It's a set of rules from staticcheck. It's not the same thing as the staticcheck binary.
# The author of staticcheck doesn't support or approve the use of staticcheck as a library inside golangci-lint.
- staticcheck

# Check import statements are formatted according to the 'goimport' command. Reformat imports in autofix mode.
- goimports

# Checks whether HTTP response body is closed successfully.
- bodyclose

# Provides diagnostics that check for bugs, performance and style issues.
# Extensible without recompilation through dynamic rules.
# Dynamic rules are written declaratively with AST patterns, filters, report message and optional suggestion.
- gocritic

# Gofmt checks whether code was gofmt-ed. By default, this tool runs with -s option to check for code simplification.
- gofmt

# Finds repeated strings that could be replaced by a constant.
- goconst

# Finds commonly misspelled English words in comments.
- misspell

# Finds naked returns in functions greater than a specified function length.
- nakedret

linters-settings:
goimports:
# A comma-separated list of prefixes, which, if set, checks import paths
# with the given prefixes are grouped after 3rd-party packages.
# Default: ""
local-prefixes: github.com/GreptimeTeam/gtctl
linters-settings:
min-occurrences: 3
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
CLUSTER=e2e-cluster
LDFLAGS = $(shell ./hack/version.sh)

##@ Build

.PHONY: gtctl
gtctl: ## Build gtctl.
@go build -ldflags '${LDFLAGS}' -o bin/gtctl ./cmd/gtctl

##@ Development

.PHONY: setup-e2e
setup-e2e: ## Setup e2e test environment.
./hack/e2e/setup-e2e-env.sh
Expand All @@ -27,6 +31,10 @@ setup-e2e: ## Setup e2e test environment.
e2e: gtctl setup-e2e ## Run e2e.
go test -timeout 8m -v ./tests/e2e/... && kind delete clusters ${CLUSTER}

.PHONY: lint
lint: golangci-lint gtctl ## Run lint.
golangci-lint run -v ./...

.PHONY: test
test: ## Run unit test.
go test -timeout 1m -v ./pkg/...
Expand All @@ -39,10 +47,16 @@ coverage: ## Run unit test with coverage.
fix-license-header: license-eye ## Fix license header.
license-eye -c .licenserc.yaml header fix

##@ Tools Installation

.PHONY: license-eye
license-eye: ## Install license-eye.
@which license-eye || go install github.com/apache/skywalking-eyes/cmd/license-eye@latest

.PHONY: golangci-lint
golangci-lint: ## Install golangci-lint.
@which golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.1

##@ General

# The help target prints out all targets with their descriptions organized
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gtctl/cluster/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"context"
"fmt"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl/cluster/connect/mysql"
"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl/cluster/connect/pg"
"github.com/GreptimeTeam/gtctl/pkg/deployer/k8s"
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/gtctl/cluster/connect/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"sync"
"syscall"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/go-sql-driver/mysql"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/GreptimeTeam/gtctl/pkg/logger"
)

Expand Down Expand Up @@ -58,7 +58,7 @@ func connect(port, clusterName string, l logger.Logger) error {
go func() {
defer waitGroup.Done()
if err = cmd.Wait(); err != nil {
//exit status 1
// exit status 1
exitError, ok := err.(*exec.ExitError)
if !ok {
l.Errorf("Error waiting for port-forwarding to finish: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/gtctl/cluster/connect/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"sync"
"syscall"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/go-pg/pg/v10"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/GreptimeTeam/gtctl/pkg/logger"
)

Expand Down Expand Up @@ -65,7 +65,7 @@ func connect(port, clusterName string, l logger.Logger) error {
go func() {
defer waitGroup.Done()
if err = cmd.Wait(); err != nil {
//exit status 1
// exit status 1
exitError, ok := err.(*exec.ExitError)
if !ok {
l.Errorf("Error waiting for port-forwarding to finish: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gtctl/cluster/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
"fmt"
"strings"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/GreptimeTeam/gtctl/pkg/cmd/gtctl/cluster/common"
"github.com/GreptimeTeam/gtctl/pkg/deployer/k8s"
"github.com/GreptimeTeam/gtctl/pkg/logger"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gtctl/cluster/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"context"
"fmt"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/GreptimeTeam/gtctl/pkg/deployer/k8s"
"github.com/GreptimeTeam/gtctl/pkg/logger"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gtctl/cluster/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"context"
"fmt"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/GreptimeTeam/gtctl/pkg/deployer/k8s"
"github.com/GreptimeTeam/gtctl/pkg/logger"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gtctl/cluster/scale/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"context"
"fmt"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/GreptimeTeam/gtctl/pkg/deployer"
"github.com/GreptimeTeam/gtctl/pkg/deployer/k8s"
"github.com/GreptimeTeam/gtctl/pkg/logger"
Expand Down
16 changes: 8 additions & 8 deletions pkg/cmd/gtctl/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

package constants

// GtctlTextBanner is the following text banner for gtctl.
// __ __ __
// ____ _/ /______/ /_/ /
// / __ `/ __/ ___/ __/ /
// / /_/ / /_/ /__/ /_/ /
// \__, /\__/\___/\__/_/
// /____/
const GtctlTextBanner = " __ __ __\n ____ _/ /______/ /_/ /\n / __ `/ __/ ___/ __/ / \n / /_/ / /_/ /__/ /_/ / \n \\__, /\\__/\\___/\\__/_/ \n/____/ \n"
// GtctlTextBanner represents the ASCII art banner for the gtctl command-line tool.
const GtctlTextBanner = `
__ __ __
____ _/ /______/ /_/ /
/ __ '/ __/ ___/ __/ /
/ /_/ / /_/ /__/ /_/ /
\__, /\__/\___/\__/_/
/____/`
Loading
Loading