From 167a0758cbb8045da86200bb21823927204c5a86 Mon Sep 17 00:00:00 2001 From: su chen Date: Wed, 30 Aug 2023 21:08:22 +0800 Subject: [PATCH] add golang ci lint (#3) * add golang ci lint * update based on github action comments --- .github/workflows/code.yml | 8 +++++++- array.go | 2 +- cmd/generate.go | 36 ++++++++++++++++++++++-------------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/.github/workflows/code.yml b/.github/workflows/code.yml index bbdbc5d..27b5a86 100644 --- a/.github/workflows/code.yml +++ b/.github/workflows/code.yml @@ -24,4 +24,10 @@ jobs: run: test -z $(gofmt -l .) || (gofmt -l . && exit 1) - name: Spelling Check - uses: reviewdog/action-misspell@v1.13.1 \ No newline at end of file + uses: reviewdog/action-misspell@v1.13.1 + + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 + args: --timeout=30m diff --git a/array.go b/array.go index 52d9477..14ffe1f 100644 --- a/array.go +++ b/array.go @@ -1,4 +1,4 @@ -// gfn is a Golang library that leverages generics to provide various methods. +// Package gfn is a Golang library that leverages generics to provide various methods. package gfn import "math/rand" diff --git a/cmd/generate.go b/cmd/generate.go index 3009965..05312b4 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -1,3 +1,4 @@ +// Package main is used to generate README.md package main import ( @@ -44,7 +45,10 @@ func main() { panic(err) } readmeStr := strings.Replace(strings.Replace(string(readmeTmpl), "{{ TOC }}", toc, 1), "{{ CONTENT }}", content, 1) - os.WriteFile(filepath.Join(wd, readmeFile), []byte(readmeStr), 0644) + err = os.WriteFile(filepath.Join(wd, readmeFile), []byte(readmeStr), 0644) + if err != nil { + panic(err) + } } const tocTemplate = ` @@ -84,24 +88,28 @@ func (c *category) toc() string { "toLower": strings.ToLower, } tmlp := template.Must(template.New("toc").Funcs(funcs).Parse(tocTemplate)) - tmlp.Execute(&sb, c) + if err := tmlp.Execute(&sb, c); err != nil { + panic(err) + } return strings.TrimSpace(sb.String()) + "\n" } func (c *category) content() string { sb := strings.Builder{} tmpl := template.Must(template.New("function").Parse(strings.ReplaceAll(contentTemplate, ";;;", "```"))) - tmpl.Execute(&sb, c) + if err := tmpl.Execute(&sb, c); err != nil { + panic(err) + } return sb.String() } type fnState int const ( - state_start fnState = iota - state_comment - state_finish - state_abort + stateStart fnState = iota + stateComment + stateFinish + stateAbort ) type function struct { @@ -128,8 +136,8 @@ func (f *function) TOC() string { } func (f *function) addComment(line string) { - if f.state == state_start { - f.state = state_comment + if f.state == stateStart { + f.state = stateComment line = strings.TrimPrefix(line, "//") line = strings.TrimSpace(line) words := strings.SplitN(line, " ", 3) @@ -141,7 +149,7 @@ func (f *function) addComment(line string) { } f.Comment = line - } else if f.state == state_comment { + } else if f.state == stateComment { line = strings.TrimPrefix(line, "//") line = strings.TrimSpace(line) f.Comment += " " + line @@ -153,20 +161,20 @@ func (f *function) addSignature(line string) { line = strings.TrimSpace(line) nameFirstChar := string(strings.TrimPrefix(line, "func ")[0]) if nameFirstChar != strings.ToUpper(nameFirstChar) { - f.state = state_abort + f.state = stateAbort return } line = strings.TrimRight(line, "{") f.Signature = line - f.state = state_finish + f.state = stateFinish } func (f *function) finish() bool { - return f.state == state_finish + return f.state == stateFinish } func (f *function) abort() bool { - return f.state == state_abort + return f.state == stateAbort } func processCategory(name, filePath string) (*category, error) {