Skip to content

Commit

Permalink
chore: Switch to std lib errors package
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasProgrammer committed Dec 15, 2023
1 parent 97012db commit c023dd1
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 87 deletions.
8 changes: 4 additions & 4 deletions driver/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package driver
import (
"context"
"fmt"

"github.com/docker/machine/libmachine/log"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/pkg/errors"
)

func (d *Driver) destroyDangling() {
Expand Down Expand Up @@ -44,7 +44,7 @@ func (d *Driver) destroyServer() error {

srv, err := d.getServerHandleNullable()
if err != nil {
return errors.Wrap(err, "could not get server handle")
return fmt.Errorf("could not get server handle: %w", err)
}

if srv == nil {
Expand All @@ -54,7 +54,7 @@ func (d *Driver) destroyServer() error {

res, _, err := d.getClient().Server.DeleteWithResult(context.Background(), srv)
if err != nil {
return errors.Wrap(err, "could not delete server")
return fmt.Errorf("could not delete server: %w", err)
}

// failure to remove a placement group is not a hard error
Expand All @@ -64,7 +64,7 @@ func (d *Driver) destroyServer() error {

// wait for the server to actually be deleted
if err = d.waitForAction(res.Action); err != nil {
return errors.Wrap(err, "could not wait for deletion")
return fmt.Errorf("could not wait for deletion: %w", err)
}
}

Expand Down
42 changes: 21 additions & 21 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver

import (
"context"
"errors"
"fmt"
"net"
"strconv"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/pkg/errors"
)

// Driver contains hetzner-specific data to implement [drivers.Driver]
Expand Down Expand Up @@ -328,7 +328,7 @@ func flagI64(opts drivers.DriverOptions, key string) (int64, error) {

ret, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return 0, errors.Wrapf(err, "could not parse int64 for %v", key)
return 0, fmt.Errorf("could not parse int64 for %v: %w", key, err)
}

return ret, nil
Expand Down Expand Up @@ -439,17 +439,17 @@ func (d *Driver) PreCreateCheck() error {
}

if serverType, err := d.getType(); err != nil {
return errors.Wrap(err, "could not get type")
return fmt.Errorf("could not get type: %w", err)
} else if d.ImageArch != "" && serverType.Architecture != d.ImageArch {
log.Warnf("supplied architecture %v differs from server architecture %v", d.ImageArch, serverType.Architecture)
}

if _, err := d.getImage(); err != nil {
return errors.Wrap(err, "could not get image")
return fmt.Errorf("could not get image: %w", err)
}

if _, err := d.getLocationNullable(); err != nil {
return errors.Wrap(err, "could not get location")
return fmt.Errorf("could not get location: %w", err)
}

if _, err := d.getPlacementGroup(); err != nil {
Expand All @@ -465,7 +465,7 @@ func (d *Driver) PreCreateCheck() error {
}

if d.UsePrivateNetwork && len(d.Networks) == 0 {
return errors.Errorf("No private network attached.")
return fmt.Errorf("no private network attached")
}

return nil
Expand Down Expand Up @@ -494,12 +494,12 @@ func (d *Driver) Create() error {
srv, _, err := d.getClient().Server.Create(context.Background(), instrumented(*srvopts))
if err != nil {
time.Sleep(time.Duration(d.WaitOnError) * time.Second)
return errors.Wrap(err, "could not create server")
return fmt.Errorf("could not create server: %w", err)
}

log.Infof(" -> Creating server %s[%d] in %s[%d]", srv.Server.Name, srv.Server.ID, srv.Action.Command, srv.Action.ID)
if err = d.waitForAction(srv.Action); err != nil {
return errors.Wrap(err, "could not wait for action")
return fmt.Errorf("could not wait for action: %w", err)
}

d.ServerID = srv.Server.ID
Expand Down Expand Up @@ -530,12 +530,12 @@ func (d *Driver) GetSSHHostname() (string, error) {
// GetURL retrieves the URL of the docker daemon on the machine; see [drivers.Driver.GetURL]
func (d *Driver) GetURL() (string, error) {
if err := drivers.MustBeRunning(d); err != nil {
return "", errors.Wrap(err, "could not execute drivers.MustBeRunning")
return "", fmt.Errorf("could not execute drivers.MustBeRunning: %w", err)
}

ip, err := d.GetIP()
if err != nil {
return "", errors.Wrap(err, "could not get IP")
return "", fmt.Errorf("could not get IP: %w", err)
}

return fmt.Sprintf("tcp://%s", net.JoinHostPort(ip, "2376")), nil
Expand All @@ -545,7 +545,7 @@ func (d *Driver) GetURL() (string, error) {
func (d *Driver) GetState() (state.State, error) {
srv, _, err := d.getClient().Server.GetByID(context.Background(), d.ServerID)
if err != nil {
return state.None, errors.Wrap(err, "could not get server by ID")
return state.None, fmt.Errorf("could not get server by ID: %w", err)
}
if srv == nil {
return state.None, errors.New("server not found")
Expand Down Expand Up @@ -588,7 +588,7 @@ func (d *Driver) Remove() error {
if !d.IsExistingKey && d.KeyID != 0 {
key, err := d.getKeyNullable()
if err != nil {
return errors.Wrap(err, "could not get ssh key")
return fmt.Errorf("could not get ssh key: %w", err)
}
if key == nil {
log.Infof(" -> SSH key does not exist anymore")
Expand All @@ -598,7 +598,7 @@ func (d *Driver) Remove() error {
log.Infof(" -> Destroying SSHKey %s[%d]...", key.Name, key.ID)

if _, err := d.getClient().SSHKey.Delete(context.Background(), key); err != nil {
return errors.Wrap(err, "could not delete ssh key")
return fmt.Errorf("could not delete ssh key: %w", err)
}
}

Expand All @@ -609,15 +609,15 @@ func (d *Driver) Remove() error {
func (d *Driver) Restart() error {
srv, err := d.getServerHandle()
if err != nil {
return errors.Wrap(err, "could not get server handle")
return fmt.Errorf("could not get server handle: %w", err)
}
if srv == nil {
return errors.New("server not found")
}

act, _, err := d.getClient().Server.Reboot(context.Background(), srv)
if err != nil {
return errors.Wrap(err, "could not reboot server")
return fmt.Errorf("could not reboot server: %w", err)
}

log.Infof(" -> Rebooting server %s[%d] in %s[%d]...", srv.Name, srv.ID, act.Command, act.ID)
Expand All @@ -629,12 +629,12 @@ func (d *Driver) Restart() error {
func (d *Driver) Start() error {
srv, err := d.getServerHandle()
if err != nil {
return errors.Wrap(err, "could not get server handle")
return fmt.Errorf("could not get server handle: %w", err)
}

act, _, err := d.getClient().Server.Poweron(context.Background(), srv)
if err != nil {
return errors.Wrap(err, "could not power on server")
return fmt.Errorf("could not power on server: %w", err)
}

log.Infof(" -> Starting server %s[%d] in %s[%d]...", srv.Name, srv.ID, act.Command, act.ID)
Expand All @@ -646,12 +646,12 @@ func (d *Driver) Start() error {
func (d *Driver) Stop() error {
srv, err := d.getServerHandle()
if err != nil {
return errors.Wrap(err, "could not get server handle")
return fmt.Errorf("could not get server handle: %w", err)
}

act, _, err := d.getClient().Server.Shutdown(context.Background(), srv)
if err != nil {
return errors.Wrap(err, "could not shutdown server")
return fmt.Errorf("could not shutdown server: %w", err)
}

log.Infof(" -> Shutting down server %s[%d] in %s[%d]...", srv.Name, srv.ID, act.Command, act.ID)
Expand All @@ -663,12 +663,12 @@ func (d *Driver) Stop() error {
func (d *Driver) Kill() error {
srv, err := d.getServerHandle()
if err != nil {
return errors.Wrap(err, "could not get server handle")
return fmt.Errorf("could not get server handle: %w", err)
}

act, _, err := d.getClient().Server.Poweroff(context.Background(), srv)
if err != nil {
return errors.Wrap(err, "could not poweroff server")
return fmt.Errorf("could not poweroff server: %w", err)
}

log.Infof(" -> Powering off server %s[%d] in %s[%d]...", srv.Name, srv.ID, act.Command, act.ID)
Expand Down
7 changes: 4 additions & 3 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package driver

import (
"github.com/docker/machine/commands/commandstest"
"github.com/docker/machine/libmachine/drivers"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"os"
"strconv"
"strings"
"testing"

"github.com/docker/machine/commands/commandstest"
"github.com/docker/machine/libmachine/drivers"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var defaultFlags = map[string]interface{}{
Expand Down
5 changes: 3 additions & 2 deletions driver/flag_failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
package driver

import (
"fmt"

"github.com/docker/machine/libmachine/drivers"
"github.com/pkg/errors"
)

func (d *Driver) flagFailure(format string, args ...interface{}) error {
return errors.Errorf(format, args...)
return fmt.Errorf(format, args...)
}

func (d *Driver) setConfigFromFlags(opts drivers.DriverOptions) error {
Expand Down
3 changes: 1 addition & 2 deletions driver/flag_failure_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/docker/machine/libmachine/drivers"
"github.com/pkg/errors"
)

var lastOpts drivers.DriverOptions
Expand All @@ -23,7 +22,7 @@ func (d *Driver) flagFailure(format string, args ...interface{}) error {
}

combined := append([]interface{}{line1, line2}, args...)
return errors.Errorf("%s\n%s\n"+format, combined...)
return fmt.Errorf("%s\n%s\n"+format, combined...)
}

func (d *Driver) setConfigFromFlags(opts drivers.DriverOptions) error {
Expand Down
9 changes: 5 additions & 4 deletions driver/flag_processing.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package driver

import (
"fmt"
"strings"

"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/pkg/errors"
"strings"
)

var legacyDefaultImages = [...]string{
Expand Down Expand Up @@ -33,7 +34,7 @@ func (d *Driver) setImageArch(arch string) error {
case string(hcloud.ArchitectureX86):
d.ImageArch = hcloud.ArchitectureX86
default:
return errors.Errorf("unknown architecture %v", arch)
return fmt.Errorf("unknown architecture %v", arch)
}
return nil
}
Expand Down Expand Up @@ -112,7 +113,7 @@ func (d *Driver) setLabelsFromFlags(opts drivers.DriverOptions) error {
for _, label := range opts.StringSlice(flagKeyLabel) {
split := strings.SplitN(label, "=", 2)
if len(split) != 2 {
return errors.Errorf("key label %v is not in key=value format", label)
return fmt.Errorf("key label %v is not in key=value format", label)
}
d.keyLabels[split[0]] = split[1]
}
Expand Down
Loading

0 comments on commit c023dd1

Please sign in to comment.