Skip to content

Commit

Permalink
Merge pull request containers#14785 from saschagrunert/cmd-podman-errors
Browse files Browse the repository at this point in the history
cmd/podman: switch to golang native error wrapping
  • Loading branch information
rhatdan authored Jun 30, 2022
2 parents aa109ae + e8adec5 commit 3e8ab31
Show file tree
Hide file tree
Showing 88 changed files with 343 additions and 379 deletions.
16 changes: 8 additions & 8 deletions cmd/podman-mac-helper/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
Expand All @@ -14,7 +15,6 @@ import (
"syscall"
"text/template"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -111,7 +111,7 @@ func install(cmd *cobra.Command, args []string) error {

file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, rw_r_r)
if err != nil {
return errors.Wrap(err, "error creating helper plist file")
return fmt.Errorf("creating helper plist file: %w", err)
}
defer file.Close()
_, err = buf.WriteTo(file)
Expand All @@ -120,7 +120,7 @@ func install(cmd *cobra.Command, args []string) error {
}

if err = runDetectErr("launchctl", "load", fileName); err != nil {
return errors.Wrap(err, "launchctl failed loading service")
return fmt.Errorf("launchctl failed loading service: %w", err)
}

return nil
Expand All @@ -133,13 +133,13 @@ func restrictRecursive(targetDir string, until string) error {
return err
}
if info.Mode()&fs.ModeSymlink != 0 {
return errors.Errorf("symlinks not allowed in helper paths (remove them and rerun): %s", targetDir)
return fmt.Errorf("symlinks not allowed in helper paths (remove them and rerun): %s", targetDir)
}
if err = os.Chown(targetDir, 0, 0); err != nil {
return errors.Wrap(err, "could not update ownership of helper path")
return fmt.Errorf("could not update ownership of helper path: %w", err)
}
if err = os.Chmod(targetDir, rwx_rx_rx|fs.ModeSticky); err != nil {
return errors.Wrap(err, "could not update permissions of helper path")
return fmt.Errorf("could not update permissions of helper path: %w", err)
}
targetDir = filepath.Dir(targetDir)
}
Expand All @@ -162,7 +162,7 @@ func verifyRootDeep(path string) error {

stat := info.Sys().(*syscall.Stat_t)
if stat.Uid != 0 {
return errors.Errorf("installation target path must be solely owned by root: %s is not", current)
return fmt.Errorf("installation target path must be solely owned by root: %s is not", current)
}

if info.Mode()&fs.ModeSymlink != 0 {
Expand Down Expand Up @@ -206,7 +206,7 @@ func installExecutable(user string) (string, error) {

targetDir := filepath.Join(installPrefix, "podman", "helper", user)
if err := os.MkdirAll(targetDir, rwx_rx_rx); err != nil {
return "", errors.Wrap(err, "could not create helper directory structure")
return "", fmt.Errorf("could not create helper directory structure: %w", err)
}

// Correct any incorrect perms on previously existing directories and verify no symlinks
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman-mac-helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -13,7 +14,6 @@ import (
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -97,7 +97,7 @@ func getUser() (string, string, string, error) {
return "", "", "", fmt.Errorf("invalid uid for user: %s", name)
}
if id == 0 {
return "", "", "", fmt.Errorf("unexpected root user")
return "", "", "", errors.New("unexpected root user")
}

return name, uid, home, nil
Expand Down
5 changes: 2 additions & 3 deletions cmd/podman-mac-helper/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os/exec"
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -48,13 +47,13 @@ func uninstall(cmd *cobra.Command, args []string) error {

if err := os.Remove(fileName); err != nil {
if !os.IsNotExist(err) {
return errors.Errorf("could not remove plist file: %s", fileName)
return fmt.Errorf("could not remove plist file: %s", fileName)
}
}

helperPath := filepath.Join(installPrefix, "podman", "helper", userName)
if err := os.RemoveAll(helperPath); err != nil {
return errors.Errorf("could not remove helper binary path: %s", helperPath)
return fmt.Errorf("could not remove helper binary path: %s", helperPath)
}
return nil
}
3 changes: 1 addition & 2 deletions cmd/podman/auto-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/errorhandling"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,7 +62,7 @@ func init() {
func autoUpdate(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
// Backwards compat. System tests expect this error string.
return errors.Errorf("`%s` takes no arguments", cmd.CommandPath())
return fmt.Errorf("`%s` takes no arguments", cmd.CommandPath())
}

allReports, failures := registry.ContainerEngine().AutoUpdate(registry.GetContext(), autoUpdateOptions.AutoUpdateOptions)
Expand Down
3 changes: 1 addition & 2 deletions cmd/podman/common/diffChanges.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/storage/pkg/archive"
"github.com/pkg/errors"
)

type ChangesReportJSON struct {
Expand All @@ -26,7 +25,7 @@ func ChangesToJSON(diffs *entities.DiffReport) error {
case archive.ChangeModify:
body.Changed = append(body.Changed, row.Path)
default:
return errors.Errorf("output kind %q not recognized", row.Kind)
return fmt.Errorf("output kind %q not recognized", row.Kind)
}
}

Expand Down
27 changes: 14 additions & 13 deletions cmd/podman/common/netflags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common

import (
"errors"
"fmt"
"net"

"github.com/containers/common/libnetwork/types"
Expand All @@ -10,7 +12,6 @@ import (
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/specgen"
"github.com/containers/podman/v4/pkg/specgenutil"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -125,13 +126,13 @@ func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*enti
if d == "none" {
opts.UseImageResolvConf = true
if len(servers) > 1 {
return nil, errors.Errorf("%s is not allowed to be specified with other DNS ip addresses", d)
return nil, fmt.Errorf("%s is not allowed to be specified with other DNS ip addresses", d)
}
break
}
dns := net.ParseIP(d)
if dns == nil {
return nil, errors.Errorf("%s is not an ip address", d)
return nil, fmt.Errorf("%s is not an ip address", d)
}
opts.DNSServers = append(opts.DNSServers, dns)
}
Expand All @@ -154,7 +155,7 @@ func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*enti
for _, dom := range dnsSearches {
if dom == "." {
if len(dnsSearches) > 1 {
return nil, errors.Errorf("cannot pass additional search domains when also specifying '.'")
return nil, errors.New("cannot pass additional search domains when also specifying '.'")
}
continue
}
Expand Down Expand Up @@ -218,18 +219,18 @@ func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*enti
if ip != "" {
// if pod create --infra=false
if infra, err := flags.GetBool("infra"); err == nil && !infra {
return nil, errors.Wrapf(define.ErrInvalidArg, "cannot set --%s without infra container", ipFlagName)
return nil, fmt.Errorf("cannot set --%s without infra container: %w", ipFlagName, define.ErrInvalidArg)
}

staticIP := net.ParseIP(ip)
if staticIP == nil {
return nil, errors.Errorf("%q is not an ip address", ip)
return nil, fmt.Errorf("%q is not an ip address", ip)
}
if !opts.Network.IsBridge() && !opts.Network.IsDefault() {
return nil, errors.Wrapf(define.ErrInvalidArg, "--%s can only be set when the network mode is bridge", ipFlagName)
return nil, fmt.Errorf("--%s can only be set when the network mode is bridge: %w", ipFlagName, define.ErrInvalidArg)
}
if len(opts.Networks) != 1 {
return nil, errors.Wrapf(define.ErrInvalidArg, "--%s can only be set for a single network", ipFlagName)
return nil, fmt.Errorf("--%s can only be set for a single network: %w", ipFlagName, define.ErrInvalidArg)
}
for name, netOpts := range opts.Networks {
netOpts.StaticIPs = append(netOpts.StaticIPs, staticIP)
Expand All @@ -245,17 +246,17 @@ func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*enti
if len(m) > 0 {
// if pod create --infra=false
if infra, err := flags.GetBool("infra"); err == nil && !infra {
return nil, errors.Wrap(define.ErrInvalidArg, "cannot set --mac without infra container")
return nil, fmt.Errorf("cannot set --mac without infra container: %w", define.ErrInvalidArg)
}
mac, err := net.ParseMAC(m)
if err != nil {
return nil, err
}
if !opts.Network.IsBridge() && !opts.Network.IsDefault() {
return nil, errors.Wrap(define.ErrInvalidArg, "--mac-address can only be set when the network mode is bridge")
return nil, fmt.Errorf("--mac-address can only be set when the network mode is bridge: %w", define.ErrInvalidArg)
}
if len(opts.Networks) != 1 {
return nil, errors.Wrap(define.ErrInvalidArg, "--mac-address can only be set for a single network")
return nil, fmt.Errorf("--mac-address can only be set for a single network: %w", define.ErrInvalidArg)
}
for name, netOpts := range opts.Networks {
netOpts.StaticMAC = types.HardwareAddr(mac)
Expand All @@ -270,10 +271,10 @@ func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*enti
if len(aliases) > 0 {
// if pod create --infra=false
if infra, err := flags.GetBool("infra"); err == nil && !infra {
return nil, errors.Wrap(define.ErrInvalidArg, "cannot set --network-alias without infra container")
return nil, fmt.Errorf("cannot set --network-alias without infra container: %w", define.ErrInvalidArg)
}
if !opts.Network.IsBridge() && !opts.Network.IsDefault() {
return nil, errors.Wrap(define.ErrInvalidArg, "--network-alias can only be set when the network mode is bridge")
return nil, fmt.Errorf("--network-alias can only be set when the network mode is bridge: %w", define.ErrInvalidArg)
}
for name, netOpts := range opts.Networks {
netOpts.Aliases = aliases
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/containers/attach.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package containers

import (
"errors"
"os"

"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/validate"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -70,7 +70,7 @@ func init() {

func attach(cmd *cobra.Command, args []string) error {
if len(args) > 1 || (len(args) == 0 && !attachOpts.Latest) {
return errors.Errorf("attach requires the name or id of one running container or the latest flag")
return errors.New("attach requires the name or id of one running container or the latest flag")
}

var name string
Expand Down
12 changes: 6 additions & 6 deletions cmd/podman/containers/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package containers

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/storage/pkg/archive"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -90,7 +90,7 @@ func checkpoint(cmd *cobra.Command, args []string) error {
podmanStart := time.Now()
if cmd.Flags().Changed("compress") {
if checkpointOptions.Export == "" {
return errors.Errorf("--compress can only be used with --export")
return errors.New("--compress can only be used with --export")
}
compress, _ := cmd.Flags().GetString("compress")
switch strings.ToLower(compress) {
Expand All @@ -101,7 +101,7 @@ func checkpoint(cmd *cobra.Command, args []string) error {
case "zstd":
checkpointOptions.Compression = archive.Zstd
default:
return errors.Errorf("Selected compression algorithm (%q) not supported. Please select one from: gzip, none, zstd", compress)
return fmt.Errorf("selected compression algorithm (%q) not supported. Please select one from: gzip, none, zstd", compress)
}
} else {
checkpointOptions.Compression = archive.Zstd
Expand All @@ -110,13 +110,13 @@ func checkpoint(cmd *cobra.Command, args []string) error {
return errors.New("checkpointing a container requires root")
}
if checkpointOptions.Export == "" && checkpointOptions.IgnoreRootFS {
return errors.Errorf("--ignore-rootfs can only be used with --export")
return errors.New("--ignore-rootfs can only be used with --export")
}
if checkpointOptions.Export == "" && checkpointOptions.IgnoreVolumes {
return errors.Errorf("--ignore-volumes can only be used with --export")
return errors.New("--ignore-volumes can only be used with --export")
}
if checkpointOptions.WithPrevious && checkpointOptions.PreCheckPoint {
return errors.Errorf("--with-previous can not be used with --pre-checkpoint")
return errors.New("--with-previous can not be used with --pre-checkpoint")
}
if (checkpointOptions.WithPrevious || checkpointOptions.PreCheckPoint) && !criu.MemTrack() {
return errors.New("system (architecture/kernel/CRIU) does not support memory tracking")
Expand Down
8 changes: 4 additions & 4 deletions cmd/podman/containers/cleanup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package containers

import (
"errors"
"fmt"

"github.com/containers/common/pkg/completion"
Expand All @@ -9,7 +10,6 @@ import (
"github.com/containers/podman/v4/cmd/podman/utils"
"github.com/containers/podman/v4/cmd/podman/validate"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -65,11 +65,11 @@ func cleanup(cmd *cobra.Command, args []string) error {
if cleanupOptions.Exec != "" {
switch {
case cleanupOptions.All:
return errors.Errorf("exec and all options conflict")
return errors.New("exec and all options conflict")
case len(args) > 1:
return errors.Errorf("cannot use exec option when more than one container is given")
return errors.New("cannot use exec option when more than one container is given")
case cleanupOptions.RemoveImage:
return errors.Errorf("exec and rmi options conflict")
return errors.New("exec and rmi options conflict")
}
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/podman/containers/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -56,7 +55,7 @@ func init() {
func clone(cmd *cobra.Command, args []string) error {
switch len(args) {
case 0:
return errors.Wrapf(define.ErrInvalidArg, "must specify at least 1 argument")
return fmt.Errorf("must specify at least 1 argument: %w", define.ErrInvalidArg)
case 2:
ctrClone.CreateOpts.Name = args[1]
case 3:
Expand All @@ -73,7 +72,7 @@ func clone(cmd *cobra.Command, args []string) error {
}
}
if ctrClone.Force && !ctrClone.Destroy {
return errors.Wrapf(define.ErrInvalidArg, "cannot set --force without --destroy")
return fmt.Errorf("cannot set --force without --destroy: %w", define.ErrInvalidArg)
}

ctrClone.ID = args[0]
Expand Down
3 changes: 1 addition & 2 deletions cmd/podman/containers/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -109,7 +108,7 @@ func commit(cmd *cobra.Command, args []string) error {
}
if len(iidFile) > 0 {
if err = ioutil.WriteFile(iidFile, []byte(response.Id), 0644); err != nil {
return errors.Wrap(err, "failed to write image ID")
return fmt.Errorf("failed to write image ID: %w", err)
}
}
fmt.Println(response.Id)
Expand Down
Loading

0 comments on commit 3e8ab31

Please sign in to comment.