Skip to content

Commit 2b1b8f8

Browse files
feat(config): add GetDefaultConf, remove DefaultConfToFile
1 parent 7021e1e commit 2b1b8f8

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

cmd/callback.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"path/filepath"
56

67
"github.com/urfave/cli/v2"
78

@@ -46,7 +47,9 @@ func HookCreate(confPath string, isReplace bool) error {
4647

4748
// ConfigCreate is the callback function for create config command
4849
func ConfigCreate(onlyEnabled bool) error {
49-
return config.DefaultConfToFile(onlyEnabled)
50+
defConf := config.GetDefaultConf(onlyEnabled)
51+
outPath := filepath.Join(".", config.DefaultFile)
52+
return config.WriteConfToFile(outPath, defConf)
5053
}
5154

5255
// ConfigCheck is the callback function for check/verify command

config/config.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
)
1515

1616
const (
17-
// ConfFileName represent config file name
18-
ConfFileName = "commitlint.yaml"
17+
// DefaultFile represent default config file name
18+
DefaultFile = "commitlint.yaml"
1919
)
2020

2121
// GetConfig returns parses config file, validate it and returns config instance
2222
func GetConfig(confPath string) (*lint.Config, error) {
23-
confFilePath, useDefault, err := GetConfigPath(confPath)
23+
confFilePath, useDefault, err := getConfigPath(confPath)
2424
if err != nil {
2525
return nil, err
2626
}
@@ -41,19 +41,19 @@ func GetConfig(confPath string) (*lint.Config, error) {
4141
return conf, nil
4242
}
4343

44-
// GetConfigPath returns config file path following below order
44+
// getConfigPath returns config file path following below order
4545
// 1. commitlint.yaml in current directory
4646
// 2. confFilePath parameter
4747
// 3. use default config
48-
func GetConfigPath(confFilePath string) (string, bool, error) {
48+
func getConfigPath(confFilePath string) (confPath string, isDefault bool, retErr error) {
4949
// get current directory
5050
currentDir, err := os.Getwd()
5151
if err != nil {
5252
return "", false, err
5353
}
5454

5555
// check if conf file exists in current directory
56-
currentDirConf := filepath.Join(currentDir, ConfFileName)
56+
currentDirConf := filepath.Join(currentDir, DefaultFile)
5757
if _, err1 := os.Stat(currentDirConf); !os.IsNotExist(err1) {
5858
return currentDirConf, false, nil
5959
}
@@ -112,26 +112,6 @@ func Validate(conf *lint.Config) error {
112112
return nil
113113
}
114114

115-
// DefaultConfToFile writes default config to given file
116-
func DefaultConfToFile(isOnlyEnabled bool) error {
117-
outPath := filepath.Join(".", filepath.Clean(ConfFileName))
118-
if !isOnlyEnabled {
119-
return WriteConfToFile(outPath, defConf)
120-
}
121-
122-
confClone := &lint.Config{
123-
Formatter: defConf.Formatter,
124-
Rules: map[string]lint.RuleConfig{},
125-
}
126-
127-
for ruleName, r := range defConf.Rules {
128-
if r.Enabled {
129-
confClone.Rules[ruleName] = r
130-
}
131-
}
132-
return WriteConfToFile(outPath, confClone)
133-
}
134-
135115
// WriteConfToFile util func to write config object to given file
136116
func WriteConfToFile(outFilePath string, conf *lint.Config) (retErr error) {
137117
file, err := os.Create(outFilePath)

config/default.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,22 @@ var defaultRules = []lint.Rule{
175175
&rule.ScopeMinLenRule{},
176176
&rule.DescriptionMinLenRule{},
177177
}
178+
179+
// GetDefaultConf writes default config to given file
180+
func GetDefaultConf(onlyEnabled bool) *lint.Config {
181+
if !onlyEnabled {
182+
return defConf
183+
}
184+
185+
confClone := &lint.Config{
186+
Formatter: defConf.Formatter,
187+
Rules: map[string]lint.RuleConfig{},
188+
}
189+
190+
for ruleName, r := range defConf.Rules {
191+
if r.Enabled {
192+
confClone.Rules[ruleName] = r
193+
}
194+
}
195+
return confClone
196+
}

0 commit comments

Comments
 (0)