Skip to content

Commit

Permalink
refactor(config): remove WriteToFile, add WriteTo
Browse files Browse the repository at this point in the history
  • Loading branch information
muthukrishnan24 committed Feb 6, 2022
1 parent f059ae5 commit f0fffde
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
26 changes: 3 additions & 23 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
package config

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

Expand Down Expand Up @@ -104,28 +104,8 @@ func LookupAndParse() (*lint.Config, error) {
return conf, nil
}

// WriteToFile util func to write config object to given file
func WriteToFile(outFilePath string, conf *lint.Config) (retErr error) {
outFilePath = filepath.Clean(outFilePath)
f, err := os.Create(outFilePath)
if err != nil {
return err
}
defer func() {
err := f.Close()
if retErr == nil && err != nil {
retErr = err
}
}()

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

// WriteTo writes config in yaml format to given io.Writer
func WriteTo(w io.Writer, conf *lint.Config) (retErr error) {
enc := yaml.NewEncoder(w)
defer func() {
err := enc.Close()
Expand Down
28 changes: 25 additions & 3 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
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) error {
defConf := config.Default()
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 errConfigExist
}
}
return config.WriteToFile(outPath, defConf)

outFilePath := filepath.Clean(outPath)
f, err := os.Create(outFilePath)
if err != nil {
return err
}
defer func() {
err := f.Close()
if retErr == nil && err != nil {
retErr = err
}
}()

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

defConf := config.Default()
return config.WriteTo(w, defConf)
}

// configCheck is the callback function for check/verify command
Expand Down

0 comments on commit f0fffde

Please sign in to comment.