From 9697a8360be2f219fde336f85ed4d315c750974c Mon Sep 17 00:00:00 2001 From: Vyacheslav Pryimak Date: Thu, 4 Aug 2022 15:06:17 +0300 Subject: [PATCH] apply rules to directories --- go.mod | 5 +++-- go.sum | 10 ++++++---- reviser/dir.go | 16 ++++++++++++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 4f9ab21..58cb5c2 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,9 @@ go 1.18 require ( github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.6.1 - golang.org/x/mod v0.5.1 - golang.org/x/tools v0.1.8 + golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e + golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 + golang.org/x/tools v0.1.10 ) require ( diff --git a/go.sum b/go.sum index 3b8b940..cb9b6c8 100644 --- a/go.sum +++ b/go.sum @@ -13,12 +13,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= -golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= +golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA= +golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= +golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o= +golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/tools v0.1.8 h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w= -golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= +golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20= +golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/reviser/dir.go b/reviser/dir.go index 3dcc8cb..9f3aa93 100644 --- a/reviser/dir.go +++ b/reviser/dir.go @@ -2,10 +2,13 @@ package reviser import ( "io/fs" + "io/ioutil" + "log" "os" "path/filepath" "github.com/pkg/errors" + "golang.org/x/exp/slices" ) const ( @@ -13,6 +16,10 @@ const ( recursivePath = "./..." ) +var ( + currentPaths = []string{".", "." + string(filepath.Separator)} +) + var ( ErrPathIsNotDir = errors.New("path is not a directory") ) @@ -52,17 +59,22 @@ func (d *SourceDir) walk(options ...SourceFileOption) fs.WalkDirFunc { return filepath.SkipDir } if isGoFile(path) && !dirEntry.IsDir() { - _, _, err := NewSourceFile(d.projectName, path).Fix(options...) + content, hasChange, err := NewSourceFile(d.projectName, path).Fix(options...) if err != nil { return errors.WithStack(err) } + if hasChange { + if err := ioutil.WriteFile(path, content, 0644); err != nil { + log.Fatalf("failed to write fixed result to file(%s): %+v", path, errors.WithStack(err)) + } + } } return nil } } func IsDir(path string) (string, bool) { - if path == recursivePath { + if path == recursivePath || slices.Contains(currentPaths, path) { var err error path, err = os.Getwd() if err != nil {