Skip to content

Commit

Permalink
Merge pull request #85 from kolyshkin/pkg-errors
Browse files Browse the repository at this point in the history
*: switch from pkg/errors to Go 1.13+ error wrapping
  • Loading branch information
vrothberg authored Sep 3, 2021
2 parents dece81e + fe455ea commit e433993
Show file tree
Hide file tree
Showing 16 changed files with 20 additions and 722 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module github.com/containers/psgo

go 1.12
go 1.13

require (
github.com/opencontainers/runc v1.0.2
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ github.com/opencontainers/runc v1.0.2 h1:opHZMaswlyxz1OuGpBE53Dwe4/xF7EZTY0A2L/F
github.com/opencontainers/runc v1.0.2/go.mod h1:aTaHFFwQXuA71CiyxOdFFIorAoemI04suvGRQFzWTD0=
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
8 changes: 3 additions & 5 deletions internal/proc/ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"fmt"
"io"
"os"

"github.com/pkg/errors"
)

type IDMap struct {
Expand Down Expand Up @@ -51,7 +49,7 @@ func ParseUserNamespace(pid string) (string, error) {
func ReadMappings(path string) ([]IDMap, error) {
file, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "cannot open %s", path)
return nil, err
}
defer file.Close()

Expand All @@ -64,15 +62,15 @@ func ReadMappings(path string) ([]IDMap, error) {
if err == io.EOF {
return mappings, nil
}
return nil, errors.Wrapf(err, "cannot read line from %s", path)
return nil, fmt.Errorf("cannot read line from %s: %w", path, err)
}
if line == nil {
return mappings, nil
}

containerID, hostID, size := 0, 0, 0
if _, err := fmt.Sscanf(string(line), "%d %d %d", &containerID, &hostID, &size); err != nil {
return nil, errors.Wrapf(err, "cannot parse %s", string(line))
return nil, fmt.Errorf("cannot parse %s: %w", string(line), err)
}
mappings = append(mappings, IDMap{ContainerID: containerID, HostID: hostID, Size: size})
}
Expand Down
6 changes: 2 additions & 4 deletions internal/proc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"os"
"os/exec"
"strings"

"github.com/pkg/errors"
)

// Status is a direct translation of a `/proc/[pid]/status`, which provides much
Expand Down Expand Up @@ -251,12 +249,12 @@ func parseStatus(pid string, lines []string) (*Status, error) {
s.TracerPid = fields[1]
case "Uid:":
if len(fields) != 5 {
return nil, errors.Wrap(errUnexpectedInput, line)
return nil, fmt.Errorf(line+": %w", errUnexpectedInput)
}
s.Uids = []string{fields[1], fields[2], fields[3], fields[4]}
case "Gid:":
if len(fields) != 5 {
return nil, errors.Wrap(errUnexpectedInput, line)
return nil, fmt.Errorf(line+": %w", errUnexpectedInput)
}
s.Gids = []string{fields[1], fields[2], fields[3], fields[4]}
case "FDSize:":
Expand Down
9 changes: 5 additions & 4 deletions internal/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
package process

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

"github.com/containers/psgo/internal/host"
"github.com/containers/psgo/internal/proc"
"github.com/opencontainers/runc/libcontainer/user"
"github.com/pkg/errors"
)

// Process includes process-related from the /proc FS.
Expand Down Expand Up @@ -50,7 +51,7 @@ type Process struct {
func LookupGID(gid string) (string, error) {
gidNum, err := strconv.Atoi(gid)
if err != nil {
return "", errors.Wrap(err, "error parsing group ID")
return "", fmt.Errorf("error parsing group ID: %w", err)
}
g, err := user.LookupGid(gidNum)
if err != nil {
Expand All @@ -64,7 +65,7 @@ func LookupGID(gid string) (string, error) {
func LookupUID(uid string) (string, error) {
uidNum, err := strconv.Atoi(uid)
if err != nil {
return "", errors.Wrap(err, "error parsing user ID")
return "", fmt.Errorf("error parsing user ID: %w", err)
}
u, err := user.LookupUid(uidNum)
if err != nil {
Expand Down Expand Up @@ -107,7 +108,7 @@ func FromPIDs(pids []string, joinUserNS bool) ([]*Process, error) {
for _, pid := range pids {
p, err := New(pid, joinUserNS)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
if errors.Is(err, os.ErrNotExist) {
// proc parsing is racy
// Let's ignore "does not exist" errors
continue
Expand Down
18 changes: 9 additions & 9 deletions psgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package psgo

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -40,7 +41,6 @@ import (
"github.com/containers/psgo/internal/dev"
"github.com/containers/psgo/internal/proc"
"github.com/containers/psgo/internal/process"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -109,7 +109,7 @@ func findID(idStr string, mapping []IDMap, lookupFunc func(uid string) (string,

id, err := strconv.ParseInt(idStr, 10, 0)
if err != nil {
return "", errors.Wrapf(err, "cannot parse %s", idStr)
return "", fmt.Errorf("cannot parse ID: %w", err)
}
for _, m := range mapping {
if int(id) >= m.ContainerID && int(id) < m.ContainerID+m.Size {
Expand All @@ -122,7 +122,7 @@ func findID(idStr string, mapping []IDMap, lookupFunc func(uid string) (string,
// User not found, read the overflow
overflow, err := ioutil.ReadFile(overflowFile)
if err != nil {
return "", errors.Wrapf(err, "cannot read %s", overflowFile)
return "", err
}
return string(overflow), nil
}
Expand All @@ -147,7 +147,7 @@ func translateDescriptors(descriptors []string) ([]aixFormatDescriptor, error) {
}
}
if !found {
return nil, errors.Wrapf(ErrUnknownDescriptor, "'%s'", d)
return nil, fmt.Errorf("'%s': %w", d, ErrUnknownDescriptor)
}
}

Expand Down Expand Up @@ -412,13 +412,13 @@ func JoinNamespaceAndProcessInfoWithOptions(pid string, descriptors []string, op
// extract user namespaces prior to joining the mount namespace
currentUserNs, err := proc.ParseUserNamespace("self")
if err != nil {
dataErr = errors.Wrapf(err, "error determining user namespace")
dataErr = fmt.Errorf("error determining user namespace: %w", err)
return
}

pidUserNs, err := proc.ParseUserNamespace(pid)
if err != nil {
dataErr = errors.Wrapf(err, "error determining user namespace of PID %s", pid)
dataErr = fmt.Errorf("error determining user namespace of PID %s: %w", pid, err)
}

// join the mount namespace of pid
Expand Down Expand Up @@ -478,11 +478,11 @@ func JoinNamespaceAndProcessInfoByPidsWithOptions(pids []string, descriptors []s
for _, pid := range pids {
ns, err := proc.ParsePIDNamespace(pid)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
if errors.Is(err, os.ErrNotExist) {
// catch race conditions
continue
}
return nil, errors.Wrapf(err, "error extracting PID namespace")
return nil, fmt.Errorf("error extracting PID namespace: %w", err)
}
if _, exists := nsMap[ns]; !exists {
nsMap[ns] = true
Expand All @@ -493,7 +493,7 @@ func JoinNamespaceAndProcessInfoByPidsWithOptions(pids []string, descriptors []s
data := [][]string{}
for i, pid := range pidList {
pidData, err := JoinNamespaceAndProcessInfoWithOptions(pid, descriptors, options)
if os.IsNotExist(errors.Cause(err)) {
if errors.Is(err, os.ErrNotExist) {
// catch race conditions
continue
}
Expand Down
24 changes: 0 additions & 24 deletions vendor/github.com/pkg/errors/.gitignore

This file was deleted.

10 changes: 0 additions & 10 deletions vendor/github.com/pkg/errors/.travis.yml

This file was deleted.

23 changes: 0 additions & 23 deletions vendor/github.com/pkg/errors/LICENSE

This file was deleted.

44 changes: 0 additions & 44 deletions vendor/github.com/pkg/errors/Makefile

This file was deleted.

59 changes: 0 additions & 59 deletions vendor/github.com/pkg/errors/README.md

This file was deleted.

Loading

0 comments on commit e433993

Please sign in to comment.