Skip to content

Commit

Permalink
Removing pkg/errors from beats repo (#35896)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitkanfer committed Jun 28, 2023
1 parent 26827e7 commit 2fec2d9
Show file tree
Hide file tree
Showing 180 changed files with 812 additions and 888 deletions.
58 changes: 29 additions & 29 deletions dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"debug/elf"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -50,7 +51,6 @@ import (
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
"github.com/pkg/errors"
)

// Expand expands the given Go text/template string.
Expand Down Expand Up @@ -91,17 +91,17 @@ func expandTemplate(name, tmpl string, funcs template.FuncMap, args ...map[strin
t, err := t.Parse(tmpl)
if err != nil {
if name == "inline" {
return "", errors.Wrapf(err, "failed to parse template '%v'", tmpl)
return "", fmt.Errorf("failed to parse template '%v': %w", tmpl, err)
}
return "", errors.Wrap(err, "failed to parse template")
return "", fmt.Errorf("failed to parse template: %w", err)
}

buf := new(bytes.Buffer)
if err := t.Execute(buf, joinMaps(args...)); err != nil {
if name == "inline" {
return "", errors.Wrapf(err, "failed to expand template '%v'", tmpl)
return "", fmt.Errorf("failed to expand template '%v': %w", tmpl, err)
}
return "", errors.Wrap(err, "failed to expand template")
return "", fmt.Errorf("failed to expand template: %w", err)
}

return buf.String(), nil
Expand All @@ -127,7 +127,7 @@ func joinMaps(args ...map[string]interface{}) map[string]interface{} {
func expandFile(src, dst string, args ...map[string]interface{}) error {
tmplData, err := ioutil.ReadFile(src)
if err != nil {
return errors.Wrapf(err, "failed reading from template %v", src)
return fmt.Errorf("failed reading from template %v: %w", src, err)
}

output, err := expandTemplate(src, string(tmplData), FuncMap, args...)
Expand All @@ -141,7 +141,7 @@ func expandFile(src, dst string, args ...map[string]interface{}) error {
}

if err = ioutil.WriteFile(createDir(dst), []byte(output), 0644); err != nil {
return errors.Wrap(err, "failed to write rendered template")
return fmt.Errorf("failed to write rendered template: %w", err)
}

return nil
Expand All @@ -151,7 +151,7 @@ func expandFile(src, dst string, args ...map[string]interface{}) error {
func CWD(elem ...string) string {
wd, err := os.Getwd()
if err != nil {
panic(errors.Wrap(err, "failed to get the CWD"))
panic(fmt.Errorf("failed to get the CWD: %w", err))
}
return filepath.Join(append([]string{wd}, elem...)...)
}
Expand Down Expand Up @@ -188,7 +188,7 @@ func (info *DockerInfo) IsBoot2Docker() bool {
// HaveDocker returns an error if docker is unavailable.
func HaveDocker() error {
if _, err := GetDockerInfo(); err != nil {
return errors.Wrap(err, "docker is not available")
return fmt.Errorf("docker is not available: %w", err)
}
return nil
}
Expand Down Expand Up @@ -274,7 +274,7 @@ func FindReplace(file string, re *regexp.Regexp, repl string) error {
// MustFindReplace invokes FindReplace and panics if an error occurs.
func MustFindReplace(file string, re *regexp.Regexp, repl string) {
if err := FindReplace(file, re, repl); err != nil {
panic(errors.Wrap(err, "failed to find and replace"))
panic(fmt.Errorf("failed to find and replace: %w", err))
}
}

Expand All @@ -285,23 +285,23 @@ func DownloadFile(url, destinationDir string) (string, error) {

resp, err := http.Get(url)
if err != nil {
return "", errors.Wrap(err, "http get failed")
return "", fmt.Errorf("http get failed: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", errors.Errorf("download failed with http status: %v", resp.StatusCode)
return "", fmt.Errorf("download failed with http status: %v", resp.StatusCode)
}

name := filepath.Join(destinationDir, filepath.Base(url))
f, err := os.Create(createDir(name))
if err != nil {
return "", errors.Wrap(err, "failed to create output file")
return "", fmt.Errorf("failed to create output file: %w", err)
}
defer f.Close()

if _, err = io.Copy(f, resp.Body); err != nil {
return "", errors.Wrap(err, "failed to write file")
return "", fmt.Errorf("failed to write file: %w", err)
}

return name, f.Close()
Expand All @@ -316,7 +316,7 @@ func Extract(sourceFile, destinationDir string) error {
case ext == ".zip":
return unzip(sourceFile, destinationDir)
default:
return errors.Errorf("failed to extract %v, unhandled file extension", sourceFile)
return fmt.Errorf("failed to extract %v, unhandled file extension", sourceFile)
}
}

Expand All @@ -340,7 +340,7 @@ func unzip(sourceFile, destinationDir string) error {

path := filepath.Join(destinationDir, f.Name)
if !strings.HasPrefix(path, destinationDir) {
return errors.Errorf("illegal file path in zip: %v", f.Name)
return fmt.Errorf("illegal file path in zip: %v", f.Name)
}

if f.FileInfo().IsDir() {
Expand Down Expand Up @@ -485,7 +485,7 @@ func untar(sourceFile, destinationDir string) error {

path := filepath.Join(destinationDir, header.Name)
if !strings.HasPrefix(path, destinationDir) {
return errors.Errorf("illegal file path in tar: %v", header.Name)
return fmt.Errorf("illegal file path in tar: %v", header.Name)
}

switch header.Typeflag {
Expand All @@ -511,7 +511,7 @@ func untar(sourceFile, destinationDir string) error {
return err
}
default:
return errors.Errorf("unable to untar type=%c in file=%s", header.Typeflag, path)
return fmt.Errorf("unable to untar type=%c in file=%s", header.Typeflag, path)
}
}

Expand Down Expand Up @@ -613,7 +613,7 @@ func ParallelCtx(ctx context.Context, fns ...interface{}) {

wg.Wait()
if len(errs) > 0 {
panic(errors.Errorf(strings.Join(errs, "\n")))
panic(fmt.Errorf(strings.Join(errs, "\n")))
}
}

Expand Down Expand Up @@ -652,7 +652,7 @@ func FindFiles(globs ...string) ([]string, error) {
for _, glob := range globs {
files, err := filepath.Glob(glob)
if err != nil {
return nil, errors.Wrapf(err, "failed on glob %v", glob)
return nil, fmt.Errorf("failed on glob %v: %w", glob, err)
}
configFiles = append(configFiles, files...)
}
Expand Down Expand Up @@ -691,7 +691,7 @@ func FindFilesRecursive(match func(path string, info os.FileInfo) bool) ([]strin
func FileConcat(out string, perm os.FileMode, files ...string) error {
f, err := os.OpenFile(createDir(out), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perm)
if err != nil {
return errors.Wrap(err, "failed to create file")
return fmt.Errorf("failed to create file: %w", err)
}
defer f.Close()

Expand Down Expand Up @@ -735,20 +735,20 @@ func MustFileConcat(out string, perm os.FileMode, files ...string) {
func VerifySHA256(file string, hash string) error {
f, err := os.Open(file)
if err != nil {
return errors.Wrap(err, "failed to open file for sha256 verification")
return fmt.Errorf("failed to open file for sha256 verification: %w", err)
}
defer f.Close()

sum := sha256.New()
if _, err := io.Copy(sum, f); err != nil {
return errors.Wrap(err, "failed reading from input file")
return fmt.Errorf("failed reading from input file: %w", err)
}

computedHash := hex.EncodeToString(sum.Sum(nil))
expectedHash := strings.TrimSpace(hash)

if computedHash != expectedHash {
return errors.Errorf("SHA256 verification of %v failed. Expected=%v, "+
return fmt.Errorf("SHA256 verification of %v failed. Expected=%v, "+
"but computed=%v", f.Name(), expectedHash, computedHash)
}
log.Println("SHA256 OK:", f.Name())
Expand All @@ -761,13 +761,13 @@ func VerifySHA256(file string, hash string) error {
func CreateSHA512File(file string) error {
f, err := os.Open(file)
if err != nil {
return errors.Wrap(err, "failed to open file for sha512 summing")
return fmt.Errorf("failed to open file for sha512 summing: %w", err)
}
defer f.Close()

sum := sha512.New()
if _, err := io.Copy(sum, f); err != nil {
return errors.Wrap(err, "failed reading from input file")
return fmt.Errorf("failed reading from input file: %w", err)
}

computedHash := hex.EncodeToString(sum.Sum(nil))
Expand Down Expand Up @@ -856,7 +856,7 @@ func XPackBeatDir(path ...string) string {
func LibbeatDir(path ...string) string {
esBeatsDir, err := ElasticBeatsDir()
if err != nil {
panic(errors.Wrap(err, "failed determine libbeat dir location"))
panic(fmt.Errorf("failed determine libbeat dir location: %w", err))
}

return filepath.Join(append([]string{esBeatsDir, "libbeat"}, path...)...)
Expand All @@ -873,7 +873,7 @@ func CreateDir(file string) string {
// Create the output directory.
if dir := filepath.Dir(file); dir != "." {
if err := os.MkdirAll(dir, 0755); err != nil {
panic(errors.Wrapf(err, "failed to create parent dir for %v", file))
panic(fmt.Errorf("failed to create parent dir for %v: %w", file, err))
}
}
return file
Expand All @@ -895,7 +895,7 @@ func ParseVersion(version string) (major, minor, patch int, err error) {
names := parseVersionRegex.SubexpNames()
matches := parseVersionRegex.FindStringSubmatch(version)
if len(matches) == 0 {
err = errors.Errorf("failed to parse version '%v'", version)
err = fmt.Errorf("failed to parse version '%v'", version)
return
}

Expand Down
18 changes: 9 additions & 9 deletions dev-tools/mage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package mage

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -30,7 +31,6 @@ import (

"github.com/magefile/mage/mg"

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

Expand Down Expand Up @@ -98,23 +98,23 @@ func Config(types ConfigFileType, args ConfigFileParams, targetDir string) error
if types.IsShort() {
file := filepath.Join(targetDir, BeatName+".yml")
if err := makeConfigTemplate(file, 0600, args, ShortConfigType); err != nil {
return errors.Wrap(err, "failed making short config")
return fmt.Errorf("failed making short config: %w", err)
}
}

// Reference
if types.IsReference() {
file := filepath.Join(targetDir, BeatName+".reference.yml")
if err := makeConfigTemplate(file, 0644, args, ReferenceConfigType); err != nil {
return errors.Wrap(err, "failed making reference config")
return fmt.Errorf("failed making reference config: %w", err)
}
}

// Docker
if types.IsDocker() {
file := filepath.Join(targetDir, BeatName+".docker.yml")
if err := makeConfigTemplate(file, 0600, args, DockerConfigType); err != nil {
return errors.Wrap(err, "failed making docker config")
return fmt.Errorf("failed making docker config: %w", err)
}
}

Expand All @@ -136,7 +136,7 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
confFile = confParams.Docker
tmplParams = map[string]interface{}{"Docker": true}
default:
panic(errors.Errorf("Invalid config file type: %v", typ))
panic(fmt.Errorf("Invalid config file type: %v", typ))
}

// Build the dependencies.
Expand Down Expand Up @@ -192,18 +192,18 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
var err error
for _, templateGlob := range confParams.Templates {
if tmpl, err = tmpl.ParseGlob(templateGlob); err != nil {
return errors.Wrapf(err, "failed to parse config templates in %q", templateGlob)
return fmt.Errorf("failed to parse config templates in %q: %w", templateGlob, err)
}
}

data, err := ioutil.ReadFile(confFile.Template)
if err != nil {
return errors.Wrapf(err, "failed to read config template %q", confFile.Template)
return fmt.Errorf("failed to read config template %q: %w", confFile.Template, err)
}

tmpl, err = tmpl.Parse(string(data))
if err != nil {
return errors.Wrap(err, "failed to parse template")
return fmt.Errorf("failed to parse template: %w", err)
}

out, err := os.OpenFile(CreateDir(destination), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode)
Expand All @@ -213,7 +213,7 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
defer out.Close()

if err = tmpl.Execute(out, EnvMap(params)); err != nil {
return errors.Wrapf(err, "failed building %v", destination)
return fmt.Errorf("failed building %v: %w", destination, err)
}

return nil
Expand Down
Loading

0 comments on commit 2fec2d9

Please sign in to comment.