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

Commit

Permalink
fix: use a better error message on WSL (#77)
Browse files Browse the repository at this point in the history
Resolves #26

Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 authored Jun 19, 2023
1 parent 6d14c42 commit 26b25ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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 := ServerAddressFromRegistry(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

0 comments on commit 26b25ce

Please sign in to comment.