cli-plugins/manager: use stdlib errors, and minor cleanup

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-03-07 19:14:14 +01:00
parent e201b4e8a5
commit 8fc0c74f9a
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 13 additions and 18 deletions

View File

@ -4,7 +4,7 @@
package manager package manager
import ( import (
"github.com/pkg/errors" "fmt"
) )
// pluginError is set as Plugin.Err by NewPlugin if the plugin // pluginError is set as Plugin.Err by NewPlugin if the plugin
@ -39,16 +39,16 @@ func (e *pluginError) MarshalText() (text []byte, err error) {
} }
// wrapAsPluginError wraps an error in a pluginError with an // wrapAsPluginError wraps an error in a pluginError with an
// additional message, analogous to errors.Wrapf. // additional message.
func wrapAsPluginError(err error, msg string) error { func wrapAsPluginError(err error, msg string) error {
if err == nil { if err == nil {
return nil return nil
} }
return &pluginError{cause: errors.Wrap(err, msg)} return &pluginError{cause: fmt.Errorf("%s: %w", msg, err)}
} }
// NewPluginError creates a new pluginError, analogous to // NewPluginError creates a new pluginError, analogous to
// errors.Errorf. // errors.Errorf.
func NewPluginError(msg string, args ...any) error { func NewPluginError(msg string, args ...any) error {
return &pluginError{cause: errors.Errorf(msg, args...)} return &pluginError{cause: fmt.Errorf(msg, args...)}
} }

View File

@ -3,6 +3,8 @@ package manager
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -10,7 +12,6 @@ import (
"strings" "strings"
"github.com/docker/cli/cli-plugins/metadata" "github.com/docker/cli/cli-plugins/metadata"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -45,14 +46,14 @@ func newPlugin(c Candidate, cmds []*cobra.Command) (Plugin, error) {
// which would fail here, so there are all real errors. // which would fail here, so there are all real errors.
fullname := filepath.Base(path) fullname := filepath.Base(path)
if fullname == "." { if fullname == "." {
return Plugin{}, errors.Errorf("unable to determine basename of plugin candidate %q", path) return Plugin{}, fmt.Errorf("unable to determine basename of plugin candidate %q", path)
} }
var err error var err error
if fullname, err = trimExeSuffix(fullname); err != nil { if fullname, err = trimExeSuffix(fullname); err != nil {
return Plugin{}, errors.Wrapf(err, "plugin candidate %q", path) return Plugin{}, fmt.Errorf("plugin candidate %q: %w", path, err)
} }
if !strings.HasPrefix(fullname, metadata.NamePrefix) { if !strings.HasPrefix(fullname, metadata.NamePrefix) {
return Plugin{}, errors.Errorf("plugin candidate %q: does not have %q prefix", path, metadata.NamePrefix) return Plugin{}, fmt.Errorf("plugin candidate %q: does not have %q prefix", path, metadata.NamePrefix)
} }
p := Plugin{ p := Plugin{

View File

@ -1,22 +1,16 @@
package manager package manager
import ( import (
"fmt"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/pkg/errors"
) )
// This is made slightly more complex due to needing to be case insensitive. // This is made slightly more complex due to needing to be case-insensitive.
func trimExeSuffix(s string) (string, error) { func trimExeSuffix(s string) (string, error) {
ext := filepath.Ext(s) ext := filepath.Ext(s)
if ext == "" { if ext == "" || !strings.EqualFold(ext, ".exe") {
return "", errors.Errorf("path %q lacks required file extension", s) return "", fmt.Errorf("path %q lacks required file extension (.exe)", s)
}
exe := ".exe"
if !strings.EqualFold(ext, exe) {
return "", errors.Errorf("path %q lacks required %q suffix", s, exe)
} }
return strings.TrimSuffix(s, ext), nil return strings.TrimSuffix(s, ext), nil
} }