Skip to content

Commit e241696

Browse files
committed
feat: optimize code
1 parent 8ca187f commit e241696

File tree

4 files changed

+149
-77
lines changed

4 files changed

+149
-77
lines changed

.goreleaser.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dist: _output/dist
2121
builds:
2222
- binary: standardizer
2323
id: standardizer
24-
main: ./standardizer.go
24+
main: ./main.go
2525
goos:
2626
- windows
2727
- darwin

README.md

+94-76
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,122 @@
1-
# Development of a Go-Based Conformity Checker for Project File and Directory Naming Standards
1+
# README for Standardizer
22

3-
An Github Actions Tools, Development of a Go-Based Conformity Checker for Project File and Directory Naming Standards
3+
## Overview
44

5-
### 1. Project Overview
5+
**Standardizer** is a versatile GitHub Action tool designed to ensure your project's file and directory names adhere to specified naming conventions. It automates the process of checking the conformity of naming standards within your repository, saving time and maintaining consistency across your codebase. Standardizer can be easily integrated into your GitHub workflows or used locally to inspect your project's structure for any deviations from the defined naming rules.
66

7-
#### Project Name
7+
## Features
88

9-
- `GoConstandardizer`
9+
- **Flexible Configuration**: Customize naming rules for files and directories through a straightforward YAML configuration.
10+
- **Regex Support for Ignoring Formats**: Utilize regular expressions to define exceptions for file formats that do not require standardization.
11+
- **Environment Variables & Flags Support**: Specify the configuration file's location using environment variables or command-line flags for versatile setup options.
12+
- **Local & CI Integration**: Designed for both local development environments and continuous integration workflows, ensuring consistency across different stages of development.
1013

11-
#### Functionality Description
14+
## Getting Started
1215

13-
- Checks if the file and subdirectory names in a specified directory adhere to specific naming conventions.
14-
- Supports specific file types (e.g., `.go`, `.yml`, `.yaml`, `.md`, `.sh`, etc.).
15-
- Allows users to specify directories to be checked and directories to be ignored.
16-
- More read https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/code-conventions.md
16+
### Installation
1717

18-
#### Naming Conventions
18+
For local use, you can either download the latest release from [GitHub Releases](https://github.com/kubecub/standardizer/releases/) or install directly using Go:
1919

20-
- Go files: Only underscores are allowed.
21-
- YAML, YML, and Markdown files: Only hyphens are allowed.
22-
- Directories: Only underscores are allowed.
23-
24-
### 2. File Structure
25-
26-
- `main.go`: Entry point of the program, handles command-line arguments.
27-
- `checker/checker.go`: Contains the core logic.
28-
- `config/config.go`: Parses and stores configuration information.
29-
30-
### 3. Core Code Design
31-
32-
#### main.go
33-
34-
- Parses command-line arguments, including the directory to be checked and directories to be ignored.
35-
- Calls the `checker` module for checking.
36-
37-
#### config.go
20+
```shell
21+
go install github.com/kubecub/standardizer@latest
22+
```
3823

39-
- Defines a configuration structure, such as directories to check and ignore.
24+
### Configuration
25+
26+
Create a `standardizer.yml` configuration file in your project's root directory or under `.github/`. Alternatively, specify the configuration file's path through environment variables or flags.
27+
28+
**Example `standardizer.yml` Configuration:**
29+
30+
```yaml
31+
baseConfig:
32+
searchDirectory: "./"
33+
ignoreCase: false
34+
35+
directoryNaming:
36+
allowHyphens: true
37+
allowUnderscores: false
38+
mustBeLowercase: true
39+
40+
fileNaming:
41+
allowHyphens: true
42+
allowUnderscores: true
43+
mustBeLowercase: true
44+
45+
ignoreFormats:
46+
- "\\.log$"
47+
- "\\.env$"
48+
- "README\\.md$"
49+
- "_test\\.go$"
50+
- "\\.md$"
51+
- LICENSE
52+
53+
ignoreDirectories:
54+
- "vendor"
55+
- ".git"
56+
- "node_modules"
57+
- "logs"
58+
- "CHANGELOG"
59+
- "components"
60+
- "_output"
61+
- "tools/openim-web"
62+
- "CHANGELOG"
63+
- "examples/Test_directory"
64+
65+
fileTypeSpecificNaming:
66+
".yaml":
67+
allowHyphens: true
68+
allowUnderscores: false
69+
mustBeLowercase: true
70+
".go":
71+
allowHyphens: false
72+
allowUnderscores: true
73+
mustBeLowercase: true
74+
```
4075
41-
#### checker.go
76+
### Configuration Details
4277
43-
- Iterates through the specified directory.
44-
- Applies different naming rules based on file types and directory names.
45-
- Records files or directories that do not conform to the standards.
78+
- **baseConfig**: General settings for the scan, such as the directory to search (`searchDirectory`) and whether to ignore case sensitivity (`ignoreCase`).
79+
- **directoryNaming** & **fileNaming**: Define the allowed naming conventions for directories and files, respectively, including the use of hyphens, underscores, and case sensitivity.
80+
- **ignoreFormats**: A list of regex patterns for file formats that should be ignored during the naming checks. This is useful for excluding specific file types or naming patterns.
81+
- **ignoreDirectories**: Directories to exclude from the scan, ensuring that vendor or third-party directories won't affect your compliance results.
82+
- **fileTypeSpecificNaming**: Allows for specifying naming conventions on a per-file-extension basis, offering greater flexibility for projects that use a variety of file types with different standards.
4683

47-
### 4. Pseudocode Example
84+
### Using Standardizer in GitHub Actions
4885

49-
#### main.go
86+
To integrate Standardizer into your GitHub Actions workflow, add the following step to your `.github/workflows/main.yml`:
5087

51-
```go
52-
package main
88+
```yaml
89+
- name: Conformity Checker for Project
90+
uses: kubecub/standardizer@main # or use a specific tag like @v1.0.0
91+
```
5392

54-
import (
55-
"flag"
56-
"fmt"
57-
"GoConstandardizer/checker"
58-
)
93+
### Local Usage
5994

60-
func main() {
61-
// Parse command-line arguments
62-
var targetDir string
63-
var ignoreDirs string
64-
flag.StringVar(&targetDir, "target", ".", "Directory to check")
65-
flag.StringVar(&ignoreDirs, "ignore", "", "Directories to ignore")
66-
flag.Parse()
95+
After configuring `standardizer.yml`, run Standardizer locally to check your project's naming conventions:
6796

68-
// Call the checker
69-
err := checker.CheckDirectory(targetDir, ignoreDirs)
70-
if err != nil {
71-
fmt.Println("Error:", err)
72-
}
73-
}
97+
```shell
98+
standardizer -config .github/standardizer.yml
7499
```
75100

76-
#### checker.go
77-
78-
```go
79-
package checker
101+
### Success Output
80102

81-
import (
82-
// Import necessary packages
83-
)
103+
A successful run will output the number of directories and files checked, along with any identified issues:
84104

85-
func CheckDirectory(targetDir, ignoreDirs string) error {
86-
// Iterate through the directory, applying rules to check file and directory names
87-
// Return any found errors or non-conformities
88-
return nil
105+
```json
106+
{
107+
"CheckedDirectories": 4,
108+
"CheckedFiles": 15,
109+
"Issues": null
89110
}
90111
```
91112

92-
### 5. Implementation Details
113+
- **CheckedDirectories** & **CheckedFiles**: The count of directories and files that were checked.
114+
- **Issues**: Lists any naming convention violations found. `null` indicates no issues were detected.
93115

94-
- **File and Directory Traversal**: Use Go's `path/filepath` package to traverse directories and subdirectories.
95-
- **Naming Rules Checking**: Apply different regex expressions for naming checks based on file extensions.
96-
- **Error Handling and Reporting**: Record files or directories that do not conform and report to the user.
116+
## Contributing
97117

98-
### 6. Future Development and Extensions
118+
Contributions to Standardizer are welcome! Please refer to the project's GitHub page for contribution guidelines: [https://github.com/kubecub/standardizer](https://github.com/kubecub/standardizer).
99119

100-
- Support more file types and naming rules.
101-
- Provide more detailed error reports, such as showing line numbers and specific naming mistakes.
102-
- Add a graphical or web interface for non-command-line users.
120+
## License
103121

104-
The above is an overview of the entire project's design. Following this design, specific coding implementation can begin. Note that the actual implementation may need adjustments based on real-world conditions.
122+
Standardizer is released under the MIT License. See the LICENSE file for more details.

main.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"os"
8+
9+
"github.com/kubecub/standardizer/checker"
10+
"github.com/kubecub/standardizer/config"
11+
)
12+
13+
func main() {
14+
var configPath string
15+
flag.StringVar(&configPath, "config", "", "Path to the configuration file")
16+
flag.Parse()
17+
18+
if configPath == "" {
19+
configPath = os.Getenv("STANDARDIZER_CONFIG_PATH")
20+
}
21+
22+
if configPath == "" {
23+
configPath = "standardizer.yaml"
24+
if _, err := os.Stat(".github/standardizer.yaml"); err == nil {
25+
configPath = ".github/standardizer.yaml"
26+
} else if _, err := os.Stat(".github/standardizer.yml"); err == nil {
27+
configPath = ".github/standardizer.yml"
28+
}
29+
}
30+
31+
cfg, err := config.LoadConfig(configPath)
32+
if err != nil {
33+
fmt.Println("Error loading config:", err)
34+
return
35+
}
36+
37+
c := &checker.Checker{Config: cfg}
38+
err = c.Check()
39+
if err != nil {
40+
fmt.Println()
41+
fmt.Println("===================================================================================================")
42+
fmt.Println("Please check whether the above file conforms to the specification, or check whether the configuration file is qualified")
43+
fmt.Println("!!!Error during check:", err)
44+
os.Exit(1)
45+
}
46+
47+
summaryJSON, err := json.MarshalIndent(c.Summary, "", " ")
48+
if err != nil {
49+
fmt.Println("Error marshalling summary:", err)
50+
return
51+
}
52+
53+
fmt.Println(string(summaryJSON))
54+
}
File renamed without changes.

0 commit comments

Comments
 (0)