Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Evsyukov Denis <denis.evsyukov@flant.com>
  • Loading branch information
juev committed Sep 12, 2024
1 parent 6d5f926 commit 9135bb8
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 38 deletions.
14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ func main() {
dirs := parseFlags()
logger.InfoF("Dirs: %v", dirs)

cfg := config.NewDefault()
err := config.NewLoader(cfg).Load()
cfg, err := config.NewDefault()
logger.CheckErr(err)

mng := manager.NewManager(dirs)
// for i := range mng.Modules {
// logger.InfoF("module[%d]: %s", i, mng.Modules[i])
// }
logger.InfoF("Config: %#v", cfg)
mng := manager.NewManager(dirs, cfg)
for i := range mng.Modules {
logger.InfoF("module[%d]: %s", i, mng.Modules[i])
}

result := mng.Run()
fmt.Printf("%v\n", result.ConvertToError())
fmt.Printf("%v\n", result)
logger.CheckErr(result.ConvertToError())
}
10 changes: 8 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ type Config struct {
Linters Linters `mapstructure:"linters"`
}

func NewDefault() *Config {
return &Config{
func NewDefault() (*Config, error) {
cfg := &Config{
LintersSettings: defaultLintersSettings,
}

if err := NewLoader(cfg).Load(); err != nil {
return nil, err
}

return cfg, nil
}

func (c *Config) Validate() error {
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ func (s *CustomLinterSettings) Validate() error {

return nil
}

type OpenAPISettings struct {
Exclude []string `mapstructure:"exclude"`
UseBuiltinExclusions bool `mapstructure:"use-builtin-exclusions"`
IgnoreTest bool `mapstructure:"ignore-test"`
}
2 changes: 1 addition & 1 deletion pkg/linters/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (*OpenAPI) Run(_ context.Context, m *module.Module) (errors.LintRuleErrorsL
}

func (*OpenAPI) Name() string {
return "OpenAPI Linter"
return "openapi"
}

func (*OpenAPI) Desc() string {
Expand Down
10 changes: 2 additions & 8 deletions pkg/linters/openapi/validators/ha_and_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,15 @@ func NewHAValidator() HAValidator {
}

func (HAValidator) Run(file, absoluteKey string, value any) error {
//values, ok := value.(map[string]any)
//if !ok {
// logger.ErrorF("Possible Bug? Have to be a map. Type: %s, Value: %s, File: %s, Key: %s", reflect.TypeOf(value), value, file, absoluteKey)
// return nil
//}

m := make(map[any]any)
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Map {
logger.ErrorF("Possible Bug? Have to be a map. Type: %s, Value: %s, File: %s", reflect.TypeOf(value), value, file)
return fmt.Errorf("not map")
}
for _, key := range rv.MapKeys() {
value := rv.MapIndex(key)
m[key.Interface()] = value.Interface()
v := rv.MapIndex(key)
m[key.Interface()] = v.Interface()
}

for key := range m {
Expand Down
10 changes: 2 additions & 8 deletions pkg/linters/openapi/validators/keys_name_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,10 @@ func (KeyNameValidator) Run(file, _ string, value any) error {
return fmt.Errorf("not map")
}
for _, key := range rv.MapKeys() {
value := rv.MapIndex(key)
m[key.Interface()] = value.Interface()
v := rv.MapIndex(key)
m[key.Interface()] = v.Interface()
}

//object, ok := value.(map[any]any)
//if !ok {
// logger.ErrorF("Possible Bug? Have to be a map. Type: %s, Value: %s, File: %s", reflect.TypeOf(value), value, file)
// return nil
//}

err := checkMapForBannedKey(m, bannedNames)
if err != nil {
return fmt.Errorf("%s file validation error: wrong property: %w", file, err)
Expand Down
57 changes: 51 additions & 6 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/mitchellh/go-homedir"

"github.com/deckhouse/d8-lint/pkg/config"
"github.com/deckhouse/d8-lint/pkg/errors"
"github.com/deckhouse/d8-lint/pkg/linters/openapi"
"github.com/deckhouse/d8-lint/pkg/logger"
Expand All @@ -19,37 +21,48 @@ const (
)

type Manager struct {
cfg *config.Config
Linters LinterList
Modules module.ModuleList

lintersMap map[string]Linter

Check failure on line 28 in pkg/manager/manager.go

View workflow job for this annotation

GitHub Actions / golangci-lint

field `lintersMap` is unused (unused)
}

func NewManager(dirs []string) *Manager {
m := &Manager{}
func NewManager(dirs []string, cfg *config.Config) *Manager {
m := &Manager{
cfg: cfg,
}

// TODO check enabled linters
m.Linters = []Linter{
openapi.New(),
}

lintersMap := make(map[string]Linter)
for _, linter := range m.Linters {
lintersMap[strings.ToLower(linter.Name())] = linter
}

var paths []string

for i := range dirs {
dir, err := homedir.Expand(dirs[i])
if err != nil {
logger.ErrorF("Failed to expand home dir: %v", err)
continue
}
result, err := getModulePaths(dir)
if err != nil {
continue
logger.ErrorF("Error getting module paths: %v", err)
}
paths = append(paths, result...)
}

for i := range paths {
mdl := module.NewModule(paths[i])
if mdl.Chart == nil {
continue
}
//if mdl.Chart == nil {

Check failure on line 63 in pkg/manager/manager.go

View workflow job for this annotation

GitHub Actions / golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
// continue
//}
m.Modules = append(m.Modules, mdl)
}

Expand Down Expand Up @@ -116,3 +129,35 @@ func getModulePaths(modulesDir string) ([]string, error) {

return chartDirs, nil
}

func (m *Manager) getEnabledLinters() LinterList {

Check failure on line 133 in pkg/manager/manager.go

View workflow job for this annotation

GitHub Actions / golangci-lint

func `(*Manager).getEnabledLinters` is unused (unused)
resultLintersSet := map[string]Linter{}
switch {
case m.cfg.Linters.DisableAll:
// no default linters
case m.cfg.Linters.EnableAll:
resultLintersSet = m.lintersMap
default:
resultLintersSet = m.lintersMap
}

for _, name := range m.cfg.Linters.Enable {
if m.lintersMap[name] == nil {
continue
}
resultLintersSet[name] = m.lintersMap[name]
}

for _, name := range m.cfg.Linters.Disable {
if m.lintersMap[name] == nil {
continue
}
delete(resultLintersSet, name)
}
result := make(LinterList, 0)
for _, linter := range resultLintersSet {
result = append(result, linter)
}

return result
}
12 changes: 6 additions & 6 deletions pkg/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"gopkg.in/yaml.v3"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
)

const (
Expand All @@ -35,11 +34,12 @@ func NewModule(path string) *Module {
Path: path,
}

var err error
module.Chart, err = loader.Load(path)
if err != nil {
return &Module{}
}
//var err error

Check failure on line 37 in pkg/module/module.go

View workflow job for this annotation

GitHub Actions / golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
//module.Chart, err = loader.Load(path)
//if err != nil {
// logger.ErrorF("Failed to load module %s: %s\n", module.Name, err)
// return &Module{}
//}

return module
}
Expand Down

0 comments on commit 9135bb8

Please sign in to comment.