Skip to content

Commit

Permalink
Replace github.com/pkg/errors with fmt and errors
Browse files Browse the repository at this point in the history
This removes the usage of github.com/pkg/errors in favor of the fmt
and errors packages from the standard library.

Signed-off-by: Thomas Hipp <[email protected]>
  • Loading branch information
monstermunchkin committed Mar 2, 2022
1 parent 6a5ebc9 commit 55c7e31
Show file tree
Hide file tree
Showing 170 changed files with 1,801 additions and 1,978 deletions.
4 changes: 1 addition & 3 deletions client/simplestreams_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"os/exec"
"strings"

"github.com/pkg/errors"

"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
)
Expand Down Expand Up @@ -44,7 +42,7 @@ func (r *ProtocolSimpleStreams) GetImageFingerprints() ([]string, error) {
func (r *ProtocolSimpleStreams) GetImage(fingerprint string) (*api.Image, string, error) {
image, err := r.ssClient.GetImage(fingerprint)
if err != nil {
return nil, "", errors.Wrapf(err, "Failed getting image")
return nil, "", fmt.Errorf("Failed getting image: %w", err)
}

return image, "", err
Expand Down
15 changes: 7 additions & 8 deletions lxc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sort"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"

Expand Down Expand Up @@ -546,7 +545,7 @@ func (c *cmdClusterEnable) Run(cmd *cobra.Command, args []string) error {
// Check if the LXD server is available on the network.
server, _, err := resource.server.GetServer()
if err != nil {
return errors.Wrap(err, "Failed to retrieve current server config")
return fmt.Errorf("Failed to retrieve current server config: %w", err)
}

if server.Config["core.https_address"] == "" {
Expand All @@ -556,7 +555,7 @@ func (c *cmdClusterEnable) Run(cmd *cobra.Command, args []string) error {
// Check if already enabled
currentCluster, etag, err := resource.server.GetCluster()
if err != nil {
return errors.Wrap(err, "Failed to retrieve current cluster config")
return fmt.Errorf("Failed to retrieve current cluster config: %w", err)
}

if currentCluster.Enabled {
Expand All @@ -569,12 +568,12 @@ func (c *cmdClusterEnable) Run(cmd *cobra.Command, args []string) error {
req.Enabled = true
op, err := resource.server.UpdateCluster(req, etag)
if err != nil {
return errors.Wrap(err, "Failed to configure cluster")
return fmt.Errorf("Failed to configure cluster: %w", err)
}

err = op.Wait()
if err != nil {
return errors.Wrap(err, "Failed to configure cluster")
return fmt.Errorf("Failed to configure cluster: %w", err)
}

fmt.Println(i18n.G("Clustering enabled"))
Expand Down Expand Up @@ -752,7 +751,7 @@ func (c *cmdClusterAdd) Run(cmd *cobra.Command, args []string) error {
opAPI := op.Get()
joinToken, err := opAPI.ToClusterJoinToken()
if err != nil {
return errors.Wrapf(err, "Failed converting token operation to join token")
return fmt.Errorf("Failed converting token operation to join token: %w", err)
}

fmt.Printf(i18n.G("Member %s join token:")+"\n", resource.name)
Expand Down Expand Up @@ -1099,7 +1098,7 @@ func (c *cmdClusterEvacuateAction) Run(cmd *cobra.Command, args []string) error
// Parse remote.
resources, err := c.global.ParseServers(args[0])
if err != nil {
return errors.Wrap(err, "Failed to parse servers")
return fmt.Errorf("Failed to parse servers: %w", err)
}

resource := resources[0]
Expand All @@ -1125,7 +1124,7 @@ func (c *cmdClusterEvacuateAction) Run(cmd *cobra.Command, args []string) error

op, err := resource.server.UpdateClusterMemberState(resource.name, state)
if err != nil {
return errors.Wrap(err, "Failed to update cluster member state")
return fmt.Errorf("Failed to update cluster member state: %w", err)
}

var format string
Expand Down
6 changes: 3 additions & 3 deletions lxc/export.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"fmt"
"io"
"os"
"strings"
"time"

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

"github.com/lxc/lxd/client"
Expand Down Expand Up @@ -78,7 +78,7 @@ func (c *cmdExport) Run(cmd *cobra.Command, args []string) error {

op, err := d.CreateInstanceBackup(name, req)
if err != nil {
return errors.Wrap(err, "Create instance backup")
return fmt.Errorf("Create instance backup: %w", err)
}

// Watch the background operation
Expand Down Expand Up @@ -152,7 +152,7 @@ func (c *cmdExport) Run(cmd *cobra.Command, args []string) error {
if err != nil {
os.Remove(targetName)
progress.Done("")
return errors.Wrap(err, "Fetch instance backup file")
return fmt.Errorf("Fetch instance backup file: %w", err)
}

progress.Done(i18n.G("Backup exported successfully!"))
Expand Down
5 changes: 2 additions & 3 deletions lxc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -166,7 +165,7 @@ func (c *cmdInit) create(conf *config.Config, args []string) (lxd.InstanceServer
if c.flagNetwork != "" {
network, _, err := d.GetNetwork(c.flagNetwork)
if err != nil {
return nil, "", errors.Wrapf(err, "Failed loading network %q", c.flagNetwork)
return nil, "", fmt.Errorf("Failed loading network %q: %w", c.flagNetwork, err)
}

// Prepare the instance's NIC device entry.
Expand Down Expand Up @@ -216,7 +215,7 @@ func (c *cmdInit) create(conf *config.Config, args []string) (lxd.InstanceServer
if c.flagStorage != "" {
_, _, err := d.GetStoragePool(c.flagStorage)
if err != nil {
return nil, "", errors.Wrapf(err, "Failed loading storage pool %q", c.flagStorage)
return nil, "", fmt.Errorf("Failed loading storage pool %q: %w", c.flagStorage, err)
}

devicesMap["root"] = map[string]string{
Expand Down
21 changes: 10 additions & 11 deletions lxc/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/lxc/lxd/shared/api"
cli "github.com/lxc/lxd/shared/cmd"
"github.com/lxc/lxd/shared/i18n"
"github.com/pkg/errors"
)

type cmdMove struct {
Expand Down Expand Up @@ -242,7 +241,7 @@ func (c *cmdMove) Run(cmd *cobra.Command, args []string) error {
del.flagForceProtected = true
err = del.Run(cmd, args[:1])
if err != nil {
return errors.Wrap(err, "Failed to delete original instance after copying it")
return fmt.Errorf("Failed to delete original instance after copying it: %w", err)
}

return nil
Expand Down Expand Up @@ -275,7 +274,7 @@ func moveClusterInstance(conf *config.Config, sourceResource string, destResourc
// Connect to the source host
source, err := conf.GetInstanceServer(sourceRemote)
if err != nil {
return errors.Wrap(err, i18n.G("Failed to connect to cluster member"))
return fmt.Errorf(i18n.G("Failed to connect to cluster member: %w"), err)
}

// Check that it's a cluster
Expand All @@ -293,7 +292,7 @@ func moveClusterInstance(conf *config.Config, sourceResource string, destResourc

op, err := source.MigrateInstance(sourceName, req)
if err != nil {
return errors.Wrap(err, i18n.G("Migration API failure"))
return fmt.Errorf(i18n.G("Migration API failure: %w"), err)
}

// Watch the background operation
Expand All @@ -316,7 +315,7 @@ func moveClusterInstance(conf *config.Config, sourceResource string, destResourc

err = op.Wait()
if err != nil {
return errors.Wrap(err, i18n.G("Migration operation failure"))
return fmt.Errorf(i18n.G("Migration operation failure: %w"), err)
}

return nil
Expand Down Expand Up @@ -349,7 +348,7 @@ func moveInstancePool(conf *config.Config, sourceResource string, destResource s
// Connect to the source host.
source, err := conf.GetInstanceServer(sourceRemote)
if err != nil {
return errors.Wrap(err, i18n.G("Failed to connect to cluster member"))
return fmt.Errorf(i18n.G("Failed to connect to cluster member: %w"), err)
}

// Pass the new pool to the migration API.
Expand All @@ -363,12 +362,12 @@ func moveInstancePool(conf *config.Config, sourceResource string, destResource s

op, err := source.MigrateInstance(sourceName, req)
if err != nil {
return errors.Wrap(err, i18n.G("Migration API failure"))
return fmt.Errorf(i18n.G("Migration API failure: %w"), err)
}

err = op.Wait()
if err != nil {
return errors.Wrap(err, i18n.G("Migration operation failure"))
return fmt.Errorf(i18n.G("Migration operation failure: %w"), err)
}

return nil
Expand Down Expand Up @@ -401,7 +400,7 @@ func moveInstanceProject(conf *config.Config, sourceResource string, destResourc
// Connect to the source host.
source, err := conf.GetInstanceServer(sourceRemote)
if err != nil {
return errors.Wrap(err, i18n.G("Failed to connect to cluster member"))
return fmt.Errorf(i18n.G("Failed to connect to cluster member: %w"), err)
}

// Pass the new project to the migration API.
Expand All @@ -415,12 +414,12 @@ func moveInstanceProject(conf *config.Config, sourceResource string, destResourc

op, err := source.MigrateInstance(sourceName, req)
if err != nil {
return errors.Wrap(err, i18n.G("Migration API failure"))
return fmt.Errorf(i18n.G("Migration API failure: %w"), err)
}

err = op.Wait()
if err != nil {
return errors.Wrap(err, i18n.G("Migration operation failure"))
return fmt.Errorf(i18n.G("Migration operation failure: %w"), err)
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions lxc/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"
"time"

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

"github.com/lxc/lxd/client"
Expand Down Expand Up @@ -220,7 +219,7 @@ func (c *cmdPublish) Run(cmd *cobra.Command, args []string) error {
if c.flagExpiresAt != "" {
expiresAt, err := time.Parse(time.RFC3339, c.flagExpiresAt)
if err != nil {
return errors.Wrapf(err, "Invalid expiration date")
return fmt.Errorf("Invalid expiration date: %w", err)
}
req.ExpiresAt = expiresAt
}
Expand Down
5 changes: 2 additions & 3 deletions lxc/storage_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -1982,7 +1981,7 @@ func (c *cmdStorageVolumeExport) Run(cmd *cobra.Command, args []string) error {

op, err := d.CreateStoragePoolVolumeBackup(name, volName, req)
if err != nil {
return errors.Wrap(err, "Failed to create storage volume backup")
return fmt.Errorf("Failed to create storage volume backup: %w", err)
}

// Watch the background operation
Expand Down Expand Up @@ -2050,7 +2049,7 @@ func (c *cmdStorageVolumeExport) Run(cmd *cobra.Command, args []string) error {
if err != nil {
os.Remove(targetName)
progress.Done("")
return errors.Wrap(err, "Fetch storage volume backup file")
return fmt.Errorf("Fetch storage volume backup file: %w", err)
}

progress.Done(i18n.G("Backup exported successfully!"))
Expand Down
7 changes: 3 additions & 4 deletions lxc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/fvbommel/sortorder"
"github.com/pkg/errors"

"github.com/lxc/lxd/client"
"github.com/lxc/lxd/shared/api"
Expand Down Expand Up @@ -236,7 +235,7 @@ func getConfig(args ...string) (map[string]string, error) {
if args[1] == "-" && !termios.IsTerminal(getStdinFd()) {
buf, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, errors.Wrap(err, i18n.G("Can't read from stdin: %s"))
return nil, fmt.Errorf(i18n.G("Can't read from stdin: %w"), err)
}

args[1] = string(buf[:])
Expand All @@ -250,13 +249,13 @@ func getConfig(args ...string) (map[string]string, error) {
for _, arg := range args {
fields := strings.SplitN(arg, "=", 2)
if len(fields) != 2 {
return nil, fmt.Errorf(i18n.G("Invalid key=value configuration: %s"), arg)
return nil, fmt.Errorf(i18n.G("Invalid key=value configuration: %w"), arg)
}

if fields[1] == "-" && !termios.IsTerminal(getStdinFd()) {
buf, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, fmt.Errorf(i18n.G("Can't read from stdin: %s"), err)
return nil, fmt.Errorf(i18n.G("Can't read from stdin: %w"), err)
}

fields[1] = string(buf[:])
Expand Down
9 changes: 4 additions & 5 deletions lxd-agent/main_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sync"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"

Expand Down Expand Up @@ -112,7 +111,7 @@ func (c *cmdAgent) Run(cmd *cobra.Command, args []string) error {
logger.Info("Loading vsock module")
err = util.LoadModule("vsock")
if err != nil {
return errors.Wrap(err, "Unable to load the vsock kernel module")
return fmt.Errorf("Unable to load the vsock kernel module: %w", err)
}

// Wait for vsock device to appear.
Expand All @@ -125,7 +124,7 @@ func (c *cmdAgent) Run(cmd *cobra.Command, args []string) error {
// Setup the listener.
l, err := vsock.Listen(shared.HTTPSDefaultPort)
if err != nil {
return errors.Wrap(err, "Failed to listen on vsock")
return fmt.Errorf("Failed to listen on vsock: %w", err)
}
logger.Info("Started vsock listener")

Expand All @@ -142,12 +141,12 @@ func (c *cmdAgent) Run(cmd *cobra.Command, args []string) error {
// Load the expected server certificate.
cert, err := shared.ReadCert("server.crt")
if err != nil {
return errors.Wrap(err, "Failed to read client certificate")
return fmt.Errorf("Failed to read client certificate: %w", err)
}

tlsConfig, err := serverTLSConfig()
if err != nil {
return errors.Wrap(err, "Failed to get TLS config")
return fmt.Errorf("Failed to get TLS config: %w", err)
}

d := newDaemon(c.global.flagLogDebug, c.global.flagLogVerbose)
Expand Down
4 changes: 2 additions & 2 deletions lxd-agent/operations.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"fmt"
"log"
"net/http"
"strings"

"github.com/gorilla/mux"
"github.com/pkg/errors"

"github.com/lxc/lxd/lxd/operations"
"github.com/lxc/lxd/lxd/response"
Expand Down Expand Up @@ -63,7 +63,7 @@ func operationGet(d *Daemon, r *http.Request) response.Response {

_, body, err = op.Render()
if err != nil {
log.Println(errors.Wrap(err, "Failed to handle operations request"))
log.Println(fmt.Errorf("Failed to handle operations request: %w", err))
}

return response.SyncResponse(true, body)
Expand Down
Loading

0 comments on commit 55c7e31

Please sign in to comment.