Skip to content

Commit

Permalink
Minor refactoring + goimports
Browse files Browse the repository at this point in the history
  • Loading branch information
justbuchanan committed Jul 8, 2018
1 parent faedac0 commit 19ab67d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 59 deletions.
3 changes: 2 additions & 1 deletion 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.
Expand Down
3 changes: 2 additions & 1 deletion formatter.go
Expand Up @@ -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.
Expand Down
35 changes: 19 additions & 16 deletions stylize.go
Expand Up @@ -19,8 +19,6 @@ package main
import (
"bytes"
"fmt"
"github.com/bradfitz/slice"
"github.com/pkg/errors"
"io"
"log"
"os"
Expand All @@ -29,6 +27,9 @@ import (
"strings"
"sync"
"syscall"

"github.com/bradfitz/slice"
"github.com/pkg/errors"
)

type FormattingResult struct {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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"))
}
}()
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion stylize_test.go
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"flag"
"github.com/pmezard/go-difflib/difflib"
"io"
"io/ioutil"
"os"
Expand All @@ -12,6 +11,8 @@ import (
"path/filepath"
"strings"
"testing"

"github.com/pmezard/go-difflib/difflib"
)

var (
Expand Down
44 changes: 44 additions & 0 deletions 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)
}
43 changes: 3 additions & 40 deletions util.go
Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 19ab67d

Please sign in to comment.