parent
853ac47e42
commit
eb4cca4737
@ -1 +0,0 @@
|
|||||||
package main
|
|
@ -0,0 +1,90 @@
|
|||||||
|
package checker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/openimsdk/open-im-server/tools/codescan/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CheckResult struct {
|
||||||
|
FilePath string
|
||||||
|
Lines []int
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkFileForChineseComments(filePath string) ([]CheckResult, error) {
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var results []CheckResult
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
reg := regexp.MustCompile(`[\p{Han}]+`)
|
||||||
|
lineNumber := 0
|
||||||
|
|
||||||
|
var linesWithChinese []int
|
||||||
|
for scanner.Scan() {
|
||||||
|
lineNumber++
|
||||||
|
if reg.FindString(scanner.Text()) != "" {
|
||||||
|
linesWithChinese = append(linesWithChinese, lineNumber)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(linesWithChinese) > 0 {
|
||||||
|
results = append(results, CheckResult{
|
||||||
|
FilePath: filePath,
|
||||||
|
Lines: linesWithChinese,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func WalkDirAndCheckComments(cfg config.Config) error {
|
||||||
|
var allResults []CheckResult
|
||||||
|
err := filepath.Walk(cfg.Directory, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, fileType := range cfg.FileTypes {
|
||||||
|
if filepath.Ext(path) == fileType {
|
||||||
|
results, err := checkFileForChineseComments(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(results) > 0 {
|
||||||
|
allResults = append(allResults, results...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(allResults) > 0 {
|
||||||
|
var errMsg strings.Builder
|
||||||
|
errMsg.WriteString("Files containing Chinese comments:\n")
|
||||||
|
for _, result := range allResults {
|
||||||
|
errMsg.WriteString(fmt.Sprintf("%s: Lines %v\n", result.FilePath, result.Lines))
|
||||||
|
}
|
||||||
|
return fmt.Errorf(errMsg.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/openimsdk/open-im-server/tools/codescan/checker"
|
||||||
|
"github.com/openimsdk/open-im-server/tools/codescan/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cfg, err := config.ParseConfig()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error parsing config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = checker.WalkDirAndCheckComments(cfg)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
directory: ./
|
||||||
|
file_types:
|
||||||
|
- .go
|
||||||
|
- .yaml
|
||||||
|
- .yml
|
||||||
|
languages:
|
||||||
|
- Chinese
|
@ -0,0 +1,35 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Directory string `yaml:"directory"`
|
||||||
|
FileTypes []string `yaml:"file_types"`
|
||||||
|
Languages []string `yaml:"languages"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseConfig() (Config, error) {
|
||||||
|
var configPath string
|
||||||
|
flag.StringVar(&configPath, "config", "./", "Path to config file")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
var config Config
|
||||||
|
if configPath != "" {
|
||||||
|
configFile, err := os.ReadFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
|
if err := yaml.Unmarshal(configFile, &config); err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Fatal("Config file must be provided")
|
||||||
|
}
|
||||||
|
return config, nil
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
module github.com/openimsdk/open-im-server/tools/codescan
|
||||||
|
|
||||||
|
go 1.19
|
Loading…
Reference in new issue