Improve usability of `func` CLI for IDE plugins (#1091)

* Better input handling for non-tty

Signed-off-by: Matej Vasek <mvasek@redhat.com>

* Make cred-helper 'not implemented' non fatal error

Signed-off-by: Matej Vasek <mvasek@redhat.com>
This commit is contained in:
Matej Vasek 2022-07-01 07:07:42 +02:00 committed by GitHub
parent 5c211a6778
commit 80505979f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 12 deletions

View File

@ -318,18 +318,42 @@ you can install docker credential helper https://github.com/docker/docker-creden
return "", nil
}
isTerm := term.IsTerminal(int(os.Stdin.Fd()))
var resp string
err := survey.AskOne(&survey.Select{
Message: "Choose credentials helper",
Options: append(availableHelpers, "None"),
}, &resp, survey.WithValidator(survey.Required))
if err != nil {
return "", err
}
if resp == "None" {
fmt.Fprintf(os.Stderr, "No helper selected. Credentials will not be saved.\n")
return "", nil
if isTerm {
err := survey.AskOne(&survey.Select{
Message: "Choose credentials helper",
Options: append(availableHelpers, "None"),
}, &resp, survey.WithValidator(survey.Required))
if err != nil {
return "", err
}
if resp == "None" {
fmt.Fprintf(os.Stderr, "No helper selected. Credentials will not be saved.\n")
return "", nil
}
} else {
fmt.Fprintf(os.Stderr, "Available credential helpers:\n")
for _, helper := range availableHelpers {
fmt.Fprintf(os.Stderr, "%s\n", helper)
}
fmt.Fprintf(os.Stderr, "Choose credentials helper: ")
reader := bufio.NewReader(os.Stdin)
var err error
resp, err = reader.ReadString('\n')
if err != nil {
return "", err
}
resp = strings.Trim(resp, "\r\n")
if resp == "" {
fmt.Fprintf(os.Stderr, "No helper selected. Credentials will not be saved.\n")
}
}
return resp, nil
}
}

View File

@ -255,6 +255,13 @@ func (c *credentialsProvider) getCredentials(ctx context.Context, registry strin
if err == nil {
err = setCredentialsByCredentialHelper(c.authFilePath, registry, result.Username, result.Password)
if err != nil {
// This shouldn't be fatal error.
if strings.Contains(err.Error(), "not implemented") {
fmt.Fprintf(os.Stderr, "the cred-helper does not support write operation (consider changing the cred-helper it in auth.json)\n")
return docker.Credentials{}, nil
}
if !errors.Is(err, errNoCredentialHelperConfigured) {
return docker.Credentials{}, err
}
@ -269,8 +276,17 @@ func (c *credentialsProvider) getCredentials(ctx context.Context, registry strin
return docker.Credentials{}, fmt.Errorf("faild to set the helper to the config: %w", err)
}
err = setCredentialsByCredentialHelper(c.authFilePath, registry, result.Username, result.Password)
if err != nil && !errors.Is(err, errNoCredentialHelperConfigured) {
return docker.Credentials{}, err
if err != nil {
// This shouldn't be fatal error.
if strings.Contains(err.Error(), "not implemented") {
fmt.Fprintf(os.Stderr, "the cred-helper does not support write operation (consider changing the cred-helper it in auth.json)\n")
return docker.Credentials{}, nil
}
if !errors.Is(err, errNoCredentialHelperConfigured) {
return docker.Credentials{}, err
}
}
}
return result, nil