Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f0fffde

Browse files
committedFeb 6, 2022
refactor(config): remove WriteToFile, add WriteTo
1 parent f059ae5 commit f0fffde

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed
 

‎config/config.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
package config
33

44
import (
5-
"bufio"
65
"errors"
76
"fmt"
7+
"io"
88
"os"
99
"path/filepath"
1010

@@ -104,28 +104,8 @@ func LookupAndParse() (*lint.Config, error) {
104104
return conf, nil
105105
}
106106

107-
// WriteToFile util func to write config object to given file
108-
func WriteToFile(outFilePath string, conf *lint.Config) (retErr error) {
109-
outFilePath = filepath.Clean(outFilePath)
110-
f, err := os.Create(outFilePath)
111-
if err != nil {
112-
return err
113-
}
114-
defer func() {
115-
err := f.Close()
116-
if retErr == nil && err != nil {
117-
retErr = err
118-
}
119-
}()
120-
121-
w := bufio.NewWriter(f)
122-
defer func() {
123-
err := w.Flush()
124-
if retErr == nil && err != nil {
125-
retErr = err
126-
}
127-
}()
128-
107+
// WriteTo writes config in yaml format to given io.Writer
108+
func WriteTo(w io.Writer, conf *lint.Config) (retErr error) {
129109
enc := yaml.NewEncoder(w)
130110
defer func() {
131111
err := enc.Close()

‎internal/cmd/config.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
package cmd
22

33
import (
4+
"bufio"
45
"os"
56
"path/filepath"
67

78
"github.com/conventionalcommit/commitlint/config"
89
)
910

1011
// configCreate is the callback function for create config command
11-
func configCreate(fileName string, isReplace bool) error {
12-
defConf := config.Default()
12+
func configCreate(fileName string, isReplace bool) (retErr error) {
1313
outPath := filepath.Join(".", fileName)
1414
// if config file already exists skip creating or overwriting it
1515
if _, err := os.Stat(outPath); !os.IsNotExist(err) {
1616
if !isReplace {
1717
return errConfigExist
1818
}
1919
}
20-
return config.WriteToFile(outPath, defConf)
20+
21+
outFilePath := filepath.Clean(outPath)
22+
f, err := os.Create(outFilePath)
23+
if err != nil {
24+
return err
25+
}
26+
defer func() {
27+
err := f.Close()
28+
if retErr == nil && err != nil {
29+
retErr = err
30+
}
31+
}()
32+
33+
w := bufio.NewWriter(f)
34+
defer func() {
35+
err := w.Flush()
36+
if retErr == nil && err != nil {
37+
retErr = err
38+
}
39+
}()
40+
41+
defConf := config.Default()
42+
return config.WriteTo(w, defConf)
2143
}
2244

2345
// configCheck is the callback function for check/verify command

0 commit comments

Comments
 (0)
Please sign in to comment.