feat: remove extra whitespace, clean up deps, and add optional gofumpt formatting

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main v0.0.4
jolheiser 2023-07-16 14:49:44 -05:00
parent 08f9985010
commit f17818f834
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
6 changed files with 110 additions and 38 deletions

View File

@ -13,10 +13,13 @@ import (
) )
``` ```
imp includes three flags: imp includes five flags:
* `--write` will write out the formatting rather than printing * `--write` will write out the formatting rather than printing
* `--ignore` reads a `.impignore` file for globs to include/exclude (`.impignore` by default) * `--ignore` reads a `.impignore` file for globs to include/exclude (`.impignore` by default)
* `--stdin` will read from stdin and print to stdout
* `--gofumpt` will also format using [gofumpt](https://github.com/mvdan/gofumpt)
* `--gofumpt-extra` will include [gofumpt extra rules](https://github.com/mvdan/gofumpt#extra-rules-behind--extra)
### `.impignore` format ### `.impignore` format

View File

@ -42,11 +42,11 @@ func (i importBlock) String() string {
var decl strings.Builder var decl strings.Builder
decl.WriteString(strings.Join(importItemSlice(i.Stdlib), "\n\t")) decl.WriteString(strings.Join(importItemSlice(i.Stdlib), "\n\t"))
if len(i.Module) > 0 { if len(i.Stdlib) != 0 && len(i.Module) > 0 {
decl.WriteString("\n\n\t") decl.WriteString("\n\n\t")
} }
decl.WriteString(strings.Join(importItemSlice(i.Module), "\n\t")) decl.WriteString(strings.Join(importItemSlice(i.Module), "\n\t"))
if len(i.Other) > 0 { if len(i.Module) != 0 && len(i.Other) > 0 {
decl.WriteString("\n\n\t") decl.WriteString("\n\n\t")
} }
decl.WriteString(strings.Join(importItemSlice(i.Other), "\n\t")) decl.WriteString(strings.Join(importItemSlice(i.Other), "\n\t"))

8
go.mod
View File

@ -1,14 +1,16 @@
module gitea.com/jolheiser/imp module go.jolheiser.com/imp
go 1.18 go 1.18
require ( require (
gitea.com/jolheiser/globber v0.0.1 gitea.com/jolheiser/globber v0.0.1
github.com/gobuffalo/here v0.6.6
github.com/matryer/is v1.4.0 github.com/matryer/is v1.4.0
mvdan.cc/gofumpt v0.5.0
) )
require ( require (
github.com/gobwas/glob v0.2.3 // indirect github.com/gobwas/glob v0.2.3 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect github.com/google/go-cmp v0.5.9 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/tools v0.8.0 // indirect
) )

38
go.sum
View File

@ -1,31 +1,19 @@
gitea.com/jolheiser/globber v0.0.1 h1:BVoc4Ml9N3ssix5Xy32gpQOO1jnAuzMsYc5h/LP7/jA= gitea.com/jolheiser/globber v0.0.1 h1:BVoc4Ml9N3ssix5Xy32gpQOO1jnAuzMsYc5h/LP7/jA=
gitea.com/jolheiser/globber v0.0.1/go.mod h1:vr1VkO/biaYgeGj8tRD6SMAWhsqINKFYl9en3YbpQvc= gitea.com/jolheiser/globber v0.0.1/go.mod h1:vr1VkO/biaYgeGj8tRD6SMAWhsqINKFYl9en3YbpQvc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
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.6 h1:/o+jfSwe36pKQ577grsXGoMYql/zheiGwg1XFo3CBJU=
github.com/gobuffalo/here v0.6.6/go.mod h1:C4JZL5PsXWKzP/CAchaIzuUWlaae6CaAiMYQ0ieY62M=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= 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/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= mvdan.cc/gofumpt v0.5.0 h1:0EQ+Z56k8tXjj/6TQD25BFNKQXpCvT0rnansIc7Ug5E=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= mvdan.cc/gofumpt v0.5.0/go.mod h1:HBeVDtMKRZpXyxFciAirzdKklDlGu8aAy1wEbH5Y9js=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

30
imp.go
View File

@ -9,18 +9,17 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"gitea.com/jolheiser/imp/format" "go.jolheiser.com/imp/format"
"gitea.com/jolheiser/globber" "gitea.com/jolheiser/globber"
"github.com/gobuffalo/here" gofumptFormat "mvdan.cc/gofumpt/format"
) )
func runImp(root, ignore string, write bool) error { func runImp(root, ignore string, write, gofumpt, gofumptExtra bool) error {
info, err := here.Current() mod, err := modInfo()
if err != nil { if err != nil {
return err return err
} }
module := info.Module.Path
globs, err := globber.ParseFile(ignore) globs, err := globber.ParseFile(ignore)
if err != nil { if err != nil {
if !errors.Is(err, fs.ErrNotExist) { if !errors.Is(err, fs.ErrNotExist) {
@ -48,7 +47,8 @@ func runImp(root, ignore string, write bool) error {
if err != nil { if err != nil {
return fmt.Errorf("could not read file %q: %w", walkPath, err) return fmt.Errorf("could not read file %q: %w", walkPath, err)
} }
formatted, err := format.Source(data, module)
formatted, err := impFormat(data, mod, gofumpt, gofumptExtra)
if err != nil { if err != nil {
return fmt.Errorf("could not format file %q: %w", walkPath, err) return fmt.Errorf("could not format file %q: %w", walkPath, err)
} }
@ -73,6 +73,24 @@ func runImp(root, ignore string, write bool) error {
return nil return nil
} }
func impFormat(src []byte, mod module, gofumpt, gofumptExtra bool) ([]byte, error) {
formatted, err := format.Source(src, mod.Path)
if err != nil {
return src, err
}
if gofumpt {
formatted, err = gofumptFormat.Source(formatted, gofumptFormat.Options{
LangVersion: mod.GoVersion,
ModulePath: mod.Path,
ExtraRules: gofumptExtra,
})
if err != nil {
return src, err
}
}
return formatted, nil
}
func checkSkip(walkPath string, walkInfo os.FileInfo, globs *globber.GlobSet) walkStatus { func checkSkip(walkPath string, walkInfo os.FileInfo, globs *globber.GlobSet) walkStatus {
// Skip current directory // Skip current directory
if strings.EqualFold(walkPath, ".") { if strings.EqualFold(walkPath, ".") {

63
main.go
View File

@ -1,9 +1,12 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io"
"os" "os"
"os/exec"
) )
var Version = "develop" var Version = "develop"
@ -14,17 +17,75 @@ func main() {
fs.BoolVar(writeFlag, "w", *writeFlag, "--write") fs.BoolVar(writeFlag, "w", *writeFlag, "--write")
ignoreFlag := fs.String("ignore", ".impignore", "Path to .impignore file") ignoreFlag := fs.String("ignore", ".impignore", "Path to .impignore file")
fs.StringVar(ignoreFlag, "i", *ignoreFlag, "--ignore") fs.StringVar(ignoreFlag, "i", *ignoreFlag, "--ignore")
gofumptFlag := fs.Bool("gofumpt", false, "Also format using gofumpt")
gofumptExtraFlag := fs.Bool("gofumpt-extra", false, "Also use extra rules with gofumpt")
stdinFlag := fs.Bool("stdin", false, "Format stdin")
if err := fs.Parse(os.Args[1:]); err != nil { if err := fs.Parse(os.Args[1:]); err != nil {
fmt.Println(err) fmt.Println(err)
} }
if *stdinFlag {
mod, err := modInfo()
if err != nil {
fmt.Println(err)
return
}
src, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Println(err)
return
}
formatted, err := impFormat(src, mod, *gofumptFlag, *gofumptExtraFlag)
if err != nil {
fmt.Println(err)
}
fmt.Print(formatted)
}
root := "." root := "."
if fs.NArg() > 0 { if fs.NArg() > 0 {
root = fs.Arg(0) root = fs.Arg(0)
} }
if err := runImp(root, *ignoreFlag, *writeFlag); err != nil { if err := runImp(root, *ignoreFlag, *writeFlag, *gofumptFlag, *gofumptExtraFlag); err != nil {
fmt.Println(err) fmt.Println(err)
} }
} }
type module struct {
Path string
GoVersion string
}
func (m *module) UnmarshalJSON(data []byte) error {
type mod struct {
Module struct {
Path string
}
Go string
}
var mo mod
if err := json.Unmarshal(data, &mo); err != nil {
return err
}
*m = module{
Path: mo.Module.Path,
GoVersion: mo.Go,
}
return nil
}
func modInfo() (module, error) {
cmd := exec.Command("go", "mod", "edit", "--json")
out, err := cmd.Output()
if err != nil {
return module{}, err
}
var m module
if err := json.Unmarshal(out, &m); err != nil {
return m, err
}
return m, nil
}