Skip to content

Commit 2bf0fef

Browse files
committed
feat: add actions
1 parent 2df26b2 commit 2bf0fef

13 files changed

+142
-60
lines changed

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the MIT License (the "License");
33
# you may not use this file except in compliance with the License.
44

5-
name: Go composite Release Version
5+
name: Go standardizer Release Version
66

77
on:
88
push:

.github/workflows/test.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ jobs:
1414
with:
1515
go-version: '1.21'
1616

17-
- name: Install composite
17+
- name: Install standardizer
1818
run: |
19-
go install github.com/kubecub/composite@latest
19+
go install github.com/kubecub/standardizer@latest
2020
21-
- name: Run composite Detector
22-
run: composite
21+
- name: Run standardizer Detector
22+
run: standardizer

.goreleaser.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ report_sizes: true
1919
dist: _output/dist
2020

2121
builds:
22-
- binary: composite
23-
id: composite
24-
main: ./composite.go
22+
- binary: standardizer
23+
id: standardizer
24+
main: ./standardizer.go
2525
goos:
2626
- windows
2727
- darwin
@@ -85,10 +85,10 @@ archives:
8585
nfpms:
8686
- id: packages
8787
builds:
88-
- composite
88+
- standardizer
8989
# Your app's vendor.
9090
vendor: kubecub
91-
homepage: https://github.com/kubecub/composite
91+
homepage: https://github.com/kubecub/standardizer
9292
maintainer: kubbot <https://github.com/kubbot>
9393
description: |-
9494
Comment Lang Detector is a tool to detect the language of comments in code files.
@@ -206,7 +206,7 @@ checksum:
206206

207207
release:
208208
footer: |
209-
**Full Changelog**: https://github.com/kubecub/composite/compare/{{ .PreviousTag }}...{{ .Tag }}
209+
**Full Changelog**: https://github.com/kubecub/standardizer/compare/{{ .PreviousTag }}...{{ .Tag }}
210210
211211
## Helping out
212212

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ An Github Actions Tools, Development of a Go-Based Conformity Checker for Projec
66

77
#### Project Name
88

9-
- `GoConcomposite`
9+
- `GoConstandardizer`
1010

1111
#### Functionality Description
1212

@@ -54,7 +54,7 @@ package main
5454
import (
5555
"flag"
5656
"fmt"
57-
"GoConcomposite/checker"
57+
"GoConstandardizer/checker"
5858
)
5959

6060
func main() {

action.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: 'Conformity Checker for Project'
2-
description: 'composite action for conformity checker for project'
2+
description: 'standardizer action for conformity checker for project'
33
author: 'cubxxw'
44
runs:
5-
using: 'composite'
5+
using: 'standardizer'
66
steps:
77
- name: Checkout Repository
88
uses: actions/checkout@v4

checker/checker.go

+100-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package checker
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
7+
"regexp"
68
"strings"
79

810
"github.com/kubecub/standardizer/config"
@@ -24,7 +26,24 @@ type Checker struct {
2426
}
2527

2628
func (c *Checker) Check() error {
27-
return filepath.Walk(c.Config.BaseConfig.SearchDirectory, c.checkPath)
29+
err := filepath.Walk(c.Config.BaseConfig.SearchDirectory, c.checkPath)
30+
if err != nil {
31+
return err
32+
}
33+
34+
if len(c.Summary.Issues) > 0 {
35+
c.printIssues()
36+
return fmt.Errorf("found %d issues in the codebase", len(c.Summary.Issues))
37+
}
38+
return nil
39+
}
40+
41+
func (c *Checker) printIssues() {
42+
fmt.Println("## Issues found:")
43+
fmt.Println("===================================================================================================")
44+
for _, issue := range c.Summary.Issues {
45+
fmt.Printf("Type: %s, Path: %s, Message: %s\n", issue.Type, issue.Path, issue.Message)
46+
}
2847
}
2948

3049
func (c *Checker) checkPath(path string, info os.FileInfo, err error) error {
@@ -42,15 +61,10 @@ func (c *Checker) checkPath(path string, info os.FileInfo, err error) error {
4261
}
4362

4463
if info.IsDir() {
45-
c.Summary.CheckedDirectories++
4664
if c.isIgnoredDirectory(relativePath) {
47-
c.Summary.Issues = append(c.Summary.Issues, Issue{
48-
Type: "ignoredDirectory",
49-
Path: path,
50-
Message: "This directory has been ignored",
51-
})
5265
return filepath.SkipDir
5366
}
67+
c.Summary.CheckedDirectories++
5468
if !c.checkDirectoryName(relativePath) {
5569
c.Summary.Issues = append(c.Summary.Issues, Issue{
5670
Type: "directoryNaming",
@@ -77,17 +91,21 @@ func (c *Checker) checkPath(path string, info os.FileInfo, err error) error {
7791

7892
func (c *Checker) isIgnoredDirectory(path string) bool {
7993
for _, ignoredDir := range c.Config.IgnoreDirectories {
80-
if strings.Contains(path, ignoredDir) {
94+
if strings.HasSuffix(path, ignoredDir) || strings.Contains(path, ignoredDir+"/") {
8195
return true
8296
}
8397
}
8498
return false
8599
}
86100

87101
func (c *Checker) isIgnoredFile(path string) bool {
88-
ext := filepath.Ext(path)
89102
for _, format := range c.Config.IgnoreFormats {
90-
if ext == format {
103+
matched, err := regexp.MatchString(format, path)
104+
if err != nil {
105+
fmt.Printf("Invalid regex pattern: %s, error: %s\n", format, err)
106+
continue
107+
}
108+
if matched {
91109
return true
92110
}
93111
}
@@ -96,21 +114,34 @@ func (c *Checker) isIgnoredFile(path string) bool {
96114

97115
func (c *Checker) checkDirectoryName(path string) bool {
98116
dirName := filepath.Base(path)
117+
errors := []string{}
118+
99119
if c.Config.DirectoryNaming.MustBeLowercase && (dirName != strings.ToLower(dirName)) {
100-
return false
120+
errors = append(errors, "directory name must be lowercase")
101121
}
102122
if !c.Config.DirectoryNaming.AllowHyphens && strings.Contains(dirName, "-") {
103-
return false
123+
errors = append(errors, "hyphens are not allowed in directory names")
104124
}
105125
if !c.Config.DirectoryNaming.AllowUnderscores && strings.Contains(dirName, "_") {
126+
errors = append(errors, "underscores are not allowed in directory names")
127+
}
128+
129+
if len(errors) > 0 {
130+
c.Summary.Issues = append(c.Summary.Issues, Issue{
131+
Type: "directoryNaming",
132+
Path: path,
133+
Message: fmt.Sprintf("Directory naming issues: %s. Example of valid directory name: '%s'", strings.Join(errors, "; "), c.exampleDirectoryName()),
134+
})
106135
return false
107136
}
137+
108138
return true
109139
}
110140

111141
func (c *Checker) checkFileName(path string) bool {
112142
fileName := filepath.Base(path)
113143
ext := filepath.Ext(fileName)
144+
errors := []string{}
114145

115146
allowHyphens := c.Config.FileNaming.AllowHyphens
116147
allowUnderscores := c.Config.FileNaming.AllowUnderscores
@@ -123,14 +154,69 @@ func (c *Checker) checkFileName(path string) bool {
123154
}
124155

125156
if mustBeLowercase && (fileName != strings.ToLower(fileName)) {
126-
return false
157+
errors = append(errors, "file name must be lowercase")
127158
}
128159
if !allowHyphens && strings.Contains(fileName, "-") {
129-
return false
160+
errors = append(errors, "hyphens are not allowed in file names")
130161
}
131162
if !allowUnderscores && strings.Contains(fileName, "_") {
163+
errors = append(errors, "underscores are not allowed in file names")
164+
}
165+
166+
if len(errors) > 0 {
167+
c.Summary.Issues = append(c.Summary.Issues, Issue{
168+
Type: "fileNaming",
169+
Path: path,
170+
Message: fmt.Sprintf("File naming issues: %s. Example of valid file name: '%s'", strings.Join(errors, "; "), c.exampleFileName(ext)),
171+
})
132172
return false
133173
}
134174

135175
return true
136176
}
177+
178+
func (c *Checker) exampleDirectoryName() string {
179+
exampleName := "ExampleDirectory"
180+
if c.Config.DirectoryNaming.MustBeLowercase {
181+
exampleName = strings.ToLower(exampleName)
182+
}
183+
if !c.Config.DirectoryNaming.AllowHyphens && !c.Config.DirectoryNaming.AllowUnderscores {
184+
exampleName = strings.ReplaceAll(exampleName, "-", "")
185+
exampleName = strings.ReplaceAll(exampleName, "_", "")
186+
} else if c.Config.DirectoryNaming.AllowHyphens {
187+
exampleName = strings.ReplaceAll(exampleName, "_", "-")
188+
} else if c.Config.DirectoryNaming.AllowUnderscores {
189+
exampleName = strings.ReplaceAll(exampleName, "-", "_")
190+
}
191+
return exampleName
192+
}
193+
194+
func (c *Checker) exampleFileName(ext string) string {
195+
baseName := "example_file"
196+
if c.Config.FileNaming.MustBeLowercase {
197+
baseName = strings.ToLower(baseName)
198+
}
199+
200+
if !c.Config.FileNaming.AllowHyphens && !c.Config.FileNaming.AllowUnderscores {
201+
baseName = strings.ReplaceAll(baseName, "-", "")
202+
baseName = strings.ReplaceAll(baseName, "_", "")
203+
} else if c.Config.FileNaming.AllowHyphens {
204+
} else if c.Config.FileNaming.AllowUnderscores {
205+
baseName = strings.ReplaceAll(baseName, "-", "_")
206+
}
207+
208+
if specificNaming, ok := c.Config.FileTypeSpecificNaming[ext]; ok {
209+
if specificNaming.MustBeLowercase {
210+
baseName = strings.ToLower(baseName)
211+
}
212+
if !specificNaming.AllowHyphens && !specificNaming.AllowUnderscores {
213+
baseName = strings.ReplaceAll(baseName, "-", "")
214+
baseName = strings.ReplaceAll(baseName, "_", "")
215+
} else if specificNaming.AllowHyphens {
216+
} else if specificNaming.AllowUnderscores {
217+
baseName = strings.ReplaceAll(baseName, "-", "_")
218+
}
219+
}
220+
221+
return fmt.Sprintf("%s%s", baseName, ext)
222+
}

config.yaml

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
1-
# config.yaml 示例配置
2-
3-
# 基础配置
41
baseConfig:
5-
searchDirectory: "./" # 检索的根目录,"./" 代表当前目录
6-
ignoreCase: false # 是否忽略大小写,true 为忽略,false 为区分大小写
2+
searchDirectory: "./"
3+
ignoreCase: false
74

8-
# 目录命名风格配置
95
directoryNaming:
10-
allowHyphens: true # 是否允许目录名中含有中划线
11-
allowUnderscores: false # 是否允许目录名中含有下划线
12-
mustBeLowercase: true # 目录名是否必须为小写
6+
allowHyphens: true
7+
allowUnderscores: false
8+
mustBeLowercase: true
139

14-
# 文件命名风格配置
1510
fileNaming:
16-
allowHyphens: true # 是否允许文件名中含有中划线
17-
allowUnderscores: true # 是否允许文件名中含有下划线
18-
mustBeLowercase: true # 文件名是否必须为小写
11+
allowHyphens: true
12+
allowUnderscores: true
13+
mustBeLowercase: true
1914

20-
# 忽略的文件格式列表
2115
ignoreFormats:
22-
- ".log"
23-
- ".env"
24-
- "_test.go"
16+
- "\\.log$"
17+
- "\\.env$"
18+
- "README\\.md$"
19+
- "_test\\.go$"
20+
- "\\.md$"
21+
- LICENSE
2522

26-
# 忽略的目录列表
2723
ignoreDirectories:
2824
- "vendor"
2925
- ".git"
3026
- "node_modules"
3127
- "logs"
28+
- "CHANGELOG"
3229
- "components"
3330
- "_output"
34-
- "README.md"
3531
- "tools/openim-web"
3632
- "CHANGELOG"
37-
- "docs/readme"
33+
- "examples/Test_directory"
3834

3935
fileTypeSpecificNaming:
4036
".yaml":

examples/config/Example.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.yaml

examples/config/example-File.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.yaml

examples/config/example-file.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.yaml

examples/config/example.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.yaml

examples/config/example_file.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.yaml

composite.go main.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func main() {
2121

2222
if configPath == "" {
2323
configPath = "config.yaml"
24-
if _, err := os.Stat(".github/composite.yaml"); err == nil {
25-
configPath = ".github/composite.yaml"
24+
if _, err := os.Stat(".github/standardizer.yaml"); err == nil {
25+
configPath = ".github/standardizer.yaml"
2626
}
2727
}
2828

@@ -35,18 +35,13 @@ func main() {
3535
c := &checker.Checker{Config: cfg}
3636
err = c.Check()
3737
if err != nil {
38-
fmt.Println("Error during check:", err)
38+
fmt.Println()
39+
fmt.Println("===================================================================================================")
40+
fmt.Println("Please check whether the above file conforms to the specification, or check whether the configuration file is qualified")
41+
fmt.Println("!!!Error during check:", err)
3942
os.Exit(1)
4043
}
4144

42-
// if len(c.Errors) > 0 {
43-
// fmt.Println("Found errors:")
44-
// for _, errMsg := range c.Errors {
45-
// fmt.Println("-", errMsg)
46-
// }
47-
// os.Exit(1)
48-
// }
49-
5045
summaryJSON, err := json.MarshalIndent(c.Summary, "", " ")
5146
if err != nil {
5247
fmt.Println("Error marshalling summary:", err)

0 commit comments

Comments
 (0)