diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..86616a6 --- /dev/null +++ b/.github/workflows/release.yaml @@ -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" diff --git a/.gitignore b/.gitignore index eef4953..849ddff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -mdcheck +dist/ diff --git a/.golangci.toml b/.golangci.toml new file mode 100644 index 0000000..862cc15 --- /dev/null +++ b/.golangci.toml @@ -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", +] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..05218c9 --- /dev/null +++ b/Makefile @@ -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; diff --git a/check/md_check.go b/check/md_check.go index 219b67b..dfa117e 100644 --- a/check/md_check.go +++ b/check/md_check.go @@ -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) diff --git a/check/md_meta.go b/check/md_meta.go index 70b2449..b0deb7f 100644 --- a/check/md_meta.go +++ b/check/md_meta.go @@ -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) @@ -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{ diff --git a/check/option.go b/check/option.go index 0f1e0c3..6f79bf0 100644 --- a/check/option.go +++ b/check/option.go @@ -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) @@ -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 } } @@ -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 } } diff --git a/main.go b/main.go index b354589..6d86d95 100644 --- a/main.go +++ b/main.go @@ -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") @@ -34,7 +35,6 @@ func main() { check.WithMetaDir(metaDir), check.WithAutofix(autofix), ) - if err != nil { log.Printf("[E] %s", err.Error()) return