diff --git a/.impignore b/.impignore new file mode 100644 index 0000000..c1fdea2 --- /dev/null +++ b/.impignore @@ -0,0 +1,2 @@ +# Ignore vendor files +vendor/* \ No newline at end of file diff --git a/README.md b/README.md index 5ac3625..1ab2749 100644 --- a/README.md +++ b/README.md @@ -16,5 +16,11 @@ import ( imp includes three flags: * `--write` will write out the formatting rather than printing -* `--exclude` can be used multiple times to set directories to exclude -* `--verbose` will print out extended information \ No newline at end of file +* `--imp-ignore` reads a `.impignore` file for globs to include/exclude (`.impignore` by default) +* `--verbose` will print out extended information + + +### `.impignore` format + +The `.impignore` file follows a [globber format](https://gitea.com/jolheiser/globber), +which closely resembles a traditional `.gitignore` file. \ No newline at end of file diff --git a/go.mod b/go.mod index 7c3d043..52071f9 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module go.jolheiser.com/imp go 1.14 require ( + gitea.com/jolheiser/globber v0.0.1 github.com/gobuffalo/here v0.6.2 github.com/urfave/cli/v2 v2.2.0 go.jolheiser.com/beaver v1.0.2 go.jolheiser.com/regexp v0.1.1 - golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8 ) diff --git a/go.sum b/go.sum index 636dfc1..5bb2ead 100644 --- a/go.sum +++ b/go.sum @@ -1,48 +1,40 @@ +gitea.com/jolheiser/globber v0.0.1 h1:BVoc4Ml9N3ssix5Xy32gpQOO1jnAuzMsYc5h/LP7/jA= +gitea.com/jolheiser/globber v0.0.1/go.mod h1:vr1VkO/biaYgeGj8tRD6SMAWhsqINKFYl9en3YbpQvc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gobuffalo/here v0.6.2 h1:ZtCqC7F9ou3moLbYfHM1Tj+gwHGgWhjyRjVjsir9BE0= github.com/gobuffalo/here v0.6.2/go.mod h1:D75Sq0p2BVHdgQu3vCRsXbg85rx943V19urJpqAVWjI= +github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= +github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA= github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= -github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.jolheiser.com/beaver v1.0.2 h1:KA2D6iO8MQhZi1nZYi/Chak/f1Cxfrs6b1XO623+Khk= go.jolheiser.com/beaver v1.0.2/go.mod h1:7X4F5+XOGSC3LejTShoBdqtRCnPWcnRgmYGmG3EKW8g= go.jolheiser.com/regexp v0.1.1 h1:nMCzilEL/oIcZJwnRT4bb+FNxaeAFBdqSjq7rgoNSGg= go.jolheiser.com/regexp v0.1.1/go.mod h1:58uCpYxGy/DbqVRuo6oU93kh3B3GeyK5eJznQvy2N6c= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8 h1:BMFHd4OFnFtWX46Xj4DN6vvT1btiBxyq+s0orYBqcQY= -golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/imp.go b/imp.go index 3a44fb5..e37cbf1 100644 --- a/imp.go +++ b/imp.go @@ -9,6 +9,7 @@ import ( "sort" "strings" + "gitea.com/jolheiser/globber" "github.com/gobuffalo/here" "github.com/urfave/cli/v2" "go.jolheiser.com/beaver" @@ -33,6 +34,14 @@ func runImp(ctx *cli.Context) error { module = info.Module.Path beaver.Debugf("Current module: %s", module) + globs, err := globber.ParseFile(ctx.String("imp-ignore")) + if err != nil { + if !os.IsNotExist(err) { + return err + } + globs = globber.New() + } + var failed bool diffColor := color.New(color.FgGreen, color.BgHiBlack) if err := filepath.Walk(".", func(walkPath string, walkInfo os.FileInfo, walkErr error) error { @@ -42,7 +51,7 @@ func runImp(ctx *cli.Context) error { walkPath = filepath.ToSlash(walkPath) - if s := checkSkip(walkPath, walkInfo, ctx.StringSlice("exclude")); s != CHECK { + if s := checkSkip(walkPath, walkInfo, globs); s != CHECK { if s == SKIPDIR { return filepath.SkipDir } @@ -97,7 +106,7 @@ func runImp(ctx *cli.Context) error { return nil } -func checkSkip(walkPath string, walkInfo os.FileInfo, excludePaths []string) walkStatus { +func checkSkip(walkPath string, walkInfo os.FileInfo, globs *globber.GlobSet) walkStatus { // Skip current directory if strings.EqualFold(walkPath, ".") { @@ -124,13 +133,13 @@ func checkSkip(walkPath string, walkInfo os.FileInfo, excludePaths []string) wal return SKIP } - // Skip excluded directories - for _, e := range excludePaths { - if strings.HasPrefix(walkPath, e) { - beaver.Debugf("Skipping file %s because of exclude path %s", walkPath, e) - return SKIP - } + // Skip included (ignored) globs + i, e := globs.Explain(walkPath) + if len(i) > 0 && len(e) == 0 { + beaver.Debugf("Skipping file %s because of .impignore rule %s", walkPath, i[0].Pattern) + return SKIP } + return CHECK } @@ -156,7 +165,7 @@ func splitImports(imports []string) []importList { var name string if !strings.HasPrefix(i, `"`) { - parts := strings.Split(i, " ") + parts := strings.SplitN(i, " ", 2) name = parts[0] i = parts[1] } diff --git a/main.go b/main.go index 741ed97..d76d593 100644 --- a/main.go +++ b/main.go @@ -17,16 +17,17 @@ func main() { Aliases: []string{"w"}, Usage: "Write the re-ordered imports instead of just printing them", }, - &cli.StringSliceFlag{ - Name: "exclude", - Aliases: []string{"e"}, - Usage: "A list of directories to exclude", - }, &cli.BoolFlag{ Name: "verbose", Aliases: []string{"v"}, Usage: "Print more information", }, + &cli.StringFlag{ + Name: "imp-ignore", + Aliases: []string{"i"}, + Usage: "Path to a .impignore file for globs", + Value: ".impignore", + }, } app.Action = runImp