Skip to content

Commit

Permalink
Поддержка внешнего конфига
Browse files Browse the repository at this point in the history
  • Loading branch information
Teryukha Mikhail Romanovich committed Jun 29, 2024
1 parent 30c24dc commit 0b5f6cf
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 19 deletions.
Binary file modified bin/refalLint
Binary file not shown.
Empty file added config.yaml
Empty file.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
10 changes: 5 additions & 5 deletions src/checks/precondition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
Expand All @@ -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
}
Expand Down Expand Up @@ -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))
}
Expand Down
16 changes: 11 additions & 5 deletions src/cmd/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var fromDirectory string
var configPath string

var LintCmd = &cobra.Command{
Use: "lint",
Expand All @@ -30,6 +31,10 @@ var LintCmd = &cobra.Command{
}
files = args
}
cfg := config.ReadConfigFromFile(configPath)
if cfg != nil {
config.Cfg = *cfg
}
prepareFiles(files)
},
}
Expand Down Expand Up @@ -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) {
Expand Down
36 changes: 28 additions & 8 deletions src/config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion src/tree/checkTree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0b5f6cf

Please sign in to comment.