From 19ab67d5abd54cfc5c465983aa4143c014a50118 Mon Sep 17 00:00:00 2001 From: Justin Buchanan Date: Sun, 8 Jul 2018 13:37:12 -0700 Subject: [PATCH] Minor refactoring + goimports --- config.go | 3 ++- formatter.go | 3 ++- stylize.go | 35 +++++++++++++++++++---------------- stylize_test.go | 3 ++- term_util.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ util.go | 43 +++---------------------------------------- 6 files changed, 72 insertions(+), 59 deletions(-) create mode 100644 term_util.go diff --git a/config.go b/config.go index 51b1a54..67c61ba 100644 --- a/config.go +++ b/config.go @@ -1,8 +1,9 @@ package main import ( - "gopkg.in/yaml.v2" "io/ioutil" + + "gopkg.in/yaml.v2" ) // This type defines the structure of the yml config file for stylize. diff --git a/formatter.go b/formatter.go index cc0143f..e9eed52 100644 --- a/formatter.go +++ b/formatter.go @@ -2,12 +2,13 @@ package main import ( "bytes" - "github.com/pmezard/go-difflib/difflib" "io" "io/ioutil" "log" "os" "path/filepath" + + "github.com/pmezard/go-difflib/difflib" ) // Common interface for all formatters. diff --git a/stylize.go b/stylize.go index cfd0490..688bc7e 100644 --- a/stylize.go +++ b/stylize.go @@ -19,8 +19,6 @@ package main import ( "bytes" "fmt" - "github.com/bradfitz/slice" - "github.com/pkg/errors" "io" "log" "os" @@ -29,6 +27,9 @@ import ( "strings" "sync" "syscall" + + "github.com/bradfitz/slice" + "github.com/pkg/errors" ) type FormattingResult struct { @@ -53,12 +54,16 @@ func IterateAllFiles(rootDir string, exclude []string) <-chan string { relPath, _ := filepath.Rel(rootDir, path) + // TODO: specify vcs excludes elsewhere? exclude = append(exclude, ".git", ".hg") - if fi.IsDir() && fileIsExcluded(relPath, exclude) { + isExcluded := fileIsExcluded(relPath, exclude) + + // Skip the entire directory + if fi.IsDir() && isExcluded { return filepath.SkipDir } - if !fileIsExcluded(relPath, exclude) && !fi.IsDir() { + if !isExcluded && !fi.IsDir() { files <- relPath } @@ -130,9 +135,7 @@ func runFormatter(rootDir, file string, formatter Formatter, formatterArgs []str result.FormatNeeded, result.Error = FormatInPlaceAndCheckModified(formatter, formatterArgs, filepath.Join(rootDir, file)) } else { result.Patch, result.Error = CreatePatchWithFormatter(formatter, formatterArgs, rootDir, file) - if len(result.Patch) > 0 { - result.FormatNeeded = true - } + result.FormatNeeded = len(result.Patch) > 0 } return result @@ -147,21 +150,21 @@ func CollectPatch(results <-chan FormattingResult, patchOut io.Writer) <-chan Fo defer close(resultsOut) // collect relevant results from the input channel and forward them to the output - var resultList []FormattingResult + var formattedResults []FormattingResult for r := range results { if r.Error == nil && r.FormatNeeded { - resultList = append(resultList, r) + formattedResults = append(formattedResults, r) } resultsOut <- r } // sort to ensure patches are consistent - slice.Sort(resultList, func(i, j int) bool { - return resultList[i].FilePath < resultList[j].FilePath + slice.Sort(formattedResults, func(i, j int) bool { + return formattedResults[i].FilePath < formattedResults[j].FilePath }) // write patch output - for _, r := range resultList { + for _, r := range formattedResults { patchOut.Write([]byte(r.Patch + "\n")) } }() @@ -174,7 +177,7 @@ func RunFormattersOnFiles(formatters map[string]Formatter, formatterArgs map[str semaphore := make(chan int, parallelism) var wg sync.WaitGroup - resultOut := make(chan FormattingResult) + resulstOut := make(chan FormattingResult) go func() { for file := range fileChan { ext := filepath.Ext(file) @@ -190,17 +193,17 @@ func RunFormattersOnFiles(formatters map[string]Formatter, formatterArgs map[str wg.Add(1) semaphore <- 0 // acquire go func(file string, formatter Formatter, inPlace bool) { - resultOut <- runFormatter(rootDir, file, formatter, formatterArgs[formatter.Name()], inPlace) + resulstOut <- runFormatter(rootDir, file, formatter, formatterArgs[formatter.Name()], inPlace) wg.Done() <-semaphore // release }(file, formatter, inPlace) } wg.Wait() - close(resultOut) + close(resulstOut) }() - return resultOut + return resulstOut } type RunStats struct { diff --git a/stylize_test.go b/stylize_test.go index 08cef72..552b2b9 100644 --- a/stylize_test.go +++ b/stylize_test.go @@ -3,7 +3,6 @@ package main import ( "bytes" "flag" - "github.com/pmezard/go-difflib/difflib" "io" "io/ioutil" "os" @@ -12,6 +11,8 @@ import ( "path/filepath" "strings" "testing" + + "github.com/pmezard/go-difflib/difflib" ) var ( diff --git a/term_util.go b/term_util.go new file mode 100644 index 0000000..cf081c7 --- /dev/null +++ b/term_util.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "os" + "strings" + "syscall" + "unsafe" + + "golang.org/x/crypto/ssh/terminal" +) + +type winsize struct { + Row uint16 + Col uint16 + Xpixel uint16 + Ypixel uint16 +} + +func isTerminal(fd *os.File) bool { + return terminal.IsTerminal(int(fd.Fd())) +} + +func getTermWidth(scall uintptr) uint { + ws := &winsize{} + retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL, + uintptr(scall), + uintptr(syscall.TIOCGWINSZ), + uintptr(unsafe.Pointer(ws))) + + if int(retCode) == -1 { + panic(errno) + } + return uint(ws.Col) +} + +func padToWidth(text string, w int) string { + spCount := w - len(text) + if spCount < 0 { + spCount = 0 + } + sp := strings.Repeat(" ", spCount) + return fmt.Sprintf("%s%s", text, sp) +} diff --git a/util.go b/util.go index ba53049..bdb8251 100644 --- a/util.go +++ b/util.go @@ -2,17 +2,13 @@ package main import ( "bytes" - "fmt" - "github.com/danwakefield/fnmatch" - "github.com/pkg/errors" - "golang.org/x/crypto/ssh/terminal" "io" "log" - "os" "os/exec" "strings" - "syscall" - "unsafe" + + "github.com/danwakefield/fnmatch" + "github.com/pkg/errors" ) // Helper method that wraps exec.Command @@ -31,39 +27,6 @@ func runIOCommand(args []string, in io.Reader, out io.Writer) error { return nil } -type winsize struct { - Row uint16 - Col uint16 - Xpixel uint16 - Ypixel uint16 -} - -func isTerminal(fd *os.File) bool { - return terminal.IsTerminal(int(fd.Fd())) -} - -func getTermWidth(scall uintptr) uint { - ws := &winsize{} - retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL, - uintptr(scall), - uintptr(syscall.TIOCGWINSZ), - uintptr(unsafe.Pointer(ws))) - - if int(retCode) == -1 { - panic(errno) - } - return uint(ws.Col) -} - -func padToWidth(text string, w int) string { - spCount := w - len(text) - if spCount < 0 { - spCount = 0 - } - sp := strings.Repeat(" ", spCount) - return fmt.Sprintf("%s%s", text, sp) -} - // Returns a list of files that have changed since the given git diffbase. These // file paths are relative to the root of the git repo, not necessarily the // given rootDir.