filestore: don't print warning multiple times

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
This commit is contained in:
Laura Brehm 2024-08-05 12:16:02 +01:00
parent e6624676e0
commit 5eb3275c28
No known key found for this signature in database
GPG Key ID: 08EC1B0491948487
1 changed files with 7 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"net/url" "net/url"
"os" "os"
"strings" "strings"
"sync/atomic"
"github.com/docker/cli/cli/config/types" "github.com/docker/cli/cli/config/types"
) )
@ -65,6 +66,10 @@ Configure a credential helper to remove this warning. See
https://docs.docker.com/go/credential-store/ https://docs.docker.com/go/credential-store/
` `
// alreadyPrinted ensures that we only print the unencryptedWarning once per
// CLI invocation (no need to warn the user multiple times per command).
var alreadyPrinted atomic.Bool
// Store saves the given credentials in the file store. // Store saves the given credentials in the file store.
func (c *fileStore) Store(authConfig types.AuthConfig) error { func (c *fileStore) Store(authConfig types.AuthConfig) error {
authConfigs := c.file.GetAuthConfigs() authConfigs := c.file.GetAuthConfigs()
@ -73,11 +78,12 @@ func (c *fileStore) Store(authConfig types.AuthConfig) error {
return err return err
} }
if authConfig.Password != "" { if !alreadyPrinted.Load() && authConfig.Password != "" {
// Display a warning if we're storing the users password (not a token). // Display a warning if we're storing the users password (not a token).
// //
// FIXME(thaJeztah): make output configurable instead of hardcoding to os.Stderr // FIXME(thaJeztah): make output configurable instead of hardcoding to os.Stderr
_, _ = fmt.Fprintln(os.Stderr, fmt.Sprintf(unencryptedWarning, c.file.GetFilename())) _, _ = fmt.Fprintln(os.Stderr, fmt.Sprintf(unencryptedWarning, c.file.GetFilename()))
alreadyPrinted.Store(true)
} }
return nil return nil