Skip to content

Commit

Permalink
Move formatters into their own directory/package
Browse files Browse the repository at this point in the history
  • Loading branch information
justbuchanan committed Jul 15, 2018
1 parent 087974c commit e1521a5
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 33 deletions.
15 changes: 8 additions & 7 deletions formatter.go
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"

"github.com/justbuchanan/stylize/formatters"
"github.com/pmezard/go-difflib/difflib"
)

Expand Down Expand Up @@ -132,12 +133,12 @@ var (
// If multiple formatters apply to the same file type, their order here
// determines precedence. Lower index = higher priority.
FormatterRegistry = []Formatter{
&ClangFormatter{},
&UncrustifyFormatter{},
&PrettierFormatter{},
&YapfFormatter{},
&GofmtFormatter{},
&BuildifierFormatter{},
&RustfmtFormatter{},
&formatters.ClangFormatter{},
&formatters.UncrustifyFormatter{},
&formatters.PrettierFormatter{},
&formatters.YapfFormatter{},
&formatters.GofmtFormatter{},
&formatters.BuildifierFormatter{},
&formatters.RustfmtFormatter{},
}
)
2 changes: 1 addition & 1 deletion buildifier_formatter.go → formatters/buildifier.go
@@ -1,4 +1,4 @@
package main
package formatters

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion clang_formatter.go → formatters/clang.go
@@ -1,4 +1,4 @@
package main
package formatters

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion gofmt_formatter.go → formatters/gofmt.go
@@ -1,4 +1,4 @@
package main
package formatters

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion prettier_formatter.go → formatters/prettier.go
@@ -1,4 +1,4 @@
package main
package formatters

// https://github.com/prettier/prettier

Expand Down
2 changes: 1 addition & 1 deletion rustfmt_formatter.go → formatters/rustfmt.go
@@ -1,4 +1,4 @@
package main
package formatters

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion uncrustify_formatter.go → formatters/uncrustify.go
@@ -1,4 +1,4 @@
package main
package formatters

import (
"io"
Expand Down
27 changes: 27 additions & 0 deletions formatters/util.go
@@ -0,0 +1,27 @@
package formatters

import (
"bytes"
"io"
"log"
"os/exec"
"strings"

"github.com/pkg/errors"
)

// Helper method that wraps exec.Command
func runIOCommand(args []string, in io.Reader, out io.Writer) error {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = in
cmd.Stdout = out
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Print("Error running command: ", strings.Join(args, " "))
return errors.Wrap(err, stderr.String())
}

return nil
}
2 changes: 1 addition & 1 deletion yapf_formatter.go → formatters/yapf.go
@@ -1,4 +1,4 @@
package main
package formatters

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -41,7 +41,7 @@ Stylize currently has support for:
- [uncrustify](https://github.com/uncrustify/uncrustify)
- [rustfmt](https://github.com/rust-lang-nursery/rustfmt)

Other formatters can easily be added. See the \*\_formatter.go files as examples.
Other formatters can easily be added. See the files in the 'formatters' directory as examples.

## Python version

Expand Down
18 changes: 0 additions & 18 deletions util.go
Expand Up @@ -2,31 +2,13 @@ package main

import (
"bytes"
"io"
"log"
"os/exec"
"strings"

"github.com/danwakefield/fnmatch"
"github.com/pkg/errors"
)

// Helper method that wraps exec.Command
func runIOCommand(args []string, in io.Reader, out io.Writer) error {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = in
cmd.Stdout = out
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Print("Error running command: ", strings.Join(args, " "))
return errors.Wrap(err, stderr.String())
}

return nil
}

// 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 e1521a5

Please sign in to comment.