Skip to content

Commit

Permalink
refactor: reorganize code base
Browse files Browse the repository at this point in the history
* rename allRules to defaultRules
* rename allFormatters to defaultFormatters
* rename GetRules to GetEnabledRules
* move config utils to config.go
* move linter utils to lint.go
* update test files
  • Loading branch information
muthukrishnan24 committed Sep 22, 2021
1 parent e007af6 commit 63f2581
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 176 deletions.
137 changes: 72 additions & 65 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,22 @@
package config

import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/conventionalcommit/commitlint/formatter"
"gopkg.in/yaml.v3"

"github.com/conventionalcommit/commitlint/lint"
"github.com/conventionalcommit/commitlint/rule"
)

const (
// ConfFileName represent config file name
ConfFileName = "commitlint.yaml"
)

var allFormatters = []lint.Formatter{
&formatter.DefaultFormatter{},
&formatter.JSONFormatter{},
}

var allRules = []lint.Rule{
&rule.BodyMinLenRule{},
&rule.BodyMaxLenRule{},

&rule.FooterMinLenRule{},
&rule.FooterMaxLenRule{},

&rule.HeadMaxLenRule{},
&rule.HeadMinLenRule{},

&rule.TypeEnumRule{},
&rule.ScopeEnumRule{},

&rule.BodyMaxLineLenRule{},
&rule.FooterMaxLineLenRule{},

&rule.TypeCharsetRule{},
&rule.ScopeCharsetRule{},

&rule.TypeMaxLenRule{},
&rule.ScopeMaxLenRule{},
&rule.DescriptionMaxLenRule{},

&rule.TypeMinLenRule{},
&rule.ScopeMinLenRule{},
&rule.DescriptionMinLenRule{},
}

// GetConfig returns conf
func GetConfig(flagConfPath string) (*lint.Config, error) {
confFilePath, useDefault, err := GetConfigPath(flagConfPath)
Expand Down Expand Up @@ -93,48 +62,86 @@ func GetConfigPath(confFilePath string) (string, bool, error) {
return filepath.Clean(confFilePath), false, nil
}

// GetFormatter returns the formatter as defined in conf
func GetFormatter(c *lint.Config) (lint.Formatter, error) {
for _, f := range allFormatters {
if f.Name() == c.Formatter {
return f, nil
}
// Parse parse Config from given file
func Parse(confPath string) (*lint.Config, error) {
confBytes, err := os.ReadFile(confPath)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%s formatter not found", c.Formatter)

conf := &lint.Config{}
err = yaml.Unmarshal(confBytes, conf)
if err != nil {
return nil, err
}

err = Validate(conf)
if err != nil {
return nil, fmt.Errorf("config error: %w", err)
}
return conf, nil
}

// GetRules forms Rule object for rules which are enabled in config
func GetRules(conf *lint.Config) ([]lint.Rule, error) {
// rules lookup map
rulesMap := map[string]lint.Rule{}
for _, r := range allRules {
rulesMap[r.Name()] = r
// Validate parses Config from given data
func Validate(conf *lint.Config) error {
if conf.Formatter == "" {
return errors.New("formatter is empty")
}

enabledRules := make([]lint.Rule, 0, len(conf.Rules))
// Check Severity Level of rule config
for ruleName, r := range conf.Rules {
switch r.Severity {
case lint.SeverityError:
case lint.SeverityWarn:
default:
return fmt.Errorf("unknown severity level '%s' for rule '%s'", r.Severity, ruleName)
}
}

return nil
}

for ruleName, ruleConfig := range conf.Rules {
r, ok := rulesMap[ruleName]
if !ok {
return nil, fmt.Errorf("unknown rule: %s", ruleName)
// DefaultConfToFile writes default config to given file
func DefaultConfToFile(isOnlyEnabled bool) error {
outPath := filepath.Join(".", filepath.Clean(ConfFileName))
if isOnlyEnabled {
confClone := &lint.Config{
Formatter: defConf.Formatter,
Rules: map[string]lint.RuleConfig{},
}
if ruleConfig.Enabled {
err := r.Apply(ruleConfig.Argument, ruleConfig.Flags)
if err != nil {
return nil, err

for ruleName, r := range defConf.Rules {
if r.Enabled {
confClone.Rules[ruleName] = r
}
enabledRules = append(enabledRules, r)
}
}

return enabledRules, nil
return WriteConfToFile(outPath, confClone)
}
return WriteConfToFile(outPath, defConf)
}

// GetLinter returns Linter for given confFilePath
func GetLinter(conf *lint.Config) (*lint.Linter, error) {
rules, err := GetRules(conf)
// WriteConfToFile util func to write config object to given file
func WriteConfToFile(outFilePath string, conf *lint.Config) (retErr error) {
file, err := os.Create(outFilePath)
if err != nil {
return nil, err
return err
}
return lint.NewLinter(conf, rules)
defer func() {
err1 := file.Close()
if retErr == nil && err1 != nil {
retErr = err1
}
}()

w := bufio.NewWriter(file)
defer func() {
err1 := w.Flush()
if retErr == nil && err1 != nil {
retErr = err1
}
}()

enc := yaml.NewEncoder(w)
return enc.Encode(conf)
}
8 changes: 4 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package config

import "testing"

func TestAllRules(t *testing.T) {
func TestDefaultRules(t *testing.T) {
var m = make(map[string]struct{})
for _, r := range allRules {
for _, r := range defaultRules {
_, ok := m[r.Name()]
if ok {
t.Errorf("error: %s rule name already exists", r.Name())
Expand All @@ -13,9 +13,9 @@ func TestAllRules(t *testing.T) {
}
}

func TestAllFormatters(t *testing.T) {
func TestDefaultFormatters(t *testing.T) {
var m = make(map[string]struct{})
for _, r := range allFormatters {
for _, r := range defaultFormatters {
_, ok := m[r.Name()]
if ok {
t.Errorf("error: %s formatter name already exists", r.Name())
Expand Down
33 changes: 33 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,36 @@ var defConf = &lint.Config{
},
},
}

var defaultFormatters = []lint.Formatter{
&formatter.DefaultFormatter{},
&formatter.JSONFormatter{},
}

var defaultRules = []lint.Rule{
&rule.BodyMinLenRule{},
&rule.BodyMaxLenRule{},

&rule.FooterMinLenRule{},
&rule.FooterMaxLenRule{},

&rule.HeadMaxLenRule{},
&rule.HeadMinLenRule{},

&rule.TypeEnumRule{},
&rule.ScopeEnumRule{},

&rule.BodyMaxLineLenRule{},
&rule.FooterMaxLineLenRule{},

&rule.TypeCharsetRule{},
&rule.ScopeCharsetRule{},

&rule.TypeMaxLenRule{},
&rule.ScopeMaxLenRule{},
&rule.DescriptionMaxLenRule{},

&rule.TypeMinLenRule{},
&rule.ScopeMinLenRule{},
&rule.DescriptionMinLenRule{},
}
4 changes: 2 additions & 2 deletions config/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func TestDefaultLint(t *testing.T) {
}

func TestDefaultConf(t *testing.T) {
if len(allRules) != len(defConf.Rules) {
t.Error("default conf does not have all rules", len(allRules), len(defConf.Rules))
if len(defaultRules) != len(defConf.Rules) {
t.Error("default conf does not have all rules", len(defaultRules), len(defConf.Rules))
return
}
}
53 changes: 53 additions & 0 deletions config/lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package config

import (
"fmt"

"github.com/conventionalcommit/commitlint/lint"
)

// GetLinter returns Linter for given confFilePath
func GetLinter(conf *lint.Config) (*lint.Linter, error) {
rules, err := GetEnabledRules(conf)
if err != nil {
return nil, err
}
return lint.NewLinter(conf, rules)
}

// GetFormatter returns the formatter as defined in conf
func GetFormatter(c *lint.Config) (lint.Formatter, error) {
for _, f := range defaultFormatters {
if f.Name() == c.Formatter {
return f, nil
}
}
return nil, fmt.Errorf("%s formatter not found", c.Formatter)
}

// GetEnabledRules forms Rule object for rules which are enabled in config
func GetEnabledRules(conf *lint.Config) ([]lint.Rule, error) {
// rules lookup map
rulesMap := map[string]lint.Rule{}
for _, r := range defaultRules {
rulesMap[r.Name()] = r
}

enabledRules := make([]lint.Rule, 0, len(conf.Rules))

for ruleName, ruleConfig := range conf.Rules {
r, ok := rulesMap[ruleName]
if !ok {
return nil, fmt.Errorf("unknown rule: %s", ruleName)
}
if ruleConfig.Enabled {
err := r.Apply(ruleConfig.Argument, ruleConfig.Flags)
if err != nil {
return nil, err
}
enabledRules = append(enabledRules, r)
}
}

return enabledRules, nil
}
49 changes: 0 additions & 49 deletions config/parse.go

This file was deleted.

Loading

0 comments on commit 63f2581

Please sign in to comment.