diff --git a/bin/refalLint b/bin/refalLint index 9b2f967..455f627 100755 Binary files a/bin/refalLint and b/bin/refalLint differ diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod index 39e4c74..c7b9526 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,8 @@ module github.com/OnionGrief/AlliumLinter go 1.21 +require gopkg.in/yaml.v3 v3.0.1 + require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/cobra v1.8.0 // indirect diff --git a/go.sum b/go.sum index d0e8c2c..b17a5f8 100644 --- a/go.sum +++ b/go.sum @@ -7,4 +7,5 @@ github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyh github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/checks/precondition.go b/src/checks/precondition.go index c5b750d..81eee5a 100644 --- a/src/checks/precondition.go +++ b/src/checks/precondition.go @@ -12,7 +12,7 @@ import ( func CheckPrecondition(tokens []lexer.Token) []logger.Log { var logs []logger.Log logs = append(logs, checkExtern(tokens)...) - if config.SnakeCase || config.CamelCase { + if config.Cfg.SnakeCase || config.Cfg.CamelCase { logs = append(logs, checkNames(tokens)...) } logs = append(logs, checkConstCount(tokens)...) @@ -22,11 +22,11 @@ func CheckPrecondition(tokens []lexer.Token) []logger.Log { // Проверка на вынесение констант func checkConstCount(tokens []lexer.Token) []logger.Log { var logs []logger.Log - count := config.ConstCount + count := config.Cfg.ConstCount if count < 3 { count = 3 } - leng := config.ConstLen + leng := config.Cfg.ConstLen if leng < 3 { leng = 3 } @@ -76,12 +76,12 @@ func checkNames(tokens []lexer.Token) []logger.Log { if tkn.TokenType == lexer.VAR { value = value[2:] } - if config.CamelCase { + if config.Cfg.CamelCase { if !isCamelCase(value) { logs = append(logs, logger.FormatNameLogCamel(tkn)) } } - if config.SnakeCase { + if config.Cfg.SnakeCase { if !isSnakeCase(value) { logs = append(logs, logger.FormatNameLogShake(tkn)) } diff --git a/src/cmd/lint/lint.go b/src/cmd/lint/lint.go index e511e80..e3a330f 100644 --- a/src/cmd/lint/lint.go +++ b/src/cmd/lint/lint.go @@ -16,6 +16,7 @@ import ( ) var fromDirectory string +var configPath string var LintCmd = &cobra.Command{ Use: "lint", @@ -30,6 +31,10 @@ var LintCmd = &cobra.Command{ } files = args } + cfg := config.ReadConfigFromFile(configPath) + if cfg != nil { + config.Cfg = *cfg + } prepareFiles(files) }, } @@ -70,11 +75,12 @@ func findRefFilesInDirectory(dir string) []string { } func init() { LintCmd.Flags().StringVarP(&fromDirectory, "from-dir", "d", "", "Specify the directory to search for .ref files") - LintCmd.Flags().BoolVarP(&config.SnakeCase, "snake", "s", false, "Use SnakeCase for format") - LintCmd.Flags().BoolVarP(&config.CamelCase, "camel", "c", false, "Use CamelCase for format") - LintCmd.Flags().UintVarP(&config.ConstLen, "constLen", "L", 3, "ConstLen") - LintCmd.Flags().UintVarP(&config.ConstCount, "constCount", "C", 3, "ConstCount") - LintCmd.Flags().UintVarP(&config.BlockLen, "blockLen", "b", 3, "Длина переиспользуемого блока") + LintCmd.Flags().StringVarP(&configPath, "cfg-path", "p", "./config.yaml", "Config file from thos directory") + LintCmd.Flags().BoolVarP(&config.Cfg.SnakeCase, "snake", "s", false, "Use SnakeCase for format") + LintCmd.Flags().BoolVarP(&config.Cfg.CamelCase, "camel", "c", false, "Use CamelCase for format") + LintCmd.Flags().UintVarP(&config.Cfg.ConstLen, "constLen", "L", 3, "ConstLen") + LintCmd.Flags().UintVarP(&config.Cfg.ConstCount, "constCount", "C", 3, "ConstCount") + LintCmd.Flags().UintVarP(&config.Cfg.BlockLen, "blockLen", "b", 3, "Длина переиспользуемого блока") } func prepareFiles(files []string) { diff --git a/src/config/config.go b/src/config/config.go index 9045dcf..54aa0aa 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -1,16 +1,36 @@ package config -var ( - SnakeCase bool - CamelCase bool - ConstCount uint - ConstLen uint - PrintTree bool - BlockLen uint +import ( + "fmt" + "gopkg.in/yaml.v3" + "os" ) +type Config struct { + SnakeCase bool `yaml:"SnakeCase"` + CamelCase bool `yaml:"CamelCase"` + ConstCount uint `yaml:"ConstCount"` + ConstLen uint `yaml:"ConstLen"` + BlockLen uint `yaml:"BlockLen"` +} + +var Cfg = Config{} + const FuncComment = "OPT:" var UsingFunctions []string = []string{"Go"} -var CountVar = 3 +func ReadConfigFromFile(filename string) *Config { + file, err := os.ReadFile(filename) + if err != nil { + return nil + } + + var config = Config{} + err = yaml.Unmarshal(file, &config) + if err != nil { + return nil + } + fmt.Println("Получен конфиг из файла", config) + return &config +} diff --git a/src/tree/checkTree.go b/src/tree/checkTree.go index 75340e1..ddaeab4 100644 --- a/src/tree/checkTree.go +++ b/src/tree/checkTree.go @@ -150,7 +150,7 @@ func CheckTreeRec(elem *treeElem) []forCheckTreeRec { if (collection[comb.a].elem.typeEl == PatternExprTerm || collection[comb.a].elem.typeEl == PatternExpr) && (collection[comb.b].elem.typeEl == ResultExpr || collection[comb.b].elem.typeEl == ResultExprTerm) { if r := checkTreeRecBool(collection[comb.a].elem, collection[comb.b].elem); r { - if collection[comb.b].deep > int(config.BlockLen) { + if collection[comb.b].deep > int(config.Cfg.BlockLen) { forCheck = append(forCheck, forCheckTreeRec{ first: сoords{ Start: collection[comb.a].elem.start,