Skip to content

Commit

Permalink
refactor: replace pkg/errors with stdlib errors (#5355)
Browse files Browse the repository at this point in the history
* refactor: replace pkg/errors with stdlib errors

use upstream error wrapping and replace pkg/errors usage with
stdlib errors.
remove unused nolint comments

* lint: fix linter issues

* lint: fix linter issues
  • Loading branch information
kruskall authored Sep 3, 2024
1 parent de3dec4 commit f904ba9
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 133 deletions.
66 changes: 33 additions & 33 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8425,39 +8425,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/pkg/errors
Version: v0.9.1
Licence type (autodetected): BSD-2-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/pkg/[email protected]/LICENSE:

Copyright (c) 2015, Dave Cheney <[email protected]>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/rcrowley/go-metrics
Version: v0.0.0-20201227073835-cf1acfcdf475
Expand Down Expand Up @@ -47211,6 +47178,39 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/pkg/errors
Version: v0.9.1
Licence type (autodetected): BSD-2-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/pkg/[email protected]/LICENSE:

Copyright (c) 2015, Dave Cheney <[email protected]>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/pmezard/go-difflib
Version: v1.0.1-0.20181226105442-5d4384ee4fb2
Expand Down
12 changes: 7 additions & 5 deletions dev-tools/mage/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mage

import (
"errors"
"fmt"
"go/build"
"log"
Expand All @@ -14,7 +15,8 @@ import (

"github.com/josephspurrier/goversioninfo"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

// BuildArgs are the arguments used for the "build" target and they define how
Expand Down Expand Up @@ -111,7 +113,7 @@ func DefaultGolangCrossBuildArgs() BuildArgs {
func GolangCrossBuild(params BuildArgs) error {
if os.Getenv("GOLANG_CROSSBUILD") != "1" {
return errors.New("Use the crossBuild target. golangCrossBuild can " +
"only be executed within the golang-crossbuild docker environment.")
"only be executed within the golang-crossbuild docker environment")
}

defer DockerChown(filepath.Join(params.OutputDir, params.Name+binaryExtension(GOOS)))
Expand Down Expand Up @@ -180,7 +182,7 @@ func Build(params BuildArgs) error {
log.Println("Generating a .syso containing Windows file metadata.")
syso, err := MakeWindowsSysoFile()
if err != nil {
return errors.Wrap(err, "failed generating Windows .syso metadata file")
return fmt.Errorf("failed generating Windows .syso metadata file: %w", err)
}
defer os.Remove(syso)
}
Expand Down Expand Up @@ -219,7 +221,7 @@ func MakeWindowsSysoFile() (string, error) {
},
StringFileInfo: goversioninfo.StringFileInfo{
CompanyName: BeatVendor,
ProductName: strings.Title(BeatName),
ProductName: cases.Title(language.Und, cases.NoLower).String(BeatName),
ProductVersion: version,
FileVersion: version,
FileDescription: BeatDescription,
Expand All @@ -233,7 +235,7 @@ func MakeWindowsSysoFile() (string, error) {
vi.Walk()
sysoFile := BeatName + "_windows_" + GOARCH + ".syso"
if err = vi.WriteSyso(sysoFile, GOARCH); err != nil {
return "", errors.Wrap(err, "failed to generate syso file with Windows metadata")
return "", fmt.Errorf("failed to generate syso file with Windows metadata: %w", err)
}
return sysoFile, nil
}
25 changes: 8 additions & 17 deletions dev-tools/mage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ import (
"text/template"

"github.com/magefile/mage/mg"

"github.com/pkg/errors"
)

// Paths to generated config file templates.
var (
shortTemplate = filepath.Join("build", BeatName+".yml.tmpl")
referenceTemplate = filepath.Join("build", BeatName+".reference.yml.tmpl")
dockerTemplate = filepath.Join("build", BeatName+".docker.yml.tmpl")
)

// ConfigFileType is a bitset that indicates what types of config files to
Expand Down Expand Up @@ -67,23 +58,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 @@ -105,7 +96,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 @@ -154,18 +145,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 := os.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 @@ -175,7 +166,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
3 changes: 1 addition & 2 deletions dev-tools/mage/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"

"github.com/elastic/elastic-agent/dev-tools/mage/gotool"
)
Expand Down Expand Up @@ -83,7 +82,7 @@ func AddLicenseHeaders() error {
case "Elasticv2", "Elastic License 2.0":
license = "Elasticv2"
default:
return errors.Errorf("unknown license type %v", BeatLicense)
return fmt.Errorf("unknown license type %s", BeatLicense)
}

licenser := gotool.Licenser
Expand Down
7 changes: 3 additions & 4 deletions dev-tools/mage/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
package mage

import (
"fmt"
"os"
"path/filepath"

"github.com/pkg/errors"

"github.com/elastic/elastic-agent/dev-tools/mage/gotool"
)

Expand All @@ -25,7 +24,7 @@ func CopyFilesToVendor(vendorFolder string, modulesToCopy []CopyModule) error {
for _, p := range modulesToCopy {
path, err := gotool.ListModuleCacheDir(p.Name)
if err != nil {
return errors.Wrapf(err, "error while looking up cached dir of module: %s", p.Name)
return fmt.Errorf("error while looking up cached dir of module: %s: %w", p.Name, err)
}

for _, f := range p.FilesToCopy {
Expand All @@ -34,7 +33,7 @@ func CopyFilesToVendor(vendorFolder string, modulesToCopy []CopyModule) error {
copyTask := &CopyTask{Source: from, Dest: to, Mode: 0600, DirMode: os.ModeDir | 0750}
err = copyTask.Execute()
if err != nil {
return errors.Wrapf(err, "error while copying file from %s to %s", from, to)
return fmt.Errorf("error while copying file from %s to %s: %w", from, to, err)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions dev-tools/mage/integtest_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"sync"
"time"

"github.com/pkg/errors"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
Expand Down Expand Up @@ -230,12 +228,12 @@ func integTestDockerComposeEnvVars() (map[string]string, error) {
func dockerComposeProjectName() string {
commit, err := CommitHash()
if err != nil {
panic(errors.Wrap(err, "failed to construct docker compose project name"))
panic(fmt.Errorf("failed to construct docker compose project name: %w", err))
}

version, err := BeatQualifiedVersion()
if err != nil {
panic(errors.Wrap(err, "failed to construct docker compose project name"))
panic(fmt.Errorf("failed to construct docker compose project name: %w", err))
}
version = strings.NewReplacer(".", "_").Replace(version)

Expand Down Expand Up @@ -278,9 +276,9 @@ func dockerComposeBuildImages() error {
"docker-compose", args...,
)

// This sleep is to avoid hitting the docker build issues when resources are not available.
if err != nil {
fmt.Println(">> Building docker images again")
//nolint:staticcheck // This sleep is to avoid hitting the docker build issues when resources are not available.
time.Sleep(10)
_, err = sh.Exec(
composeEnv,
Expand Down
Loading

0 comments on commit f904ba9

Please sign in to comment.