Skip to content

Commit

Permalink
add -use-cache parameter, use cache to improve performance (#83)
Browse files Browse the repository at this point in the history
* enable batch generate

* add errgroup dep

* feat: add file cache to improve performance

* fix: remove errgroup dep
  • Loading branch information
devenami authored Sep 28, 2022
1 parent 9697a83 commit e490d8d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Usage of goimports-reviser:
Set alias for versioned package names, like 'github.com/go-pg/pg/v9'. In this case import will be set as 'pg "github.com/go-pg/pg/v9"'. Optional parameter.
-set-exit-status
set the exit status to 1 if a change is needed/made. Optional parameter.
-use-cache
Use cache to improve performance. Optional parameter.
```

## Install
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/incu6us/goimports-reviser/v3
go 1.18

require (
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
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/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
72 changes: 71 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package main

import (
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"

"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"

"github.com/incu6us/goimports-reviser/v3/helper"
Expand All @@ -26,6 +31,7 @@ const (
listDiffFileNameArg = "list-diff"
setExitStatusArg = "set-exit-status"
recursiveArg = "recursive"
useCacheArg = "use-cache"

// Deprecated options
localArg = "local"
Expand All @@ -46,6 +52,7 @@ var (
listFileName *bool
setExitStatus *bool
isRecursive *bool
isUseCache *bool
)

var (
Expand Down Expand Up @@ -140,6 +147,12 @@ Optional parameter.`,
"Apply rules recursively if target is a directory. In case of ./... execution will be recursively applied by default. Optional parameter.",
)

isUseCache = flag.Bool(
useCacheArg,
false,
"Use cache to improve performance. Optional parameter.",
)

if Tag != "" {
shouldShowVersion = flag.Bool(
versionArg,
Expand Down Expand Up @@ -244,11 +257,68 @@ func main() {
return
}

formattedOutput, hasChange, err := reviser.NewSourceFile(originProjectName, originPath).Fix(options...)
originPath, err = filepath.Abs(originPath)
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
}

var formattedOutput []byte
var hasChange bool
if *isUseCache {
hash := md5.Sum([]byte(originPath))

var home string
if home, err = homedir.Dir(); err != nil {
log.Fatalf("%+v\n", errors.WithStack(err))
}
cacheDir := path.Join(home, ".cache", "goimports-reviser")
if err = os.MkdirAll(cacheDir, os.ModePerm); err != nil {
log.Fatalf("%+v\n", errors.WithStack(err))
}
cacheFile := path.Join(cacheDir, hex.EncodeToString(hash[:]))

var cacheContent, fileContent []byte
if cacheContent, err = ioutil.ReadFile(cacheFile); err == nil {
// compare file content hash
var fileHashHex string
if fileContent, err = ioutil.ReadFile(originPath); err == nil {
fileHash := md5.Sum(fileContent)
fileHashHex = hex.EncodeToString(fileHash[:])
}
if string(cacheContent) == fileHashHex {
// point to cache
return
}
}
formattedOutput, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...)
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
}
fileHash := md5.Sum(formattedOutput)
fileHashHex := hex.EncodeToString(fileHash[:])
if fileInfo, err := os.Stat(cacheFile); err != nil || fileInfo.IsDir() {
if _, err = os.Create(cacheFile); err != nil {
log.Fatalf("%+v", errors.WithStack(err))
}
}
file, _ := os.OpenFile(cacheFile, os.O_RDWR, os.ModePerm)
defer file.Close()
if err = file.Truncate(0); err != nil {
log.Fatalf("%+v", errors.WithStack(err))
}
if _, err = file.Seek(0, 0); err != nil {
log.Fatalf("%+v", errors.WithStack(err))
}
if _, err = file.WriteString(fileHashHex); err != nil {
log.Fatalf("%+v", errors.WithStack(err))
}
} else {
formattedOutput, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...)
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
}
}

resultPostProcess(hasChange, deprecatedMessagesCh, originPath, formattedOutput)
}

Expand Down

0 comments on commit e490d8d

Please sign in to comment.