From 4dc9812c21ef717ec59269b0e75ca9ca3b2175e2 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Mon, 27 Feb 2023 14:30:37 +0200 Subject: [PATCH] Remove usages of deprecated github.com/pkg/errors (#109) Fixes #105. Co-authored-by: Vyacheslav Pryimak --- README.md | 12 +-- doc.go | 4 +- go.mod | 1 - go.sum | 2 - main.go | 26 +++-- pkg/astutil/astutil_test.go | 8 +- pkg/astutil/testdata/testdata.go | 4 +- .../testdata_with_deprecated_build_tag.go | 4 +- pkg/std/gen/gen.go | 13 ++- reviser/dir.go | 9 +- reviser/file_test.go | 102 +++++++++--------- 11 files changed, 90 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index f084592..666d944 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) ``` @@ -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" ) ``` @@ -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" ) @@ -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 diff --git a/doc.go b/doc.go index 581b008..1880c0d 100644 --- a/doc.go +++ b/doc.go @@ -12,7 +12,7 @@ // // "bytes" // -// "github.com/pkg/errors" +// "golang.org/x/exp/slices" // ) // // Output: @@ -21,7 +21,7 @@ // "bytes" // "log" // -// "github.com/pkg/errors" +// "golang.org/x/exp/slices" // // "github.com/incu6us/goimports-reviser/testdata/innderpkg" // ) diff --git a/go.mod b/go.mod index 58cb5c2..597e9a1 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index cb9b6c8..7ba34d2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index f170af7..2111067 100644 --- a/main.go +++ b/main.go @@ -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" ) @@ -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 @@ -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[:])) @@ -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) } } @@ -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) @@ -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 diff --git a/pkg/astutil/astutil_test.go b/pkg/astutil/astutil_test.go index e232266..6bfeb57 100644 --- a/pkg/astutil/astutil_test.go +++ b/pkg/astutil/astutil_test.go @@ -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, }, @@ -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, }, diff --git a/pkg/astutil/testdata/testdata.go b/pkg/astutil/testdata/testdata.go index 622bdb7..bf8c350 100644 --- a/pkg/astutil/testdata/testdata.go +++ b/pkg/astutil/testdata/testdata.go @@ -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})) } diff --git a/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go b/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go index e02157d..408d81c 100644 --- a/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go +++ b/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go @@ -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})) } diff --git a/pkg/std/gen/gen.go b/pkg/std/gen/gen.go index eecea11..728a249 100644 --- a/pkg/std/gen/gen.go +++ b/pkg/std/gen/gen.go @@ -10,7 +10,6 @@ import ( "os" "path/filepath" - "github.com/pkg/errors" "golang.org/x/tools/go/packages" ) @@ -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 { @@ -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) } } diff --git a/reviser/dir.go b/reviser/dir.go index 64d4fa6..e73ba6c 100644 --- a/reviser/dir.go +++ b/reviser/dir.go @@ -1,12 +1,13 @@ package reviser import ( + "errors" + "fmt" "io/fs" "log" "os" "path/filepath" - "github.com/pkg/errors" "golang.org/x/exp/slices" ) @@ -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 @@ -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) } } } diff --git a/reviser/file_test.go b/reviser/file_test.go index d39337b..5b036d2 100644 --- a/reviser/file_test.go +++ b/reviser/file_test.go @@ -37,7 +37,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -49,7 +49,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" ) @@ -75,7 +75,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -88,7 +88,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" ) @@ -114,7 +114,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -127,7 +127,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" ) @@ -215,13 +215,13 @@ import ( projectName: "github.com/incu6us/goimports-reviser", filePath: "./testdata/example.go", fileContent: `package testdata - + import ( "log" "bytes" -"github.com/pkg/errors" +"golang.org/x/exp/slices" ) // nolint:gomnd @@ -233,7 +233,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -280,7 +280,7 @@ import ( import ( - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -289,7 +289,7 @@ import ( want: `package testdata import ( - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -427,8 +427,8 @@ import ( "fmt" _ "github.com/lib/pq" // configure database/sql with postgres driver - "github.com/pkg/errors" "go.uber.org/fx" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/pkg/somepkg" ) @@ -442,8 +442,8 @@ import ( "fmt" _ "github.com/lib/pq" // configure database/sql with postgres driver - "github.com/pkg/errors" "go.uber.org/fx" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/pkg/somepkg" ) @@ -617,7 +617,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -629,7 +629,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" ) @@ -654,7 +654,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -666,7 +666,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" ) @@ -691,7 +691,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -702,7 +702,7 @@ import ( import ( "github.com/incu6us/goimports-reviser/testdata/innderpkg" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "bytes" "log" @@ -759,7 +759,7 @@ func TestSourceFile_Fix_WithRemoveUnusedImports(t *testing.T) { import ( "fmt" //fmt package - "github.com/pkg/errors" //custom package + "golang.org/x/exp/slices" //custom package ) // nolint:gomnd @@ -792,7 +792,7 @@ func main() { import ( "fmt" //fmt package - p "github.com/pkg/errors" //p package + p "golang.org/x/exp/slices" //p package ) // nolint:gomnd @@ -825,7 +825,7 @@ func main() { import ( "fmt" //fmt package - _ "github.com/pkg/errors" //custom package + _ "golang.org/x/exp/slices" //custom package ) // nolint:gomnd @@ -839,7 +839,7 @@ func main(){ import ( "fmt" //fmt package - _ "github.com/pkg/errors" //custom package + _ "golang.org/x/exp/slices" //custom package ) // nolint:gomnd @@ -861,7 +861,7 @@ package testdata // test import ( "fmt" //fmt package - _ "github.com/pkg/errors" //custom package + _ "golang.org/x/exp/slices" //custom package ) // nolint:gomnd @@ -877,7 +877,7 @@ package testdata import ( "fmt" //fmt package - _ "github.com/pkg/errors" //custom package + _ "golang.org/x/exp/slices" //custom package ) // nolint:gomnd @@ -975,7 +975,7 @@ func main() { import "C" import( "fmt" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "strconv" ) @@ -1074,14 +1074,14 @@ func main() { wantErr: false, }, { - name: "success with github.com/pkg/errors", + name: "success with golang.org/x/exp/slices", args: args{ projectName: "github.com/incu6us/goimports-reviser", filePath: "./testdata/example.go", fileContent: `package testdata import( "fmt" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "strconv" ) @@ -1096,7 +1096,7 @@ import ( "fmt" "strconv" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) func main() { @@ -1122,7 +1122,7 @@ func main() { import "C" import( "fmt" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "strconv" ) @@ -1144,7 +1144,7 @@ import ( "fmt" "strconv" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) func main() { @@ -1200,7 +1200,7 @@ func TestSourceFile_Fix_WithLocalPackagePrefixes(t *testing.T) { import ( "fmt" //fmt package - "github.com/pkg/errors" //custom package + "golang.org/x/exp/slices" //custom package "github.com/incu6us/goimports-reviser/pkg" "goimports-reviser/pkg" ) @@ -1221,7 +1221,7 @@ func main(){ import ( "fmt" //fmt package - "github.com/pkg/errors" //custom package + "golang.org/x/exp/slices" //custom package "goimports-reviser/pkg" @@ -1251,7 +1251,7 @@ func main() { import ( "fmt" // fmt package - "github.com/pkg/errors" //custom package + "golang.org/x/exp/slices" //custom package "github.com/incu6us/goimports-reviser/pkg" "goimports-reviser/pkg" ) @@ -1266,7 +1266,7 @@ func main(){ import ( "fmt" // fmt package - "github.com/pkg/errors" //custom package + "golang.org/x/exp/slices" //custom package "github.com/incu6us/goimports-reviser/pkg" @@ -1507,7 +1507,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -1520,7 +1520,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" ) @@ -1546,7 +1546,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -1562,7 +1562,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -1586,7 +1586,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -1602,7 +1602,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -1631,7 +1631,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -1652,7 +1652,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -1681,7 +1681,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -1699,7 +1699,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" ) @@ -1726,7 +1726,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint:gomnd @@ -1740,7 +1740,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" ) @@ -1765,7 +1765,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // Code generated by some tool DO NOT EDIT. @@ -1778,7 +1778,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" ) @@ -1804,7 +1804,7 @@ import ( "bytes" - "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // Inner comment @@ -1820,7 +1820,7 @@ import ( "bytes" "log" - "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/incu6us/goimports-reviser/testdata/innderpkg" )