Skip to content

Commit

Permalink
apply rules to directories
Browse files Browse the repository at this point in the history
  • Loading branch information
incu6us committed Aug 4, 2022
1 parent 10c64a0 commit 9697a83
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
16 changes: 14 additions & 2 deletions reviser/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ package reviser

import (
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/pkg/errors"
"golang.org/x/exp/slices"
)

const (
goExtension = ".go"
recursivePath = "./..."
)

var (
currentPaths = []string{".", "." + string(filepath.Separator)}
)

var (
ErrPathIsNotDir = errors.New("path is not a directory")
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9697a83

Please sign in to comment.