Skip to content

Commit

Permalink
Remove usages of deprecated github.com/pkg/errors (#109)
Browse files Browse the repository at this point in the history
Fixes #105.

Co-authored-by: Vyacheslav Pryimak <[email protected]>
  • Loading branch information
alexandear and incu6us authored Feb 27, 2023
1 parent 3532668 commit 4dc9812
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 95 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ import (

"bytes"

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

Expand All @@ -123,9 +123,9 @@ package testdata
import (
"bytes"
"log"
"github.com/pkg/errors"

"golang.org/x/exp/slices"

"github.com/incu6us/goimports-reviser/testdata/innderpkg"
)
```
Expand All @@ -148,7 +148,7 @@ package testdata // goimports-reviser/testdata

import (
"fmt" //fmt package
"github.com/pkg/errors" //custom package
"golang.org/x/exp/slices" //custom package
"github.com/incu6us/goimports-reviser/pkg" // this is a company package which is not a part of the project, but is a part of your organization
"goimports-reviser/pkg"
)
Expand All @@ -161,7 +161,7 @@ package testdata // goimports-reviser/testdata
import (
"fmt" // fmt package

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

"github.com/incu6us/goimports-reviser/pkg" // this is a company package which is not a part of the project, but is a part of your organization

Expand Down
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
// "bytes"
//
// "github.com/pkg/errors"
// "golang.org/x/exp/slices"
// )
//
// Output:
Expand All @@ -21,7 +21,7 @@
// "bytes"
// "log"
//
// "github.com/pkg/errors"
// "golang.org/x/exp/slices"
//
// "github.com/incu6us/goimports-reviser/testdata/innderpkg"
// )
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/incu6us/goimports-reviser/v3
go 1.18

require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ 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/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=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
26 changes: 12 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"path/filepath"
"strings"

"github.com/pkg/errors"

"github.com/incu6us/goimports-reviser/v3/helper"
"github.com/incu6us/goimports-reviser/v3/reviser"
)
Expand Down Expand Up @@ -263,14 +261,14 @@ func main() {
if _, ok := reviser.IsDir(originPath); ok {
err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive).Fix(options...)
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to fix directory: %+v\n", err)
}
return
}

originPath, err = filepath.Abs(originPath)
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to get abs path: %+v\n", err)
}

var formattedOutput []byte
Expand All @@ -280,11 +278,11 @@ func main() {

u, err := user.Current()
if err != nil {
log.Fatalf("%+v\n", errors.WithStack(err))
log.Fatalf("Failed to get current user: %+v\n", err)
}
cacheDir := path.Join(u.HomeDir, ".cache", "goimports-reviser")
if err = os.MkdirAll(cacheDir, os.ModePerm); err != nil {
log.Fatalf("%+v\n", errors.WithStack(err))
log.Fatalf("Failed to create cache directory: %+v\n", err)
}
cacheFile := path.Join(cacheDir, hex.EncodeToString(hash[:]))

Expand All @@ -303,30 +301,30 @@ func main() {
}
formattedOutput, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...)
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to fix file: %+v\n", 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))
log.Fatalf("Failed to create cache file: %+v\n", 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))
log.Fatalf("Failed file truncate: %+v\n", err)
}
if _, err = file.Seek(0, 0); err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed file seek: %+v\n", err)
}
if _, err = file.WriteString(fileHashHex); err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to write file hash: %+v\n", err)
}
} else {
formattedOutput, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...)
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to fix file: %+v\n", err)
}
}

Expand All @@ -349,7 +347,7 @@ func resultPostProcess(hasChange bool, deprecatedMessagesCh chan string, originF
}

if err := os.WriteFile(originFilePath, formattedOutput, 0644); err != nil {
log.Fatalf("failed to write fixed result to file(%s): %+v", originFilePath, errors.WithStack(err))
log.Fatalf("failed to write fixed result to file(%s): %+v\n", originFilePath, err)
}
if *listFileName {
fmt.Println(originFilePath)
Expand All @@ -370,7 +368,7 @@ func validateRequiredParam(filePath string) error {
stat, _ := os.Stdin.Stat()
if stat.Mode()&os.ModeNamedPipe == 0 {
// no data on stdin
return errors.Errorf("-%s should be set", filePathArg)
return fmt.Errorf("-%s should be set", filePathArg)
}
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/astutil/astutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ func TestLoadPackageDeps(t *testing.T) {
filename: "testdata.go",
},
want: map[string]string{
"fmt": "fmt",
"github.com/pkg/errors": "errors",
"fmt": "fmt",
"golang.org/x/exp/slices": "slices",
},
wantErr: false,
},
Expand All @@ -198,8 +198,8 @@ func TestLoadPackageDeps(t *testing.T) {
filename: "testdata_with_deprecated_build_tag.go",
},
want: map[string]string{
"fmt": "fmt",
"github.com/pkg/errors": "errors",
"fmt": "fmt",
"golang.org/x/exp/slices": "slices",
},
wantErr: false,
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/astutil/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package testdata
import (
"fmt"

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

func main() {
fmt.Printf("%s", errors.New("some error here"))
fmt.Println(slices.IsSorted([]int{1, 2, 3}))
}
4 changes: 2 additions & 2 deletions pkg/astutil/testdata/testdata_with_deprecated_build_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package testdata
import (
"fmt"

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

func main() {
fmt.Printf("%s", errors.New("some error here"))
fmt.Println(slices.IsSorted([]int{1, 2, 3}))
}
13 changes: 6 additions & 7 deletions pkg/std/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
)

Expand Down Expand Up @@ -42,12 +41,12 @@ func main() {
tpl := template.New("tpl")
tpl, err := tpl.Parse(fileTemplate)
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to parse template: %+v\n", err)
}

packageList, err := packages.Load(nil, "std")
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to load packages: %+v\n", err)
}

for _, experimentalPackage := range staticPackageList {
Expand All @@ -57,22 +56,22 @@ func main() {
}

if err := tpl.Execute(w, packageList); err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to execute template: %+v\n", err)
}

data, err := format.Source(w.Bytes())
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to format source: %+v\n", err)
}

currentDir, err := os.Getwd()
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to get current directory: %+v\n", err)
}

filePath := filepath.Join(filepath.Join(currentDir, "pkg/std"), fileName)
log.Printf("file path to be updated: %s", filePath)
if err := os.WriteFile(filePath, data, 0644); err != nil {
log.Fatalf("%+v", errors.WithStack(err))
log.Fatalf("Failed to write file: %+v\n", err)
}
}
9 changes: 5 additions & 4 deletions reviser/dir.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package reviser

import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"

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

Expand Down Expand Up @@ -46,7 +47,7 @@ func (d *SourceDir) Fix(options ...SourceFileOption) error {

err := filepath.WalkDir(d.dir, d.walk(options...))
if err != nil {
return errors.WithStack(err)
return fmt.Errorf("failed to walk dif: %w", err)
}

return nil
Expand All @@ -60,11 +61,11 @@ func (d *SourceDir) walk(options ...SourceFileOption) fs.WalkDirFunc {
if isGoFile(path) && !dirEntry.IsDir() {
content, hasChange, err := NewSourceFile(d.projectName, path).Fix(options...)
if err != nil {
return errors.WithStack(err)
return fmt.Errorf("failed to fix: %w", err)
}
if hasChange {
if err := os.WriteFile(path, content, 0644); err != nil {
log.Fatalf("failed to write fixed result to file(%s): %+v", path, errors.WithStack(err))
log.Fatalf("failed to write fixed result to file(%s): %+v\n", path, err)
}
}
}
Expand Down
Loading

0 comments on commit 4dc9812

Please sign in to comment.