Skip to content

Commit

Permalink
feat: support config default open
Browse files Browse the repository at this point in the history
  • Loading branch information
xwen-winnie committed Feb 4, 2024
1 parent 5499d30 commit aee9125
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 19 deletions.
29 changes: 23 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 9 additions & 2 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
87 changes: 80 additions & 7 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,92 @@
package config

import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"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"},
},
}

case3 := ``
exp3 := map[string]map[string]Linter{
"qbox": {
"staticcheck": {Enable: &defaultTrue},
"govet": {Enable: &defaultTrue},
"luacheck": {Enable: &defaultTrue},
},
}

for k, c := range repoConfig {
fmt.Printf("%v: %v \n", k, c)
cs := map[string]map[string]map[string]Linter{
case1: exp1,
case2: exp2,
case3: exp3,
}

//dir, _ := os.MkdirTemp("", "linter_tmp")
//defer os.RemoveAll(dir)

//// 创建一个子目录作为临时目录
tempDir := filepath.Join("./", "linter_tmp")
err := os.MkdirAll(tempDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tempDir)

for k, v := range cs {
f := path.Join(tempDir, "configtest_ut.yaml")

file, err := os.OpenFile(f, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = file.Write([]byte(k))
if err != nil {
log.Fatal(err)
}
//err := os.WriteFile(f, []byte(k), 0x666)
assert.NoError(t, err)

res, err := NewConfig(f)
assert.NoError(t, err)

assert.EqualValues(t, v, res)
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@ 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
)

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
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit aee9125

Please sign in to comment.