diff --git a/config/config.go b/config/config.go index 50e99ba2..bce71803 100644 --- a/config/config.go +++ b/config/config.go @@ -7,29 +7,46 @@ import ( ) // Config maps org or repo to LinterConfig +//type Config map[string]map[string]Linter + type Config map[string]map[string]Linter type Linter struct { // Enable is whether to enable this linter, if false, linter still run but not report. - Enable bool `json:"enable,omitempty"` - WorkDir string `json:"workDir,omitempty"` - Command string `json:"command,omitempty"` - Args []string `json:"args,omitempty"` + Enable *bool `json:"enable,omitempty" yaml:"enable,omitempty"` + WorkDir string `json:"workDir,omitempty" yaml:"workDir,omitempty"` + Command string `json:"command,omitempty" yaml:"command,omitempty"` + Args []string `json:"args,omitempty" yaml:"args,omitempty"` } // NewConfig returns a new Config. func NewConfig(conf string) (Config, error) { - var c Config f, err := os.ReadFile(conf) if err != nil { return nil, err } + c := Config{} if err = yaml.Unmarshal(f, &c); err != nil { return nil, err } - // TODO: 应该默认开启staticcheck? + defaultEnable := true + for _, v := range c { + for _, val := range v { + if val.Enable == nil { + val.Enable = &defaultEnable + } + } + } + + if len(c) == 0 { + c["qbox"] = map[string]Linter{ + "staticcheck": {Enable: &defaultEnable}, + "govet": {Enable: &defaultEnable}, + "luacheck": {Enable: &defaultEnable}, + } + } return c, nil } diff --git a/config/config.yaml b/config/config.yaml index 6fa6f4c9..9614cab8 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,10 +1,17 @@ # This is the default config file, you can override it by creating a config.yaml in the same directory # by default, all linters are enabled if you don't specify. You can disable them by setting enable to false # example1: disable staticcheck for org -# qbox: +#qbox: # staticcheck: # enable: false -# +# govet: +# enable: true +# luacheck: +# enable: true +# workDir: "nginx/Lua" +# #command: luacheck + +# # example2: disable staticcheck for repo # qbox/kodo: # staticcheck: diff --git a/config/config_test.go b/config/config_test.go index 92a6bef0..b4dd0ebd 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,19 +1,70 @@ package config import ( - "fmt" + "os" + "path" "testing" + + "github.com/stretchr/testify/assert" ) func TestConfig(t *testing.T) { - repoConfig, err := NewConfig("config.yaml") - if err != nil { - t.Errorf("NewConfig() error = %v", err) - return + defaultTrue := true + defaultFalse := false + case1 := ` +qbox: + staticcheck: + enable: false + luacheck: + enable: true + workDir: "nginx/Lua" + command: luacheck +` + + exp1 := map[string]map[string]Linter{ + "qbox": { + "staticcheck": {Enable: &defaultFalse}, + "luacheck": {Enable: &defaultTrue, WorkDir: "nginx/Lua", Command: "luacheck"}, + }, + } + + case2 := ` +qbox: + luacheck: + enable: false + workDir: "nginx/Lua" + command: luacheck +` + exp2 := map[string]map[string]Linter{ + "qbox": { + "luacheck": {Enable: &defaultFalse, WorkDir: "nginx/Lua", Command: "luacheck"}, + }, } - for k, c := range repoConfig { - fmt.Printf("%v: %v \n", k, c) + case3 := `` + exp3 := map[string]map[string]Linter{ + "qbox": { + "staticcheck": {Enable: &defaultTrue}, + "govet": {Enable: &defaultTrue}, + "luacheck": {Enable: &defaultTrue}, + }, } + cs := map[string]map[string]map[string]Linter{ + case1: exp1, + case2: exp2, + case3: exp3, + } + + tempDir := t.TempDir() + defer os.RemoveAll(tempDir) + for k, v := range cs { + f := path.Join("./", "configtest_ut.yaml") + err := os.WriteFile(f, []byte(k), 0o666) + assert.NoError(t, err) + + res, er := NewConfig(f) + assert.NoError(t, er) + assert.EqualValues(t, v, res) + } } diff --git a/go.mod b/go.mod index 2be9ff79..646cbbd2 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 github.com/qiniu/x v1.13.2 github.com/sirupsen/logrus v1.9.0 + github.com/stretchr/testify v1.8.3 k8s.io/test-infra v0.0.0-20231205233654-937dbc605e95 sigs.k8s.io/yaml v1.4.0 ) @@ -15,17 +16,20 @@ require ( require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-github/v56 v56.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.13.0 // indirect github.com/prometheus/client_model v0.3.0 // indirect github.com/prometheus/common v0.37.0 // indirect github.com/prometheus/procfs v0.8.0 // indirect golang.org/x/sys v0.8.0 // indirect google.golang.org/protobuf v1.30.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/apimachinery v0.26.5 // indirect k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect ) diff --git a/go.sum b/go.sum index a4664e45..d6f97610 100644 --- a/go.sum +++ b/go.sum @@ -119,7 +119,6 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github/v56 v56.0.0 h1:TysL7dMa/r7wsQi44BjqlwaHvwlFlqkK8CtBWCX3gb4= github.com/google/go-github/v56 v56.0.0/go.mod h1:D8cdcX98YWJvi7TLo7zM4/h8ZTx6u6fwGEkCdisopo0= github.com/google/go-github/v57 v57.0.0 h1:L+Y3UPTY8ALM8x+TV0lg+IEBI+upibemtBD8Q9u7zHs= @@ -161,7 +160,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -173,7 +171,6 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -491,7 +488,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=