Skip to content

Commit

Permalink
Merge pull request #277 from k1LoW/gostyle
Browse files Browse the repository at this point in the history
Update linter setting
  • Loading branch information
k1LoW authored Sep 30, 2023
2 parents 6d92fbe + ec23583 commit 547dfaf
Show file tree
Hide file tree
Showing 52 changed files with 388 additions and 350 deletions.
17 changes: 13 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
curl -sL -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/rate_limit
- name: Check out source code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}

Expand All @@ -32,13 +32,22 @@ jobs:

- name: Run lint
uses: reviewdog/action-golangci-lint@v2
with:
fail_on_error: true

- name: Run govulncheck
uses: golang/govulncheck-action@v1
with:
go-version-input: '${{ steps.setup-go.outputs.go-version }}'
check-latest: true
go-package: ./...
go-version-input: '${{ steps.setup-go.outputs.go-version }}'
check-latest: true
go-package: ./...
fail-on-error: true

- name: Run gostyle
uses: k1LoW/gostyle-action@v1
with:
go-version-input: '${{ steps.setup-go.outputs.go-version }}'
fail-on-error: true

- name: Run tests
run: make ci
Expand Down
7 changes: 1 addition & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ linters:
- gosec
- godot
- revive
- errorlint
linters-settings:
errcheck:
check-type-assertions: true
Expand All @@ -17,9 +18,3 @@ linters-settings:
disabled: true
- name: exported
disabled: false
issues:
# include:
# - EXC0012
# - EXC0014
exclude:
- SA3000
20 changes: 20 additions & 0 deletions .gostyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
analyzers:
disable:
- mixedcaps # disable mixedcaps analyzer. because the underscores analyzer is more detailed.
analyzers-settings:
# dontpanic:
# exclude-test: true # exclude test files (default: false)
# handlerrors:
# exclude-test: true # exclude test files (default: false)
# ifacenames:
# all: true # all interface names with the -er suffix are required (default: false)
# recvnames:
# max: 3 # max length of receiver name (default: 2)
repetition:
exclude:
- NeedToShrink
# varnames:
# small-varname-max: 4 # max length of variable name for small scope (default: -1)
# medium-varname-max: 8 # max length of variable name for medium scope (default: -1)
# large-varname-max: 16 # max length of variable name for large scope (default: -1)
# very-large-varname-max: 32 # max length of variable name for very large scope (default: -1)
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test_no_coverage: build
lint:
golangci-lint run ./...
govulncheck ./...
go vet -vettool=`which gostyle` -gostyle.config=$(PWD)/.gostyle.yml ./...

build:
go build -ldflags="$(BUILD_LDFLAGS)"
Expand All @@ -45,6 +46,7 @@ depsdev:
go install github.com/Songmu/ghch/cmd/ghch@latest
go install github.com/Songmu/gocredits/cmd/gocredits@latest
go install golang.org/x/vuln/cmd/govulncheck@latest
go install github.com/k1LoW/gostyle@latest

prerelease:
git pull origin main --tag
Expand Down
14 changes: 8 additions & 6 deletions central/central.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ func (c *Central) collectReports() error {
}
current, ok := rsMap[r.Repository]
if !ok {
_, _ = fmt.Fprintf(os.Stderr, "Collect report of %s\n", r.Repository)
if _, err := fmt.Fprintf(os.Stderr, "Collect report of %s\n", r.Repository); err != nil {
return err
}
rsMap[r.Repository] = r
return nil
}
Expand Down Expand Up @@ -182,7 +184,7 @@ func (c *Central) generateBadges() ([]string, error) {
badges[bp] = out.Bytes()
}
}
generatedPaths := []string{}
var generatedPaths []string
for _, d := range c.config.Badges {
for path, content := range badges {
if err := d.Put(ctx, path, content); err != nil {
Expand Down Expand Up @@ -222,12 +224,12 @@ func (c *Central) renderIndex(wr io.Writer) error {
query string
)
if !isPrivate {
rootURL, err = g.GetRawRootURL(ctx, repo.Owner, repo.Repo)
rootURL, err = g.FetchRawRootURL(ctx, repo.Owner, repo.Repo)
if err != nil {
return err
}
} else {
b, err := g.GetDefaultBranch(ctx, repo.Owner, repo.Repo)
b, err := g.FetchDefaultBranch(ctx, repo.Owner, repo.Repo)
if err != nil {
return err
}
Expand Down Expand Up @@ -261,7 +263,7 @@ func (c *Central) renderIndex(wr io.Writer) error {
return err
}

d := map[string]interface{}{
d := map[string]any{
"Host": host,
"Reports": c.reports,
"BadgesLinkRel": badgesLinkRel,
Expand All @@ -277,7 +279,7 @@ func (c *Central) renderIndex(wr io.Writer) error {
return nil
}

func funcs() map[string]interface{} {
func funcs() map[string]any {
return template.FuncMap{
"coverage": func(r *report.Report) string {
return fmt.Sprintf("%.1f%%", r.CoveragePercent())
Expand Down
10 changes: 5 additions & 5 deletions central/central_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCollectReports(t *testing.T) {
ctr := New(&Config{
Repository: "owner/repo",
Index: ".",
Wd: c.Getwd(),
Wd: c.Wd(),
Badges: []datastore.Datastore{bd},
Reports: []datastore.Datastore{rd},
CoverageColor: c.CoverageColor,
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestGenerateBadges(t *testing.T) {
ctr := New(&Config{
Repository: "owner/repo",
Index: ".",
Wd: c.Getwd(),
Wd: c.Wd(),
Badges: []datastore.Datastore{bd},
Reports: []datastore.Datastore{rd},
CoverageColor: c.CoverageColor,
Expand All @@ -75,7 +75,7 @@ func TestGenerateBadges(t *testing.T) {
t.Errorf("got %v\nwant %v", len(paths), want)
}

got := []string{}
var got []string
if err := filepath.Walk(td, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -115,14 +115,14 @@ func TestRenderIndex(t *testing.T) {
if err != nil {
t.Fatal(err)
}
bd, err := local.New(filepath.Join(c.Getwd(), "example/central/badges"))
bd, err := local.New(filepath.Join(c.Wd(), "example/central/badges"))
if err != nil {
t.Fatal(err)
}
ctr := New(&Config{
Repository: c.Repository,
Index: c.Central.Root,
Wd: c.Getwd(),
Wd: c.Wd(),
Badges: []datastore.Datastore{bd},
Reports: []datastore.Datastore{rd},
CoverageColor: c.CoverageColor,
Expand Down
4 changes: 2 additions & 2 deletions cmd/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var badgeCmd = &cobra.Command{
}
case badgeRatio:
if !c.Loaded() {
cmd.PrintErrf("%s are not found\n", strings.Join(config.DefaultConfigFilePaths, " and "))
cmd.PrintErrf("%s are not found\n", strings.Join(config.DefaultPaths, " and "))
}
if err := c.CodeToTestRatioConfigReady(); err != nil {
return err
Expand All @@ -120,7 +120,7 @@ var badgeCmd = &cobra.Command{
if err := c.TestExecutionTimeConfigReady(); err != nil {
return err
}
stepNames := []string{}
var stepNames []string
if len(c.TestExecutionTime.Steps) > 0 {
stepNames = c.TestExecutionTime.Steps
}
Expand Down
24 changes: 6 additions & 18 deletions cmd/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func createReportContent(ctx context.Context, c *config.Config, r, rPrev *report
var files []*gh.PullRequestFile
n, err := g.DetectCurrentPullRequestNumber(ctx, repo.Owner, repo.Repo)
if err == nil {
files, err = g.GetPullRequestFiles(ctx, repo.Owner, repo.Repo, n)
files, err = g.FetchPullRequestFiles(ctx, repo.Owner, repo.Repo, n)
if err != nil {
return "", err
}
} else {
files, err = g.GetChangedFiles(ctx, repo.Owner, repo.Repo)
files, err = g.FetchChangedFiles(ctx, repo.Owner, repo.Repo)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func createReportContent(ctx context.Context, c *config.Config, r, rPrev *report
comment := []string{fmt.Sprintf("## %s", title)}

if err := c.Acceptable(r, rPrev); err != nil {
merr, ok := err.(*multierror.Error)
merr, ok := err.(*multierror.Error) //nolint:errorlint
if !ok {
return "", fmt.Errorf("failed to convert error to multierror: %w", err)
}
Expand All @@ -99,21 +99,9 @@ func createReportContent(ctx context.Context, c *config.Config, r, rPrev *report
comment = append(comment, merr.Error())
}

comment = append(
comment,
table,
"",
fileTable,
)
comment = append(
comment,
customTables...,
)
comment = append(
comment,
"---",
footer,
)
comment = append(comment, table, "", fileTable)
comment = append(comment, customTables...)
comment = append(comment, "---", footer)

return strings.Join(comment, "\n"), nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var dumpCmd = &cobra.Command{
if err := c.TestExecutionTimeConfigReady(); err != nil {
cmd.PrintErrf("Skip measuring test execution time: %v\n", err)
} else {
stepNames := []string{}
var stepNames []string
if len(c.TestExecutionTime.Steps) > 0 {
stepNames = c.TestExecutionTime.Steps
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var initCmd = &cobra.Command{
if err != nil {
return err
}
cf := config.DefaultConfigFilePaths[0]
cf := config.DefaultPaths[0]
p := filepath.Join(wd, cf)
if _, err := os.Stat(p); err == nil {
return fmt.Errorf("%s already exist", p)
Expand Down
21 changes: 15 additions & 6 deletions cmd/lsFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ var lsFilesCmd = &cobra.Command{
if err != nil {
return err
}
gitRoot, err := internal.GetRootPath(wd)
gitRoot, err := internal.RootPath(wd)
if err != nil {
return err
}
cfiles := []string{}
var cfiles []string
for _, f := range r.Coverage.Files {
cfiles = append(cfiles, f.File)
}
files := []string{}
var files []string
if err := filepath.Walk(wd, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -130,9 +130,18 @@ var lsFilesCmd = &cobra.Command{
}

func detectTermColor(cl string) (*color.Color, error) {
termGreen, _ := colorful.Hex("#4e9a06")
termYellow, _ := colorful.Hex("#c4a000")
termRed, _ := colorful.Hex("#cc0000")
termGreen, err := colorful.Hex("#4e9a06")
if err != nil {
return nil, err
}
termYellow, err := colorful.Hex("#c4a000")
if err != nil {
return nil, err
}
termRed, err := colorful.Hex("#cc0000")
if err != nil {
return nil, err
}
tc, err := colorful.Hex(cl)
if err != nil {
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions cmd/migrateBqTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var migrateBqTableCmd = &cobra.Command{
return err
}
if !c.Loaded() {
cmd.PrintErrf("%s are not found\n", strings.Join(config.DefaultConfigFilePaths, " and "))
cmd.PrintErrf("%s are not found\n", strings.Join(config.DefaultPaths, " and "))
}

c.Build()
Expand Down Expand Up @@ -80,7 +80,9 @@ var migrateBqTableCmd = &cobra.Command{
if err := b.CreateTable(ctx); err != nil {
merr = multierror.Append(merr, err)
} else {
_, _ = fmt.Fprintf(os.Stderr, "%s has been created\n", u)
if _, err := fmt.Fprintf(os.Stderr, "%s has been created\n", u); err != nil {
merr = multierror.Append(merr, err)
}
}
}
return merr
Expand Down
14 changes: 7 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var rootCmd = &cobra.Command{
return printMetrics(cmd)
}

addPaths := []string{}
var addPaths []string
cmd.PrintErrf("%s version %s\n", version.Name, version.Version)

c := config.New()
Expand All @@ -74,7 +74,7 @@ var rootCmd = &cobra.Command{
c.Build()

if !c.Loaded() {
cmd.PrintErrf("%s are not found\n", strings.Join(config.DefaultConfigFilePaths, " and "))
cmd.PrintErrf("%s are not found\n", strings.Join(config.DefaultPaths, " and "))
}

ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
Expand All @@ -92,7 +92,7 @@ var rootCmd = &cobra.Command{
return err
}

badges := []datastore.Datastore{}
var badges []datastore.Datastore
for _, s := range c.Central.Badges.Datastores {
d, err := datastore.New(ctx, s, datastore.Root(c.Root()))
if err != nil {
Expand All @@ -101,7 +101,7 @@ var rootCmd = &cobra.Command{
badges = append(badges, d)
}

reports := []datastore.Datastore{}
var reports []datastore.Datastore
for _, s := range c.Central.Reports.Datastores {
d, err := datastore.New(ctx, s, datastore.Root(c.Root()))
if err != nil {
Expand All @@ -113,7 +113,7 @@ var rootCmd = &cobra.Command{
ctr := central.New(&central.Config{
Repository: c.Repository,
Index: c.Central.Root,
Wd: c.Getwd(),
Wd: c.Wd(),
Badges: badges,
Reports: reports,
CoverageColor: c.CoverageColor,
Expand Down Expand Up @@ -185,7 +185,7 @@ var rootCmd = &cobra.Command{
if err := c.TestExecutionTimeConfigReady(); err != nil {
cmd.PrintErrf("Skip measuring test execution time: %v\n", err)
} else {
stepNames := []string{}
var stepNames []string
if len(c.TestExecutionTime.Steps) > 0 {
stepNames = c.TestExecutionTime.Steps
}
Expand Down Expand Up @@ -520,7 +520,7 @@ func printMetrics(cmd *cobra.Command) error {
}

if err := c.TestExecutionTimeConfigReady(); r.Repository != "" && err == nil {
stepNames := []string{}
var stepNames []string
if len(c.TestExecutionTime.Steps) > 0 {
stepNames = c.TestExecutionTime.Steps
}
Expand Down
Loading

0 comments on commit 547dfaf

Please sign in to comment.