Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

fix: use a better error message on WSL #77

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions internal/executer/executer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
"os/exec"
)

// dockerDesktopHelperName is the name of the docker credentials helper
// execuatable.
const dockerDesktopHelperName = "docker-credential-desktop.exe"

// Executer is an interface that simulates an executable binary.
type Executer interface {
Execute(ctx context.Context, input io.Reader, action string) ([]byte, error)
Expand All @@ -50,9 +54,15 @@ func (c *executable) Execute(ctx context.Context, input io.Reader, action string
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
switch execErr := err.(type) {
case *exec.ExitError:
if errMessage := string(bytes.TrimSpace(output)); errMessage != "" {
err = errors.New(errMessage)
return nil, errors.New(errMessage)
}
case *exec.Error:
// check if the error is caused by Docker Desktop not running
if execErr.Err == exec.ErrNotFound && c.name == dockerDesktopHelperName {
return nil, errors.New("credentials store is configured to `desktop.exe` but Docker Desktop seems not running")
}
}
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func Login(ctx context.Context, store Store, reg *remote.Registry, cred auth.Cre
authClient.Credential = auth.StaticCredential(reg.Reference.Registry, cred)
// validate and store the credential
if err := regClone.Ping(ctx); err != nil {
return fmt.Errorf("failed to validate the credential for %s: %w", regClone.Reference.Registry, err)
return fmt.Errorf("failed to validate the credentials for %s: %w", regClone.Reference.Registry, err)
}
hostname := mapStoreRegistryName(regClone.Reference.Registry)
if err := store.Put(ctx, hostname, cred); err != nil {
return fmt.Errorf("failed to store the credential for %s: %w", hostname, err)
return fmt.Errorf("failed to store the credentials for %s: %w", hostname, err)
}
return nil
}
Expand Down