Skip to content

Commit

Permalink
chore: add Makefile/github workflow/lint
Browse files Browse the repository at this point in the history
  • Loading branch information
coanor committed Jul 23, 2023
1 parent 44c6728 commit a06f845
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 7 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
on:
release:
types: [created]

permissions:
contents: write
packages: write

jobs:
releases-matrix:
name: Release mdcheck Binary
runs-on: ubuntu-latest
strategy:
matrix:
# build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64
goos: [linux, windows, darwin]
goarch: ["386", amd64, arm64]
exclude:
- goarch: "386"
goos: darwin
- goarch: arm64
goos: windows
steps:
- uses: actions/checkout@v3
- uses: wangyoucao577/go-release-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "1.18"
binary_name: "mdcheck"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mdcheck
dist/
155 changes: 155 additions & 0 deletions .golangci.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
[run]
timeout = "10m"
issues-exit-code = 1
skip-files = ["packrd"]
go = "1.18"
skip-dirs = [
# TODO: we should add lint check on the package
"internal/obfuscate",
]

[linters-settings]

[linters-settings.govet]
check-shadowing = false

[linters-settings.loggercheck]
zap = true

[linters-settings.golint]
min-confidence = 0.0

# See https://github.com/uudashr/gocognit
[linters-settings.gocognit]
min-complexity = 50.0

[linters-settings.cyclo]
min-complexity = 20.0

[linters-settings.maligned]
suggest-new = true

[linters-settings.goconst]
min-len = 3.0
min-occurrences = 4.0

[linters-settings.misspell]
locale = "US"

[linters-settings.funlen]
lines = 230 # default 60
statements = 150 # default 40

[linters-settings.forbidigo]
forbid = ['^print(ln)?$', '^spew\.Print(f|ln)?$', '^spew\.Dump$']

[linters-settings.depguard]
list-type = "blacklist"
include-go-root = false
packages = ["github.com/pkg/errors"]

[linters-settings.godox]
keywords = ["FIXME"]

[linters-settings.wsl]
allow-assign-and-anything = true

[linters-settings.importas]
corev1 = "k8s.io/api/core/v1"
networkingv1beta1 = "k8s.io/api/networking/v1beta1"
extensionsv1beta1 = "k8s.io/api/extensions/v1beta1"
metav1 = "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeerror = "k8s.io/apimachinery/pkg/api/errors"

[linters-settings.gomoddirectives]
replace-allow-list = [
"github.com/abbot/go-http-auth",
"github.com/go-check/check",
"github.com/gorilla/mux",
"github.com/mailgun/minheap",
"github.com/mailgun/multibuf",
]

[linters-settings.lll]
line-length = 150
tab-width = 2

[linters-settings.staticcheck]
checks = ["all", "-SA1019"] # disable `Deprecated warnings`

[linters]
enable-all = true
disable = [
# 权且放开他们
"testpackage", # Too strict
"wrapcheck", # 不便于错误处理
"tagliatelle", # 跟现有 json tag 命名方式冲突
"paralleltest", # 可开启,改动范围较大
"noctx", # 要求 HTTP 请求都用 context 形式,改动较大
"nlreturn", # 要求 return 语句前有一个空行
"wsl", # 更好代码分段
"gomnd", # 不放过任何一个魔术数
"prealloc", # Too many false-positive.
"nestif", # Too many false-positive.
"goerr113", # 不能 fmt.Errorf/errors.New
"gochecknoglobals", # 不能搞全局变量
"exhaustivestruct", # 结构体初始化字段是否完整
"golint", # Too strict
"scopelint", # obsoluted: https://github.com/kyoh86/scopelint#obsoleted

# 以下俩个 link 检查代码(函数)复杂度
"gocognit",
"gocyclo",

"dupl", # 还不允许有相似代码
"maligned", # deprecated: https://github.com/mdempsky/maligned

# go1.18 disabled
"bodyclose", "nilerr", "rowserrcheck", "sqlclosecheck", "structcheck", "tparallel", "unparam", "wastedassign",

"cyclop",
"gomoddirectives", # used `replace' in go.mod
"nolintlint",
"revive",

#############################3
# go1.18 disabled
#############################3
"bodyclose", "nilerr", "rowserrcheck", "sqlclosecheck", "structcheck", "tparallel", "unparam", "wastedassign", "interfacer",
"exhaustruct", # [升级 Go1.18 后加入] 要求结构体每次务必每个字段都要填
"varnamelen", # [升级 Go1.18 后加入] 变量名长度检查
"nonamedreturns", # [升级 Go1.18 后加入] 不允许函数有名返回
"forcetypeassert", # [升级 Go1.18 后加入] 强制断言检查
"gci", # [升级 Go1.18 后加入] Tab 键检查
"maintidx", # [升级 Go1.18 后加入] 函数长度检查
"containedctx", # [升级 Go1.18 后加入] Go 不推荐把 context 放在结构体中:https://go.dev/blog/context-and-structs
"ireturn", # [升级 Go1.18 后加入] 函数 interface 返回检查
"contextcheck", # [升级 Go1.18 后加入] 函数参数必须传入 context
"errchkjson", # [升级 Go1.18 后加入] json 序列化/反序列化必须检查
"nilnil", # [升级 Go1.18 后加入] 函数返回 nil, nil 检查
]

[issues]
exclude-use-default = false
max-per-linter = 0
max-same-issues = 0
exclude = []

[[issues.exclude-rules]]
path = "(.+)_test.go"
linters = [
"errcheck",
"goconst",
"funlen",
"godot",
"lll",
"gosec",
"stylecheck",
"wsl",
"unused",
"deadcode",
"unparam",
"varcheck",
"gocognit",
"gocyclo",
]
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: default

default:
GOOS=darwin GOARCH=arm64 go build -o dist/mdcheck-darwin-arm64 -ldflags "-w -s" main.go
GOOS=linux GOARCH=arm64 go build -o dist/mdcheck-linux-arm64 -ldflags "-w -s" main.go
GOOS=linux GOARCH=amd64 go build -o dist/mdcheck-linux-amd64 -ldflags "-w -s" main.go
GOOS=windows GOARCH=amd64 go build -o dist/mdcheck-windows-amd64.exe -ldflags "-w -s" main.go
GOOS=windows GOARCH=arm64 go build -o dist/mdcheck-windows-arm64.exe -ldflags "-w -s" main.go

lint:
golangci-lint run --fix --allow-parallel-runners;
1 change: 0 additions & 1 deletion check/md_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ func getLineNumber(from, sub []byte) int {
}

func doMatch(md string, autofix bool) (res []*CheckResult, fix string) {

d, err := ioutil.ReadFile(filepath.Clean(md))
if err != nil {
log.Printf("[E] ReadFile: %s", err)
Expand Down
2 changes: 0 additions & 2 deletions check/md_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
// path: 'dir/to/monitor-json'
// ---.
func checkMarkdownMeta(file string, metaDataDir string) []*CheckResult {

source, err := ioutil.ReadFile(filepath.Clean(file))
if err != nil {
log.Printf("[E] ReadFile: %s", err)
Expand Down Expand Up @@ -68,7 +67,6 @@ func checkMarkdownMeta(file string, metaDataDir string) []*CheckResult {
}

func checkMeta(md string, meta map[string]interface{}, metaDataDir string) (res []*CheckResult) {

for _, k := range requiredKeys {
if _, ok := meta[k]; !ok {
res = append(res, &CheckResult{
Expand Down
5 changes: 3 additions & 2 deletions check/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.

// Package check implements markdown basic checkings.
package check

type option func(*checkOption)
Expand All @@ -15,7 +16,7 @@ type checkOption struct {
// WithMarkdownDir set markdown path to checking.
func WithMarkdownDir(dir string) option {
return func(o *checkOption) {
if len(dir) >= 0 {
if len(dir) > 0 {
o.mddir = dir
}
}
Expand All @@ -24,7 +25,7 @@ func WithMarkdownDir(dir string) option {
// WithMetaDir set markdown meta data path for checking.
func WithMetaDir(dir string) option {
return func(o *checkOption) {
if len(dir) >= 0 {
if len(dir) > 0 {
o.metadir = dir
}
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
metaDir string
)

//nolint:gochecknoinits
func init() {
flag.StringVar(&markdownDir, "md-dir", "", "markdown dirs")
flag.StringVar(&metaDir, "meta-dir", "", "markdown meta dir, with meta dir set, only checking meta info of markdown")
Expand All @@ -34,7 +35,6 @@ func main() {
check.WithMetaDir(metaDir),
check.WithAutofix(autofix),
)

if err != nil {
log.Printf("[E] %s", err.Error())
return
Expand Down

0 comments on commit a06f845

Please sign in to comment.