Skip to content

Commit f904ba9

Browse files
authored
refactor: replace pkg/errors with stdlib errors (#5355)
* 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
1 parent de3dec4 commit f904ba9

File tree

14 files changed

+118
-133
lines changed

14 files changed

+118
-133
lines changed

NOTICE.txt

+33-33
Original file line numberDiff line numberDiff line change
@@ -8425,39 +8425,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
84258425
THE SOFTWARE.
84268426

84278427

8428-
--------------------------------------------------------------------------------
8429-
Dependency : github.com/pkg/errors
8430-
Version: v0.9.1
8431-
Licence type (autodetected): BSD-2-Clause
8432-
--------------------------------------------------------------------------------
8433-
8434-
Contents of probable licence file $GOMODCACHE/github.com/pkg/errors@v0.9.1/LICENSE:
8435-
8436-
Copyright (c) 2015, Dave Cheney <dave@cheney.net>
8437-
All rights reserved.
8438-
8439-
Redistribution and use in source and binary forms, with or without
8440-
modification, are permitted provided that the following conditions are met:
8441-
8442-
* Redistributions of source code must retain the above copyright notice, this
8443-
list of conditions and the following disclaimer.
8444-
8445-
* Redistributions in binary form must reproduce the above copyright notice,
8446-
this list of conditions and the following disclaimer in the documentation
8447-
and/or other materials provided with the distribution.
8448-
8449-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
8450-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8451-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
8452-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
8453-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8454-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
8455-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
8456-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
8457-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8458-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8459-
8460-
84618428
--------------------------------------------------------------------------------
84628429
Dependency : github.com/rcrowley/go-metrics
84638430
Version: v0.0.0-20201227073835-cf1acfcdf475
@@ -47211,6 +47178,39 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4721147178
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4721247179

4721347180

47181+
--------------------------------------------------------------------------------
47182+
Dependency : github.com/pkg/errors
47183+
Version: v0.9.1
47184+
Licence type (autodetected): BSD-2-Clause
47185+
--------------------------------------------------------------------------------
47186+
47187+
Contents of probable licence file $GOMODCACHE/github.com/pkg/errors@v0.9.1/LICENSE:
47188+
47189+
Copyright (c) 2015, Dave Cheney <dave@cheney.net>
47190+
All rights reserved.
47191+
47192+
Redistribution and use in source and binary forms, with or without
47193+
modification, are permitted provided that the following conditions are met:
47194+
47195+
* Redistributions of source code must retain the above copyright notice, this
47196+
list of conditions and the following disclaimer.
47197+
47198+
* Redistributions in binary form must reproduce the above copyright notice,
47199+
this list of conditions and the following disclaimer in the documentation
47200+
and/or other materials provided with the distribution.
47201+
47202+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47203+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47204+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47205+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
47206+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47207+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47208+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47209+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47210+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47211+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47212+
47213+
4721447214
--------------------------------------------------------------------------------
4721547215
Dependency : github.com/pmezard/go-difflib
4721647216
Version: v1.0.1-0.20181226105442-5d4384ee4fb2

dev-tools/mage/build.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package mage
66

77
import (
8+
"errors"
89
"fmt"
910
"go/build"
1011
"log"
@@ -14,7 +15,8 @@ import (
1415

1516
"github.com/josephspurrier/goversioninfo"
1617
"github.com/magefile/mage/sh"
17-
"github.com/pkg/errors"
18+
"golang.org/x/text/cases"
19+
"golang.org/x/text/language"
1820
)
1921

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

117119
defer DockerChown(filepath.Join(params.OutputDir, params.Name+binaryExtension(GOOS)))
@@ -180,7 +182,7 @@ func Build(params BuildArgs) error {
180182
log.Println("Generating a .syso containing Windows file metadata.")
181183
syso, err := MakeWindowsSysoFile()
182184
if err != nil {
183-
return errors.Wrap(err, "failed generating Windows .syso metadata file")
185+
return fmt.Errorf("failed generating Windows .syso metadata file: %w", err)
184186
}
185187
defer os.Remove(syso)
186188
}
@@ -219,7 +221,7 @@ func MakeWindowsSysoFile() (string, error) {
219221
},
220222
StringFileInfo: goversioninfo.StringFileInfo{
221223
CompanyName: BeatVendor,
222-
ProductName: strings.Title(BeatName),
224+
ProductName: cases.Title(language.Und, cases.NoLower).String(BeatName),
223225
ProductVersion: version,
224226
FileVersion: version,
225227
FileDescription: BeatDescription,
@@ -233,7 +235,7 @@ func MakeWindowsSysoFile() (string, error) {
233235
vi.Walk()
234236
sysoFile := BeatName + "_windows_" + GOARCH + ".syso"
235237
if err = vi.WriteSyso(sysoFile, GOARCH); err != nil {
236-
return "", errors.Wrap(err, "failed to generate syso file with Windows metadata")
238+
return "", fmt.Errorf("failed to generate syso file with Windows metadata: %w", err)
237239
}
238240
return sysoFile, nil
239241
}

dev-tools/mage/config.go

+8-17
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ import (
1414
"text/template"
1515

1616
"github.com/magefile/mage/mg"
17-
18-
"github.com/pkg/errors"
19-
)
20-
21-
// Paths to generated config file templates.
22-
var (
23-
shortTemplate = filepath.Join("build", BeatName+".yml.tmpl")
24-
referenceTemplate = filepath.Join("build", BeatName+".reference.yml.tmpl")
25-
dockerTemplate = filepath.Join("build", BeatName+".docker.yml.tmpl")
2617
)
2718

2819
// ConfigFileType is a bitset that indicates what types of config files to
@@ -67,23 +58,23 @@ func Config(types ConfigFileType, args ConfigFileParams, targetDir string) error
6758
if types.IsShort() {
6859
file := filepath.Join(targetDir, BeatName+".yml")
6960
if err := makeConfigTemplate(file, 0600, args, ShortConfigType); err != nil {
70-
return errors.Wrap(err, "failed making short config")
61+
return fmt.Errorf("failed making short config: %w", err)
7162
}
7263
}
7364

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

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

@@ -105,7 +96,7 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
10596
confFile = confParams.Docker
10697
tmplParams = map[string]interface{}{"Docker": true}
10798
default:
108-
panic(errors.Errorf("Invalid config file type: %v", typ))
99+
panic(fmt.Errorf("invalid config file type: %v", typ))
109100
}
110101

111102
// Build the dependencies.
@@ -154,18 +145,18 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
154145
var err error
155146
for _, templateGlob := range confParams.Templates {
156147
if tmpl, err = tmpl.ParseGlob(templateGlob); err != nil {
157-
return errors.Wrapf(err, "failed to parse config templates in %q", templateGlob)
148+
return fmt.Errorf("failed to parse config templates in %q: %w", templateGlob, err)
158149
}
159150
}
160151

161152
data, err := os.ReadFile(confFile.Template)
162153
if err != nil {
163-
return errors.Wrapf(err, "failed to read config template %q", confFile.Template)
154+
return fmt.Errorf("failed to read config template %q: %w", confFile.Template, err)
164155
}
165156

166157
tmpl, err = tmpl.Parse(string(data))
167158
if err != nil {
168-
return errors.Wrap(err, "failed to parse template")
159+
return fmt.Errorf("failed to parse template: %w", err)
169160
}
170161

171162
out, err := os.OpenFile(CreateDir(destination), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode)
@@ -175,7 +166,7 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
175166
defer out.Close()
176167

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

181172
return nil

dev-tools/mage/fmt.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/magefile/mage/mg"
1313
"github.com/magefile/mage/sh"
14-
"github.com/pkg/errors"
1514

1615
"github.com/elastic/elastic-agent/dev-tools/mage/gotool"
1716
)
@@ -83,7 +82,7 @@ func AddLicenseHeaders() error {
8382
case "Elasticv2", "Elastic License 2.0":
8483
license = "Elasticv2"
8584
default:
86-
return errors.Errorf("unknown license type %v", BeatLicense)
85+
return fmt.Errorf("unknown license type %s", BeatLicense)
8786
}
8887

8988
licenser := gotool.Licenser

dev-tools/mage/gomod.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
package mage
66

77
import (
8+
"fmt"
89
"os"
910
"path/filepath"
1011

11-
"github.com/pkg/errors"
12-
1312
"github.com/elastic/elastic-agent/dev-tools/mage/gotool"
1413
)
1514

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

3130
for _, f := range p.FilesToCopy {
@@ -34,7 +33,7 @@ func CopyFilesToVendor(vendorFolder string, modulesToCopy []CopyModule) error {
3433
copyTask := &CopyTask{Source: from, Dest: to, Mode: 0600, DirMode: os.ModeDir | 0750}
3534
err = copyTask.Execute()
3635
if err != nil {
37-
return errors.Wrapf(err, "error while copying file from %s to %s", from, to)
36+
return fmt.Errorf("error while copying file from %s to %s: %w", from, to, err)
3837
}
3938
}
4039
}

dev-tools/mage/integtest_docker.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"sync"
1717
"time"
1818

19-
"github.com/pkg/errors"
20-
2119
"github.com/magefile/mage/mg"
2220
"github.com/magefile/mage/sh"
2321
)
@@ -230,12 +228,12 @@ func integTestDockerComposeEnvVars() (map[string]string, error) {
230228
func dockerComposeProjectName() string {
231229
commit, err := CommitHash()
232230
if err != nil {
233-
panic(errors.Wrap(err, "failed to construct docker compose project name"))
231+
panic(fmt.Errorf("failed to construct docker compose project name: %w", err))
234232
}
235233

236234
version, err := BeatQualifiedVersion()
237235
if err != nil {
238-
panic(errors.Wrap(err, "failed to construct docker compose project name"))
236+
panic(fmt.Errorf("failed to construct docker compose project name: %w", err))
239237
}
240238
version = strings.NewReplacer(".", "_").Replace(version)
241239

@@ -278,9 +276,9 @@ func dockerComposeBuildImages() error {
278276
"docker-compose", args...,
279277
)
280278

281-
// This sleep is to avoid hitting the docker build issues when resources are not available.
282279
if err != nil {
283280
fmt.Println(">> Building docker images again")
281+
//nolint:staticcheck // This sleep is to avoid hitting the docker build issues when resources are not available.
284282
time.Sleep(10)
285283
_, err = sh.Exec(
286284
composeEnv,

0 commit comments

Comments
 (0)