-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconfig.go
More file actions
53 lines (45 loc) · 1.31 KB
/
config.go
File metadata and controls
53 lines (45 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cmd
import (
"bufio"
"os"
"path/filepath"
"github.com/conventionalcommit/commitlint/config"
)
// configCreate is the callback function for create config command
func configCreate(fileName string, isReplace bool) (retErr error) {
outPath := filepath.Join(".", fileName)
// if config file already exists skip creating or overwriting it
if _, err := os.Stat(outPath); !os.IsNotExist(err) {
if !isReplace {
return handleError(errConfigExist, "Config file already exists")
}
}
outFilePath := filepath.Clean(outPath)
f, err := os.Create(outFilePath)
if handleError(err, "Failed to create config file") != nil {
return err
}
defer func() {
err := f.Close()
if retErr == nil && err != nil {
retErr = handleError(err, "Failed to close config file")
}
}()
w := bufio.NewWriter(f)
defer func() {
err := w.Flush()
if retErr == nil && err != nil {
retErr = handleError(err, "Failed to flush writer")
}
}()
defConf := config.NewDefault()
return handleError(config.WriteTo(w, defConf), "Failed to write config to file")
}
// configCheck is the callback function for check/verify command
func configCheck(confPath string) []error {
conf, err := config.Parse(confPath)
if handleError(err, "Failed to parse configuration file") != nil {
return []error{err}
}
return config.Validate(conf)
}