Skip to content

Commit 8e52909

Browse files
feat(config): check min version in GetLinter, GetFormatter
1 parent 396f6b1 commit 8e52909

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

config/config.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,20 @@ func WriteConfToFile(outFilePath string, conf *lint.Config) (retErr error) {
155155
return enc.Encode(conf)
156156
}
157157

158-
func checkVersion(verInfo string) error {
159-
if verInfo == "" {
158+
func checkVersion(versionNo string) error {
159+
if versionNo == "" {
160160
return errors.New("version is empty")
161161
}
162-
if !semver.IsValid(verInfo) {
162+
if !semver.IsValid(versionNo) {
163163
return errors.New("invalid version should be in semver format")
164164
}
165-
return nil
165+
return checkIfMinVersion(versionNo)
166+
}
167+
168+
func checkIfMinVersion(versionNo string) error {
169+
cmp := semver.Compare(Version(), versionNo)
170+
if cmp != -1 {
171+
return nil
172+
}
173+
return fmt.Errorf("min version required is %s. you have %s.\nupgrade commitlint", versionNo, Version())
166174
}

config/lint.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package config
33
import (
44
"fmt"
55

6-
"golang.org/x/mod/semver"
7-
86
"github.com/conventionalcommit/commitlint/lint"
97
)
108

119
// GetLinter returns Linter for given confFilePath
1210
func GetLinter(conf *lint.Config) (*lint.Linter, error) {
13-
if !checkIfMinVersion(conf) {
14-
return nil, fmt.Errorf("min version required is %s. you have %s. \nupgrade commitlint", conf.Version, Version())
11+
err := checkIfMinVersion(conf.Version)
12+
if err != nil {
13+
return nil, err
1514
}
1615

1716
rules, err := GetEnabledRules(conf)
@@ -23,6 +22,11 @@ func GetLinter(conf *lint.Config) (*lint.Linter, error) {
2322

2423
// GetFormatter returns the formatter as defined in conf
2524
func GetFormatter(conf *lint.Config) (lint.Formatter, error) {
25+
err := checkIfMinVersion(conf.Version)
26+
if err != nil {
27+
return nil, err
28+
}
29+
2630
format, ok := globalRegistry.GetFormatter(conf.Formatter)
2731
if !ok {
2832
return nil, fmt.Errorf("config error: '%s' formatter not found", conf.Formatter)
@@ -55,7 +59,3 @@ func GetEnabledRules(conf *lint.Config) ([]lint.Rule, error) {
5559

5660
return enabledRules, nil
5761
}
58-
59-
func checkIfMinVersion(conf *lint.Config) bool {
60-
return semver.Compare(Version(), conf.Version) != -1
61-
}

0 commit comments

Comments
 (0)