Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed to golang native error wrapping #2571

Merged
merged 6 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions cmd/krel/cmd/announce_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strings"
"text/template"

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

Expand Down Expand Up @@ -177,7 +176,7 @@ func runBuildBranchAnnounce(opts *buildBranchAnnounceOptions, buildOpts *buildAn
if err := t.Execute(&annoucement, struct {
Branch string
}{opts.branch}); err != nil {
return errors.Wrap(err, "generating the announcement html file")
return fmt.Errorf("generating the announcement html file: %w", err)
}

announcementSubject := fmt.Sprintf("Kubernetes %s branch has been created", opts.branch)
Expand All @@ -187,7 +186,7 @@ func runBuildBranchAnnounce(opts *buildBranchAnnounceOptions, buildOpts *buildAn
// runBuildReleaseAnnounce build the announcement file when creating a new Kubernetes release
func runBuildReleaseAnnounce(opts *buildReleaseAnnounceOptions, buildOpts *buildAnnounceOptions, announceOpts *announceOptions) error {
if err := announceOpts.Validate(); err != nil {
return errors.Wrap(err, "validating annoucement send options")
return fmt.Errorf("validating annoucement send options: %w", err)
}

logrus.Info("Building release announcement for new release")
Expand Down Expand Up @@ -223,7 +222,7 @@ func runBuildReleaseAnnounce(opts *buildReleaseAnnounceOptions, buildOpts *build
filepath.Base(opts.changelogFilePath),
string(changelogHTML),
}); err != nil {
return errors.Wrap(err, "generating the announcement html file")
return fmt.Errorf("generating the announcement html file: %w", err)
}

announcementSubject := fmt.Sprintf("Kubernetes %s is live!", announceOpts.tag)
Expand All @@ -238,16 +237,15 @@ func (opts *buildAnnounceOptions) saveAnnouncement(announcementSubject string, a
logrus.Infof("Writing HTML file to %s", absOutputPath)
err := os.WriteFile(absOutputPath, annoucement.Bytes(), os.FileMode(0o644))
if err != nil {
return errors.Wrap(err, "saving announcement.html")
return fmt.Errorf("saving announcement.html: %w", err)
}

absOutputPath = filepath.Join(opts.workDir, "announcement-subject.txt")
logrus.Infof("Writing announcement subject to %s", absOutputPath)
err = os.WriteFile(absOutputPath, []byte(announcementSubject), os.FileMode(0o644))
if err != nil {
return errors.Wrap(err, "saving announcement-subject.txt")
return fmt.Errorf("saving announcement-subject.txt: %w", err)
}

logrus.Info("Kubernetes Announcement created.")
return nil
}
Expand All @@ -257,9 +255,8 @@ func getGoVersion() (string, error) {
"go", "version").
RunSilentSuccessOutput()
if err != nil {
return "", errors.Wrap(err, "get go version")
return "", fmt.Errorf("get go version: %w", err)
}

versionRegex := regexp.MustCompile(semVerRegex)
return versionRegex.FindString(strings.TrimSpace(cmdStatus.Output())), nil
}
18 changes: 9 additions & 9 deletions cmd/krel/cmd/announce_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package cmd

import (
"errors"
"fmt"

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

Expand Down Expand Up @@ -110,7 +110,7 @@ func init() {

func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, rootOpts *rootOptions) error {
if err := announceRootOpts.Validate(); err != nil {
return errors.Wrap(err, "validating annoucement send options")
return fmt.Errorf("validating annoucement send options: %w", err)
}
logrus.Info("Retrieving release announcement from Google Cloud Bucket")

Expand All @@ -123,8 +123,8 @@ func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, r

content, err := http.GetURLResponse(u, false)
if err != nil {
return errors.Wrapf(err,
"unable to retrieve release announcement form url: %s", u,
return fmt.Errorf(
"unable to retrieve release announcement form url: %s: %w", u, err,
)
}

Expand All @@ -135,7 +135,7 @@ func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, r
}

if opts.sendgridAPIKey == "" {
return errors.Errorf(
return fmt.Errorf(
"$%s is not set", sendgridAPIKeyEnvKey,
)
}
Expand All @@ -145,12 +145,12 @@ func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, r

if opts.name != "" && opts.email != "" {
if err := m.SetSender(opts.name, opts.email); err != nil {
return errors.Wrap(err, "unable to set mail sender")
return fmt.Errorf("unable to set mail sender: %w", err)
}
} else {
logrus.Info("Retrieving default sender from sendgrid API")
if err := m.SetDefaultSender(); err != nil {
return errors.Wrap(err, "setting default sender")
return fmt.Errorf("setting default sender: %w", err)
}
}

Expand All @@ -164,7 +164,7 @@ func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, r
logrus.Infof("Using Google Groups as announcement target: %v", groups)

if err := m.SetGoogleGroupRecipients(groups...); err != nil {
return errors.Wrap(err, "unable to set mail recipients")
return fmt.Errorf("unable to set mail recipients: %w", err)
}

logrus.Info("Sending mail")
Expand All @@ -181,7 +181,7 @@ func runAnnounce(opts *sendAnnounceOptions, announceRootOpts *announceOptions, r

if yes {
if err := m.Send(content, subject); err != nil {
return errors.Wrap(err, "unable to send mail")
return fmt.Errorf("unable to send mail: %w", err)
}
}

Expand Down
21 changes: 11 additions & 10 deletions cmd/krel/cmd/cve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package cmd

import (
"bytes"
"errors"
"fmt"
"os"
"strings"

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

Expand Down Expand Up @@ -87,7 +88,7 @@ var argFunc = func(cmd *cobra.Command, args []string) error {
}
cveOpts.CVE = strings.ToUpper(args[0])
if err := cve.NewClient().CheckID(cveOpts.CVE); err != nil {
return errors.Errorf("invalid CVE ID. Format must match %s", cve.CVEIDRegExp)
return fmt.Errorf("invalid CVE ID. Format must match %s", cve.CVEIDRegExp)
}
return nil
}
Expand All @@ -113,20 +114,20 @@ func writeNewCVE(opts *cveOptions) (err error) {

file, err := client.CreateEmptyMap(opts.CVE)
if err != nil {
return errors.Wrap(err, "creating new cve data map")
return fmt.Errorf("creating new cve data map: %w", err)
}

oldFile, err := os.ReadFile(file.Name())
if err != nil {
return errors.Wrap(err, "reading local copy of CVE entry")
return fmt.Errorf("reading local copy of CVE entry: %w", err)
}

kubeEditor := editor.NewDefaultEditor([]string{"KUBE_EDITOR", "EDITOR"})
changes, tempFilePath, err := kubeEditor.LaunchTempFile(
"cve-datamap-", ".yaml", bytes.NewReader(oldFile),
)
if err != nil {
return errors.Wrap(err, "launching editor")
return fmt.Errorf("launching editor: %w", err)
}

if bytes.Equal(changes, oldFile) || len(changes) == 0 {
Expand All @@ -145,7 +146,7 @@ func writeCVEFiles(opts *cveOptions) error {
client := cve.NewClient()
for _, mapFile := range opts.mapFiles {
if err := client.Write(opts.CVE, mapFile); err != nil {
return errors.Wrapf(err, "writing map file %s", mapFile)
return fmt.Errorf("writing map file %s: %w", mapFile, err)
}
}
return nil
Expand All @@ -170,7 +171,7 @@ func editCVE(opts *cveOptions) (err error) {
// or we should first pull the data from the bucket
exists, err := client.EntryExists(opts.CVE)
if err != nil {
return errors.Wrap(err, "checking if cve entry exists")
return fmt.Errorf("checking if cve entry exists: %w", err)
}

if exists {
Expand All @@ -186,19 +187,19 @@ func editExistingCVE(opts *cveOptions) (err error) {
client := cve.NewClient()
file, err := client.CopyToTemp(opts.CVE)
if err != nil {
return errors.Wrap(err, "copying CVE entry for edting")
return fmt.Errorf("copying CVE entry for edting: %w", err)
}
oldFile, err := os.ReadFile(file.Name())
if err != nil {
return errors.Wrap(err, "reading local copy of CVE entry")
return fmt.Errorf("reading local copy of CVE entry: %w", err)
}

kubeEditor := editor.NewDefaultEditor([]string{"KUBE_EDITOR", "EDITOR"})
changes, tempFilePath, err := kubeEditor.LaunchTempFile(
"cve-datamap-", ".yaml", bytes.NewReader(oldFile),
)
if err != nil {
return errors.Wrap(err, "launching editor")
return fmt.Errorf("launching editor: %w", err)
}

if bytes.Equal(changes, oldFile) || len(changes) == 0 {
Expand Down
3 changes: 1 addition & 2 deletions cmd/krel/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"fmt"

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

"k8s.io/release/pkg/build"
Expand Down Expand Up @@ -51,7 +50,7 @@ var pushBuildCmd = &cobra.Command{
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := runPushBuild(pushBuildOpts); err != nil {
return errors.Wrap(err, "failed to run")
return fmt.Errorf("failed to run: %w", err)
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions cmd/krel/cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"strings"

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

Expand Down Expand Up @@ -138,7 +137,7 @@ func runRelease(options *anago.ReleaseOptions) error {
// Perform a local check of the specified options
// before launching a Cloud Build job:
if err := options.Validate(&anago.State{}); err != nil {
return errors.Wrap(err, "prechecking release options")
return fmt.Errorf("prechecking release options: %w", err)
}
return rel.Submit(stream)
}
Expand Down
Loading