Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 8a400d0

Browse files
authored
scout hint: tell user logging in is required (#2262)
The `docker scout quickview` hint, displayed after a user pulls or builds an image, only works if the user is logged in to Hub. Check if user isn't logged in, and make hint more explicit in this case. Signed-off-by: Laura Brehm <[email protected]>
1 parent d22f3a8 commit 8a400d0

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

cli/mobycli/scout_suggest.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ import (
2020
"fmt"
2121
"os"
2222
"strings"
23+
"time"
2324

25+
"github.com/docker/cli/cli/config"
2426
"github.com/docker/compose/v2/pkg/utils"
27+
"github.com/docker/docker/registry"
2528

2629
"github.com/fatih/color"
2730
)
@@ -53,10 +56,14 @@ func displayScoutQuickViewSuggestMsg(image string) {
5356
}
5457
out := os.Stderr
5558
b := color.New(color.Bold)
56-
_, _ = fmt.Fprintln(out)
57-
_, _ = b.Fprintln(out, "What's Next?")
58-
_, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations → %s", color.CyanString("docker scout quickview%s", image))
59-
_, _ = fmt.Fprintln(out)
59+
60+
_, _ = b.Fprintln(out, "\nWhat's Next?")
61+
if !hubLoggedIn() {
62+
_, _ = fmt.Fprintln(out, " 1. Sign in to your Docker account → "+color.CyanString("docker login"))
63+
_, _ = fmt.Fprintln(out, " 2. View a summary of image vulnerabilities and recommendations → "+color.CyanString("docker scout quickview"+image))
64+
} else {
65+
_, _ = fmt.Fprintln(out, " View a summary of image vulnerabilities and recommendations → "+color.CyanString("docker scout quickview"+image))
66+
}
6067
}
6168

6269
func pulledImageFromArgs(args []string) string {
@@ -74,3 +81,27 @@ func pulledImageFromArgs(args []string) string {
7481
}
7582
return image
7683
}
84+
85+
// hubLoggedIn checks whether the user has credentials configured
86+
// for Docker Hub. If it fails to get a result within 100ms, it
87+
// short-circuits and returns `true`.
88+
// This can be an expensive operation, so use it mindfully.
89+
func hubLoggedIn() bool {
90+
result := make(chan bool)
91+
go func() {
92+
creds, err := config.LoadDefaultConfigFile(nil).GetAllCredentials()
93+
if err != nil {
94+
// preserve original behaviour if we fail to fetch creds
95+
result <- true
96+
}
97+
_, ok := creds[registry.IndexServer]
98+
result <- ok
99+
}()
100+
select {
101+
case loggedIn := <-result:
102+
return loggedIn
103+
case <-time.After(100 * time.Millisecond):
104+
// preserve original behaviour if we time out
105+
return true
106+
}
107+
}

0 commit comments

Comments
 (0)