Skip to content

Commit

Permalink
feat: replace pkg/errors with Go 1.13 native errors (#123)
Browse files Browse the repository at this point in the history
* feat: replace pkg/errors with Go 1.13 native errors

Add support for error wrapping and switch to modern
Go 1.13 errors.

* fix: do not return nil error

* changelog: add changelog entry
  • Loading branch information
kruskall authored Jul 28, 2022
1 parent 9aede31 commit 44e5479
Show file tree
Hide file tree
Showing 36 changed files with 138 additions and 153 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Replace pkg/errors with Go 1.13 native errors #123.

### Changed

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.17
require (
github.com/elastic/go-windows v1.0.0
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
github.com/pkg/errors v0.8.1
github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0
github.com/stretchr/testify v1.3.0
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
Expand All @@ -14,5 +13,6 @@ require (

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)
6 changes: 3 additions & 3 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package registry

import (
"github.com/pkg/errors"
"fmt"

"github.com/elastic/go-sysinfo/types"
)
Expand All @@ -41,14 +41,14 @@ type ProcessProvider interface {
func Register(provider interface{}) {
if h, ok := provider.(HostProvider); ok {
if hostProvider != nil {
panic(errors.Errorf("HostProvider already registered: %v", hostProvider))
panic(fmt.Sprintf("HostProvider already registered: %v", hostProvider))
}
hostProvider = h
}

if p, ok := provider.(ProcessProvider); ok {
if processProvider != nil {
panic(errors.Errorf("ProcessProvider already registered: %v", processProvider))
panic(fmt.Sprintf("ProcessProvider already registered: %v", processProvider))
}
processProvider = p
}
Expand Down
7 changes: 3 additions & 4 deletions providers/aix/boottime_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ package aix

import (
"encoding/binary"
"fmt"
"os"
"time"

"github.com/pkg/errors"
)

// utmp can't be used by "encoding/binary" if generated by cgo,
Expand Down Expand Up @@ -60,7 +59,7 @@ func bootTime(filename string) (time.Time, error) {
// Get boot time from /etc/utmp
file, err := os.Open(filename)
if err != nil {
return time.Time{}, errors.Wrap(err, "failed to get host uptime: cannot open /etc/utmp")
return time.Time{}, fmt.Errorf("failed to get host uptime: cannot open /etc/utmp: %w", err)
}

defer file.Close()
Expand All @@ -76,5 +75,5 @@ func bootTime(filename string) (time.Time, error) {
}
}

return time.Time{}, errors.Wrap(err, "failed to get host uptime: no utmp record")
return time.Time{}, fmt.Errorf("failed to get host uptime: no utmp record: %w", err)
}
9 changes: 5 additions & 4 deletions providers/aix/host_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ package aix
import "C"

import (
"errors"
"fmt"
"os"
"time"

"github.com/joeshaw/multierror"
"github.com/pkg/errors"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers/shared"
Expand Down Expand Up @@ -80,7 +81,7 @@ func (*host) CPUTime() (types.CPUTimes, error) {
cpudata := C.perfstat_cpu_total_t{}

if _, err := C.perfstat_cpu_total(nil, &cpudata, C.sizeof_perfstat_cpu_total_t, 1); err != nil {
return types.CPUTimes{}, errors.Wrap(err, "error while callin perfstat_cpu_total")
return types.CPUTimes{}, fmt.Errorf("error while callin perfstat_cpu_total: %w", err)
}

return types.CPUTimes{
Expand All @@ -100,7 +101,7 @@ func (*host) Memory() (*types.HostMemoryInfo, error) {
meminfo := C.perfstat_memory_total_t{}
_, err := C.perfstat_memory_total(nil, &meminfo, C.sizeof_perfstat_memory_total_t, 1)
if err != nil {
return nil, errors.Wrap(err, "perfstat_memory_total failed")
return nil, fmt.Errorf("perfstat_memory_total failed: %w", err)
}

mem.Total = uint64(meminfo.real_total) * pagesize
Expand Down Expand Up @@ -137,7 +138,7 @@ type reader struct {

func (r *reader) addErr(err error) bool {
if err != nil {
if errors.Cause(err) != types.ErrNotImplemented {
if !errors.Is(err, types.ErrNotImplemented) {
r.errs = append(r.errs, err)
}
return true
Expand Down
9 changes: 4 additions & 5 deletions providers/aix/kernel_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,26 @@ package aix
import "C"

import (
"fmt"
"strconv"

"github.com/pkg/errors"
)

var oslevel string

func getKernelVersion() (int, int, error) {
name := C.struct_utsname{}
if _, err := C.uname(&name); err != nil {
return 0, 0, errors.Wrap(err, "kernel version: uname")
return 0, 0, fmt.Errorf("kernel version: uname: %w", err)
}

version, err := strconv.Atoi(C.GoString(&name.version[0]))
if err != nil {
return 0, 0, errors.Wrap(err, "parsing kernel version")
return 0, 0, fmt.Errorf("parsing kernel version: %w", err)
}

release, err := strconv.Atoi(C.GoString(&name.release[0]))
if err != nil {
return 0, 0, errors.Wrap(err, "parsing kernel release")
return 0, 0, fmt.Errorf("parsing kernel release: %w", err)
}
return version, release, nil
}
Expand Down
6 changes: 2 additions & 4 deletions providers/aix/machineid_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ package aix
*/
import "C"

import (
"github.com/pkg/errors"
)
import "fmt"

// MachineID returns the id of the machine
func MachineID() (string, error) {
name := C.struct_utsname{}
if _, err := C.uname(&name); err != nil {
return "", errors.Wrap(err, "machine id")
return "", fmt.Errorf("machine id: %w", err)
}
return C.GoString(&name.machine[0]), nil
}
5 changes: 2 additions & 3 deletions providers/aix/os_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
package aix

import (
"fmt"
"io/ioutil"
"strconv"
"strings"

"github.com/pkg/errors"

"github.com/elastic/go-sysinfo/types"
)

Expand All @@ -44,7 +43,7 @@ func getOSInfo() (*types.OSInfo, error) {
// Retrieve build version from "/proc/version".
procVersion, err := ioutil.ReadFile("/proc/version")
if err != nil {
return nil, errors.Wrap(err, "failed to get OS info: cannot open /proc/version")
return nil, fmt.Errorf("failed to get OS info: cannot open /proc/version: %w", err)
}
build := strings.SplitN(string(procVersion), "\n", 4)[2]

Expand Down
26 changes: 13 additions & 13 deletions providers/aix/process_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import "C"
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
Expand All @@ -43,8 +45,6 @@ import (
"time"
"unsafe"

"github.com/pkg/errors"

"github.com/elastic/go-sysinfo/types"
)

Expand All @@ -54,7 +54,7 @@ func (aixSystem) Processes() ([]types.Process, error) {
// getprocs which will also retrieve kernel threads.
files, err := ioutil.ReadDir("/proc")
if err != nil {
return nil, errors.Wrap(err, "error while reading /proc")
return nil, fmt.Errorf("error while reading /proc: %w", err)
}

processes := make([]types.Process, 0, len(files))
Expand Down Expand Up @@ -121,7 +121,7 @@ func (p *process) Info() (types.ProcessInfo, error) {
err = syscall.ESRCH
}
if err != nil {
return types.ProcessInfo{}, errors.Wrap(err, "error while calling getprocs")
return types.ProcessInfo{}, fmt.Errorf("error while calling getprocs: %w", err)
}

p.info.PPID = int(info.pi_ppid)
Expand All @@ -133,7 +133,7 @@ func (p *process) Info() (types.ProcessInfo, error) {
buf := make([]byte, 8192)
var args []string
if _, err := C.getargs(unsafe.Pointer(&info), C.sizeof_struct_procsinfo64, (*C.char)(&buf[0]), 8192); err != nil {
return types.ProcessInfo{}, errors.Wrap(err, "error while calling getargs")
return types.ProcessInfo{}, fmt.Errorf("error while calling getargs: %w", err)
}

bbuf := bytes.NewBuffer(buf)
Expand All @@ -143,7 +143,7 @@ func (p *process) Info() (types.ProcessInfo, error) {
break
}
if err != nil {
return types.ProcessInfo{}, errors.Wrap(err, "error while reading arguments")
return types.ProcessInfo{}, fmt.Errorf("error while reading arguments: %w", err)
}

args = append(args, string(chop(arg)))
Expand Down Expand Up @@ -183,7 +183,7 @@ func (p *process) Info() (types.ProcessInfo, error) {
cwd, err := os.Readlink("/proc/" + strconv.Itoa(p.pid) + "/cwd")
if err != nil {
if !os.IsNotExist(err) {
return types.ProcessInfo{}, errors.Wrapf(err, "error while reading /proc/%s/cwd", strconv.Itoa(p.pid))
return types.ProcessInfo{}, fmt.Errorf("error while reading /proc/%s/cwd: %w", strconv.Itoa(p.pid), err)
}
}

Expand All @@ -205,7 +205,7 @@ func (p *process) Environment() (map[string]string, error) {
info.pi_pid = C.pid_t(p.pid)

if _, err := C.getevars(unsafe.Pointer(&info), C.sizeof_struct_procsinfo64, (*C.char)(&buf[0]), 8192); err != nil {
return nil, errors.Wrap(err, "error while calling getevars")
return nil, fmt.Errorf("error while calling getevars: %w", err)
}

bbuf := bytes.NewBuffer(buf)
Expand All @@ -218,12 +218,12 @@ func (p *process) Environment() (map[string]string, error) {
break
}
if err != nil {
return nil, errors.Wrap(err, "error while calling getevars")
return nil, fmt.Errorf("error while calling getevars: %w", err)
}

pair := bytes.SplitN(chop(line), delim, 2)
if len(pair) != 2 {
return nil, errors.Wrap(err, "error reading process environment")
return nil, errors.New("error reading process environment")
}
p.env[string(pair[0])] = string(pair[1])
}
Expand Down Expand Up @@ -260,7 +260,7 @@ func (p *process) Memory() (types.MemoryInfo, error) {
err = syscall.ESRCH
}
if err != nil {
return types.MemoryInfo{}, errors.Wrap(err, "error while calling getprocs")
return types.MemoryInfo{}, fmt.Errorf("error while calling getprocs: %w", err)
}

mem.Resident = uint64(info.pi_drss+info.pi_trss) * pagesize
Expand All @@ -286,12 +286,12 @@ func (p *process) decodeProcfsFile(name string, data interface{}) error {

file, err := os.Open(fileName)
if err != nil {
return errors.Wrapf(err, "error while opening %s", fileName)
return fmt.Errorf("error while opening %s: %w", fileName, err)
}
defer file.Close()

if err := binary.Read(file, binary.BigEndian, data); err != nil {
return errors.Wrapf(err, "error while decoding %s", fileName)
return fmt.Errorf("error while decoding %s: %w", fileName, err)
}

return nil
Expand Down
5 changes: 2 additions & 3 deletions providers/darwin/arch_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@
package darwin

import (
"fmt"
"syscall"

"github.com/pkg/errors"
)

const hardwareMIB = "hw.machine"

func Architecture() (string, error) {
arch, err := syscall.Sysctl(hardwareMIB)
if err != nil {
return "", errors.Wrap(err, "failed to get architecture")
return "", fmt.Errorf("failed to get architecture: %w", err)
}

return arch, nil
Expand Down
5 changes: 2 additions & 3 deletions providers/darwin/boottime_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
package darwin

import (
"fmt"
"syscall"
"time"

"github.com/pkg/errors"
)

const kernBoottimeMIB = "kern.boottime"

func BootTime() (time.Time, error) {
var tv syscall.Timeval
if err := sysctlByName(kernBoottimeMIB, &tv); err != nil {
return time.Time{}, errors.Wrap(err, "failed to get host uptime")
return time.Time{}, fmt.Errorf("failed to get host uptime: %w", err)
}

bootTime := time.Unix(int64(tv.Sec), int64(tv.Usec)*int64(time.Microsecond))
Expand Down
Loading

0 comments on commit 44e5479

Please sign in to comment.