Skip to content

Commit

Permalink
Replace errors.Wrap with %w
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Mar 23, 2020
1 parent e9c4111 commit bfc8b0a
Show file tree
Hide file tree
Showing 155 changed files with 630 additions and 700 deletions.
8 changes: 4 additions & 4 deletions cmd/skaffold/app/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package cmd
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -71,18 +71,18 @@ func doBuild(ctx context.Context, out io.Writer) error {
cmdOut := flags.BuildOutput{Builds: bRes}
var buildOutput bytes.Buffer
if err := buildFormatFlag.Template().Execute(&buildOutput, cmdOut); err != nil {
return errors.Wrap(err, "executing template")
return fmt.Errorf("executing template: %w", err)
}

if quietFlag {
if _, err := out.Write(buildOutput.Bytes()); err != nil {
return errors.Wrap(err, "writing build output")
return fmt.Errorf("writing build output: %w", err)
}
}

if buildOutputFlag != "" {
if err := ioutil.WriteFile(buildOutputFlag, buildOutput.Bytes(), 0644); err != nil {
return errors.Wrap(err, "writing build output to file")
return fmt.Errorf("writing build output to file: %w", err)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"os"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -85,7 +84,7 @@ func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
// Start API Server
shutdown, err := server.Initialize(opts)
if err != nil {
return errors.Wrap(err, "initializing api server")
return fmt.Errorf("initializing api server: %w", err)
}
shutdownAPIServer = shutdown

Expand Down Expand Up @@ -191,7 +190,7 @@ func updateCheck(ch chan string, configfile string) error {
}
latest, current, err := update.GetLatestAndCurrentVersion()
if err != nil {
return errors.Wrap(err, "get latest and current Skaffold version")
return fmt.Errorf("get latest and current Skaffold version: %w", err)
}
if latest.GT(current) {
ch <- fmt.Sprintf("There is a new version (%s) of Skaffold available. Download it at %s\n", latest, constants.LatestDownloadURL)
Expand Down Expand Up @@ -233,7 +232,7 @@ func setUpLogs(stdErr io.Writer, level string) error {
logrus.SetOutput(stdErr)
lvl, err := logrus.ParseLevel(level)
if err != nil {
return errors.Wrap(err, "parsing log level")
return fmt.Errorf("parsing log level: %w", err)
}
logrus.SetLevel(lvl)
return nil
Expand Down
5 changes: 2 additions & 3 deletions cmd/skaffold/app/cmd/config/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io"

"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
Expand All @@ -39,7 +38,7 @@ func List(ctx context.Context, out io.Writer) error {
}
configYaml, err = yaml.Marshal(&cfg)
if err != nil {
return errors.Wrap(err, "marshaling config")
return fmt.Errorf("marshaling config: %w", err)
}
} else {
contextConfig, err := getConfigForKubectx()
Expand All @@ -51,7 +50,7 @@ func List(ctx context.Context, out io.Writer) error {
}
configYaml, err = yaml.Marshal(&contextConfig)
if err != nil {
return errors.Wrap(err, "marshaling config")
return fmt.Errorf("marshaling config: %w", err)
}
}

Expand Down
8 changes: 3 additions & 5 deletions cmd/skaffold/app/cmd/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strconv"
"strings"

"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
Expand Down Expand Up @@ -166,15 +165,14 @@ func writeConfig(cfg *config.ContextConfig) error {
func writeFullConfig(cfg *config.GlobalConfig) error {
contents, err := yaml.Marshal(cfg)
if err != nil {
return errors.Wrap(err, "marshaling config")
return fmt.Errorf("marshaling config: %w", err)
}
configFileOrDefault, err := config.ResolveConfigFile(configFile)
if err != nil {
return err
}
err = ioutil.WriteFile(configFileOrDefault, contents, 0644)
if err != nil {
return errors.Wrap(err, "writing config file")
if err := ioutil.WriteFile(configFileOrDefault, contents, 0644); err != nil {
return fmt.Errorf("writing config file: %w", err)
}
return nil
}
Expand Down
15 changes: 7 additions & 8 deletions cmd/skaffold/app/cmd/credits/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package credits

import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"

"github.com/pkg/errors"
"github.com/rakyll/statik/fs"

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/statik"
Expand All @@ -37,30 +37,29 @@ var Path string
func Export(ctx context.Context, out io.Writer) error {
statikFS, err := statik.FS()
if err != nil {
return errors.Wrap(err, "opening embedded filesystem")
return fmt.Errorf("opening embedded filesystem: %w", err)
}

if err := fs.Walk(statikFS, "/skaffold-credits", func(filePath string, fileInfo os.FileInfo, err error) error {
newPath := path.Join(Path, "..", filePath)
if fileInfo.IsDir() {
err := os.Mkdir(newPath, 0755)
if err != nil && !os.IsExist(err) {
return errors.Wrapf(err, "creating directory %s", newPath)
return fmt.Errorf("creating directory %q: %w", newPath, err)
}
} else {
file, err := statikFS.Open(filePath)
if err != nil {
return errors.Wrapf(err, "opening %s in embedded filesystem", filePath)
return fmt.Errorf("opening %q in embedded filesystem: %w", filePath, err)
}

buf, err := ioutil.ReadAll(file)
if err != nil {
return errors.Wrapf(err, "reading %s in embedded filesystem", filePath)
return fmt.Errorf("reading %q in embedded filesystem: %w", filePath, err)
}

err = ioutil.WriteFile(newPath, buf, 0664)
if err != nil {
return errors.Wrapf(err, "writing %s to %s", filePath, newPath)
if err := ioutil.WriteFile(newPath, buf, 0664); err != nil {
return fmt.Errorf("writing %q to %q: %w", filePath, newPath, err)
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package cmd

import (
"context"
"errors"
"io"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -90,7 +90,7 @@ func doDev(ctx context.Context, out io.Writer) error {
return err
})
if err != nil {
if errors.Cause(err) != runner.ErrorConfigurationChanged {
if !errors.Is(err, runner.ErrorConfigurationChanged) {
return err
}
// Otherwise, the skaffold config has changed.
Expand Down
5 changes: 2 additions & 3 deletions cmd/skaffold/app/cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io"

"github.com/pkg/errors"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"

Expand All @@ -46,13 +45,13 @@ func doDiagnose(ctx context.Context, out io.Writer) error {
fmt.Fprintln(out, "Number of artifacts:", len(config.Build.Artifacts))

if err := r.DiagnoseArtifacts(ctx, out); err != nil {
return errors.Wrap(err, "running diagnostic on artifacts")
return fmt.Errorf("running diagnostic on artifacts: %w", err)
}

color.Blue.Fprintln(out, "\nConfiguration")
buf, err := yaml.Marshal(config)
if err != nil {
return errors.Wrap(err, "marshalling configuration")
return fmt.Errorf("marshalling configuration: %w", err)
}
out.Write(buf)

Expand Down
8 changes: 4 additions & 4 deletions cmd/skaffold/app/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package cmd

import (
"context"
"fmt"
"io"
"io/ioutil"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -63,17 +63,17 @@ func fix(out io.Writer, configFile string, overwrite bool) error {
}

if err := validation.Process(cfg.(*latest.SkaffoldConfig)); err != nil {
return errors.Wrap(err, "validating upgraded config")
return fmt.Errorf("validating upgraded config: %w", err)
}

newCfg, err := yaml.Marshal(cfg)
if err != nil {
return errors.Wrap(err, "marshaling new config")
return fmt.Errorf("marshaling new config: %w", err)
}

if overwrite {
if err := ioutil.WriteFile(configFile, newCfg, 0644); err != nil {
return errors.Wrap(err, "writing config file")
return fmt.Errorf("writing config file: %w", err)
}
color.Default.Fprintf(out, "New config at version %s generated and written to %s\n", cfg.GetVersion(), opts.ConfigurationFile)
} else {
Expand Down
4 changes: 2 additions & 2 deletions cmd/skaffold/app/cmd/generate_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package cmd

import (
"context"
"fmt"
"io"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand All @@ -47,7 +47,7 @@ func NewCmdGeneratePipeline() *cobra.Command {
func doGeneratePipeline(ctx context.Context, out io.Writer) error {
return withRunner(ctx, func(r runner.Runner, config *latest.SkaffoldConfig) error {
if err := r.GeneratePipeline(ctx, out, config, configFiles, "pipeline.yaml"); err != nil {
return errors.Wrap(err, "generating ")
return fmt.Errorf("generating : %w", err)
}
color.Default.Fprintln(out, "Pipeline config written to pipeline.yaml!")
return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/skaffold/app/cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package cmd

import (
"context"
"fmt"
"io"
"io/ioutil"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -56,11 +56,11 @@ func doRender(ctx context.Context, out io.Writer) error {
bRes, err := r.BuildAndTest(ctx, buildOut, targetArtifacts(opts, config))

if err != nil {
return errors.Wrap(err, "executing build")
return fmt.Errorf("executing build: %w", err)
}

if err := r.Render(ctx, out, bRes, renderOutputPath); err != nil {
return errors.Wrap(err, "rendering manifests")
return fmt.Errorf("rendering manifests: %w", err)
}
return nil
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/skaffold/app/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package cmd

import (
"context"
"fmt"
"io"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/tips"
Expand All @@ -43,7 +43,7 @@ func doRun(ctx context.Context, out io.Writer) error {
return withRunner(ctx, func(r runner.Runner, config *latest.SkaffoldConfig) error {
bRes, err := r.BuildAndTest(ctx, out, config.Build.Artifacts)
if err != nil {
return errors.Wrap(err, "failed to build")
return fmt.Errorf("failed to build: %w", err)
}

err = r.DeployAndLog(ctx, out, bRes)
Expand Down
18 changes: 9 additions & 9 deletions cmd/skaffold/app/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
Expand All @@ -42,7 +42,7 @@ var createRunner = createNewRunner
func withRunner(ctx context.Context, action func(runner.Runner, *latest.SkaffoldConfig) error) error {
runner, config, err := createRunner(opts)
if err != nil {
return errors.Wrap(err, "creating runner")
return fmt.Errorf("creating runner: %w", err)
}

err = action(runner, config)
Expand All @@ -54,41 +54,41 @@ func withRunner(ctx context.Context, action func(runner.Runner, *latest.Skaffold
func createNewRunner(opts config.SkaffoldOptions) (runner.Runner, *latest.SkaffoldConfig, error) {
parsed, err := schema.ParseConfig(opts.ConfigurationFile, true)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
if os.IsNotExist(errors.Unwrap(err)) {
return nil, nil, fmt.Errorf("[%s] not found. You might need to run `skaffold init`", opts.ConfigurationFile)
}

// If the error is NOT that the file doesn't exist, then we warn the user
// that maybe they are using an outdated version of Skaffold that's unable to read
// the configuration.
warnIfUpdateIsAvailable()
return nil, nil, errors.Wrap(err, "parsing skaffold config")
return nil, nil, fmt.Errorf("parsing skaffold config: %w", err)
}

config := parsed.(*latest.SkaffoldConfig)

if err = schema.ApplyProfiles(config, opts); err != nil {
return nil, nil, errors.Wrap(err, "applying profiles")
return nil, nil, fmt.Errorf("applying profiles: %w", err)
}

kubectx.ConfigureKubeConfig(opts.KubeConfig, opts.KubeContext, config.Deploy.KubeContext)

if err := defaults.Set(config); err != nil {
return nil, nil, errors.Wrap(err, "setting default values")
return nil, nil, fmt.Errorf("setting default values: %w", err)
}

if err := validation.Process(config); err != nil {
return nil, nil, errors.Wrap(err, "invalid skaffold config")
return nil, nil, fmt.Errorf("invalid skaffold config: %w", err)
}

runCtx, err := runcontext.GetRunContext(opts, config.Pipeline)
if err != nil {
return nil, nil, errors.Wrap(err, "getting run context")
return nil, nil, fmt.Errorf("getting run context: %w", err)
}

runner, err := runner.NewForConfig(runCtx)
if err != nil {
return nil, nil, errors.Wrap(err, "creating runner")
return nil, nil, fmt.Errorf("creating runner: %w", err)
}

return runner, config, nil
Expand Down
Loading

0 comments on commit bfc8b0a

Please sign in to comment.