Skip to content

Commit

Permalink
refactor: replace pkg/errors by golang built-in errors (#2175)
Browse files Browse the repository at this point in the history
Signed-off-by: epsxy <[email protected]>
Co-authored-by: epsxy <[email protected]>
Co-authored-by: Carolyn Van Slyck <[email protected]>
  • Loading branch information
3 people authored Jun 22, 2022
1 parent ddefcea commit 730b039
Show file tree
Hide file tree
Showing 130 changed files with 740 additions and 674 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ and we will add you. **All** contributors belong here. 💯
* [Chioma Onyekpere](https://github.com/Simpcyclassy)
* [Hrittik Roy](https://github.com/hrittikhere)
* [Tanmay Chaudhry](https://github.com/tchaudhry91)
* [Kevin Barbour](https://github.com/kevinbarbour)
* [Kevin Barbour](https://github.com/kevinbarbour)
* [Epsxy](https://github.com/epsxy)
3 changes: 1 addition & 2 deletions cmd/porter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"get.porter.sh/porter/pkg/cli"
"get.porter.sh/porter/pkg/porter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -72,7 +71,7 @@ func main() {
defer func() {
// Capture panics and trace them
if panicErr := recover(); panicErr != nil {
log.Error(errors.New(fmt.Sprintf("%s", panicErr)),
log.Error(fmt.Errorf("%s", panicErr),
attribute.Bool("panic", true),
attribute.String("stackTrace", string(debug.Stack())))
log.EndSpan()
Expand Down
3 changes: 1 addition & 2 deletions docs/content/contribute/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ YOURNAME` that prints `Hello YOURNAME!`.
import (
"fmt"
"github.com/pkg/errors"
"errors"
)
// Define flags and arguments for `porter hello`.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ require (
github.com/osteele/liquid v1.3.0
github.com/pelletier/go-toml v1.9.4
github.com/pivotal/image-relocation v0.0.0-20191111101224-e94aff6df06c
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.5.1
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -174,6 +173,7 @@ require (
github.com/opencontainers/runc v1.1.2 // indirect
github.com/osteele/tuesday v1.0.3 // indirect
github.com/pierrec/lz4/v4 v4.0.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand Down
17 changes: 13 additions & 4 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/carolynvs/magex/xplat"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -411,7 +410,11 @@ func UseXBuildBinaries() error {
// Run `chmod +x -R bin`.
func SetBinExecutable() error {
err := chmodRecursive("bin", pkg.FileModeExecutable)
return errors.Wrap(err, "could not set +x on the test bin")
if err != nil {
return fmt.Errorf("could not set +x on the test bin: %w", err)
}

return nil
}

func chmodRecursive(name string, mode os.FileMode) error {
Expand Down Expand Up @@ -456,7 +459,10 @@ func Install() {
// Copy mixin binaries
mixinsDir := filepath.Join("bin", "mixins")
mixinsDirItems, err := ioutil.ReadDir(mixinsDir)
mgx.Must(errors.Wrap(err, "could not list mixins in bin"))
if err != nil {
mgx.Must(fmt.Errorf("could not list mixins in bin: %w", err))
}

for _, fi := range mixinsDirItems {
if !fi.IsDir() {
continue
Expand Down Expand Up @@ -490,7 +496,10 @@ func getPorterHome() string {
porterHome := os.Getenv("PORTER_HOME")
if porterHome == "" {
home, err := os.UserHomeDir()
mgx.Must(errors.Wrap(err, "could not determine home directory"))
if err != nil {
mgx.Must(fmt.Errorf("could not determine home directory: %w", err))
}

porterHome = filepath.Join(home, ".porter")
}
return porterHome
Expand Down
3 changes: 1 addition & 2 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"get.porter.sh/porter/pkg"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -74,7 +73,7 @@ func Execute(porterCommand []string, porterHome string, porterConfig string) (er
cmd.Stdout = stderr // send all non-bundle output to stderr
cmd.Stderr = stderr
if err = cmd.Run(); err != nil {
return errors.Wrap(err, "porter version check failed"), false
return fmt.Errorf("porter version check failed: %w", err), false
}

// Run the specified porter command
Expand Down
20 changes: 12 additions & 8 deletions pkg/build/buildkit/buildx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
dockerclient "github.com/docker/docker/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/attribute"
)

Expand Down Expand Up @@ -80,7 +79,7 @@ func (b *Builder) BuildInvocationImage(ctx context.Context, manifest *manifest.M

d, err := driver.GetDriver(ctx, "porter-driver", nil, cli.Client(), imageopt.Auth, nil, nil, nil, nil, nil, b.Getwd())
if err != nil {
return log.Error(errors.Wrapf(err, "error loading buildx driver"))
return log.Error(fmt.Errorf("error loading buildx driver: %w", err))
}

drivers := []buildx.DriverInfo{
Expand All @@ -97,13 +96,13 @@ func (b *Builder) BuildInvocationImage(ctx context.Context, manifest *manifest.M
session := []session.Attachable{authprovider.NewDockerAuthProvider(b.Err)}
ssh, err := buildflags.ParseSSHSpecs(opts.SSH)
if err != nil {
return errors.Wrap(err, "error parsing the --ssh flags")
return fmt.Errorf("error parsing the --ssh flags: %w", err)
}
session = append(session, ssh)

secrets, err := buildflags.ParseSecretSpecs(opts.Secrets)
if err != nil {
return errors.Wrap(err, "error parsing the --secret flags")
return fmt.Errorf("error parsing the --secret flags: %w", err)
}
session = append(session, secrets)

Expand Down Expand Up @@ -148,10 +147,15 @@ func (b *Builder) BuildInvocationImage(ctx context.Context, manifest *manifest.M
_, buildErr := buildx.Build(ctx, drivers, buildxOpts, dockerToBuildx{cli}, confutil.ConfigDir(cli), printer)
printErr := printer.Wait()

if buildErr == nil {
return log.Error(errors.Wrapf(printErr, "error with docker printer"))
if buildErr == nil && printErr != nil {
return log.Error(fmt.Errorf("error with docker printer: %w", printErr))
}
return log.Error(errors.Wrapf(buildErr, "error building docker image"))

if buildErr != nil {
return log.Error(fmt.Errorf("error building docker image: %w", buildErr))
}

return nil
}

func parseBuildArgs(unparsed []string, parsed map[string]string) {
Expand Down Expand Up @@ -195,7 +199,7 @@ func (b *Builder) TagInvocationImage(ctx context.Context, origTag, newTag string
}

if err := cli.Client().ImageTag(ctx, origTag, newTag); err != nil {
return log.Error(errors.Wrapf(err, "could not tag image %s with value %s", origTag, newTag))
return log.Error(fmt.Errorf("could not tag image %s with value %s: %w", origTag, newTag, err))
}
return nil
}
Expand Down
25 changes: 16 additions & 9 deletions pkg/build/dockerfile-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"get.porter.sh/porter/pkg/pkgmgmt"
"get.porter.sh/porter/pkg/templates"
"get.porter.sh/porter/pkg/tracing"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -45,7 +44,7 @@ func NewDockerfileGenerator(config *config.Config, m *manifest.Manifest, tmpl *t
func (g *DockerfileGenerator) GenerateDockerFile(ctx context.Context) error {
lines, err := g.buildDockerfile(ctx)
if err != nil {
return errors.Wrap(err, "error generating the Dockerfile")
return fmt.Errorf("error generating the Dockerfile: %w", err)
}

fmt.Fprintf(g.Out, "\nWriting Dockerfile =======>\n")
Expand All @@ -56,7 +55,11 @@ func (g *DockerfileGenerator) GenerateDockerFile(ctx context.Context) error {
}

err = g.FileSystem.WriteFile(DOCKER_FILE, []byte(contents), pkg.FileModeWritable)
return errors.Wrap(err, "couldn't write the Dockerfile")
if err != nil {
return fmt.Errorf("couldn't write the Dockerfile: %w", err)
}

return nil
}

func (g *DockerfileGenerator) buildDockerfile(ctx context.Context) ([]string, error) {
Expand Down Expand Up @@ -88,10 +91,10 @@ func (g *DockerfileGenerator) getBaseDockerfile(ctx context.Context) ([]string,
if g.Manifest.Dockerfile != "" {
exists, err := g.FileSystem.Exists(g.Manifest.Dockerfile)
if err != nil {
return nil, errors.Wrapf(err, "error checking if Dockerfile exists: %q", g.Manifest.Dockerfile)
return nil, fmt.Errorf("error checking if Dockerfile exists: %q: %w", g.Manifest.Dockerfile, err)
}
if !exists {
return nil, errors.Errorf("the Dockerfile specified in the manifest doesn't exist: %q", g.Manifest.Dockerfile)
return nil, fmt.Errorf("the Dockerfile specified in the manifest doesn't exist: %q", g.Manifest.Dockerfile)
}

file, err := g.FileSystem.Open(g.Manifest.Dockerfile)
Expand All @@ -103,7 +106,7 @@ func (g *DockerfileGenerator) getBaseDockerfile(ctx context.Context) ([]string,
} else {
contents, err := g.Templates.GetDockerfile()
if err != nil {
return nil, errors.Wrap(err, "error loading default Dockerfile template")
return nil, fmt.Errorf("error loading default Dockerfile template: %w", err)
}
reader = bytes.NewReader(contents)
}
Expand Down Expand Up @@ -215,7 +218,7 @@ func (g *DockerfileGenerator) PrepareFilesystem() error {

err = g.FileSystem.WriteFile(LOCAL_RUN, runTmpl, pkg.FileModeExecutable)
if err != nil {
return errors.Wrapf(err, "failed to write %s", LOCAL_RUN)
return fmt.Errorf("failed to write %s: %w", LOCAL_RUN, err)
}

homeDir, err := g.GetHomeDir()
Expand Down Expand Up @@ -246,7 +249,11 @@ func (g *DockerfileGenerator) copyMixin(mixin string) error {
}

err = g.Context.CopyDirectory(mixinDir, LOCAL_MIXINS, true)
return errors.Wrapf(err, "could not copy mixin directory contents for %s", mixin)
if err != nil {
return fmt.Errorf("could not copy mixin directory contents for %s: %w", mixin, err)
}

return nil
}

func (g *DockerfileGenerator) getIndexOfToken(lines []string, token string) int {
Expand All @@ -263,7 +270,7 @@ func (g *DockerfileGenerator) getIndexOfToken(lines []string, token string) int
func (g *DockerfileGenerator) replaceTokens(ctx context.Context, lines []string) ([]string, error) {
mixinLines, err := g.buildMixinsSection(ctx)
if err != nil {
return nil, errors.Wrap(err, "error generating Dockerfile content for mixins")
return nil, fmt.Errorf("error generating Dockerfile content for mixins: %w", err)
}

fromToken := g.getIndexOfToken(lines, "FROM")
Expand Down
17 changes: 8 additions & 9 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/encoding"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)

type BundleCache interface {
Expand Down Expand Up @@ -75,24 +74,24 @@ func (c *Cache) StoreBundle(bundleRef cnab.BundleReference) (CachedBundle, error
// Remove any previously cached bundle files
err = c.FileSystem.RemoveAll(cb.cacheDir)
if err != nil {
return CachedBundle{}, errors.Wrapf(err, "cannot remove existing cache directory %s", cb.BundlePath)
return CachedBundle{}, fmt.Errorf("cannot remove existing cache directory %s: %w", cb.BundlePath, err)
}

cb.BundlePath = cb.BuildBundlePath()
err = c.FileSystem.MkdirAll(filepath.Dir(cb.BundlePath), pkg.FileModeDirectory)
if err != nil {
return CachedBundle{}, errors.Wrap(err, "unable to create cache directory")
return CachedBundle{}, fmt.Errorf("unable to create cache directory: %w", err)
}

f, err := c.FileSystem.OpenFile(cb.BundlePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, pkg.FileModeWritable)
if err != nil {
return CachedBundle{}, errors.Wrapf(err, "error creating cnab/bundle.json for %s", cb.Reference)
return CachedBundle{}, fmt.Errorf("error creating cnab/bundle.json for %s: %w", cb.Reference, err)
}
defer f.Close()

_, err = cb.Definition.WriteTo(f)
if err != nil {
return CachedBundle{}, errors.Wrapf(err, "error writing to cnab/bundle.json for %s", cb.Reference)
return CachedBundle{}, fmt.Errorf("error writing to cnab/bundle.json for %s: %w", cb.Reference, err)
}

err = c.cacheMetadata(&cb)
Expand All @@ -110,18 +109,18 @@ func (c *Cache) StoreBundle(bundleRef cnab.BundleReference) (CachedBundle, error
cb.RelocationFilePath = cb.BuildRelocationFilePath()
f, err = c.FileSystem.OpenFile(cb.RelocationFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, pkg.FileModeWritable)
if err != nil {
return CachedBundle{}, errors.Wrapf(err, "error creating cnab/relocation-mapping.json for %s", cb.Reference)
return CachedBundle{}, fmt.Errorf("error creating cnab/relocation-mapping.json for %s: %w", cb.Reference, err)
}
defer f.Close()

b, err := json.Marshal(cb.RelocationMap)
if err != nil {
return CachedBundle{}, errors.Wrapf(err, "couldn't marshall relocation mapping for %s", cb.Reference)
return CachedBundle{}, fmt.Errorf("couldn't marshall relocation mapping for %s: %w", cb.Reference, err)
}

_, err = f.Write(b)
if err != nil {
return CachedBundle{}, errors.Wrapf(err, "couldn't write relocation mapping for %s", cb.Reference)
return CachedBundle{}, fmt.Errorf("couldn't write relocation mapping for %s: %w", cb.Reference, err)
}

}
Expand Down Expand Up @@ -163,7 +162,7 @@ func (c *Cache) cacheManifest(cb *CachedBundle) error {
cb.ManifestPath = cb.BuildManifestPath()
err = stamp.WriteManifest(c.Context, cb.ManifestPath)
if err != nil {
return errors.Wrapf(err, "error writing porter.yaml for %s", cb.Reference)
return fmt.Errorf("error writing porter.yaml for %s: %w", cb.Reference, err)
}
}

Expand Down
Loading

0 comments on commit 730b039

Please sign in to comment.