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

Commit 397d109

Browse files
committed
scout hint: tell user logging in is required
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 397d109

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

cli/mobycli/scout_suggest.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ 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"
2527

2628
"github.com/fatih/color"
@@ -53,10 +55,14 @@ func displayScoutQuickViewSuggestMsg(image string) {
5355
}
5456
out := os.Stderr
5557
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)
58+
59+
_, _ = b.Fprintln(out, "\nWhat's Next?")
60+
if !hubLoggedIn() {
61+
_, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations (login required) → %s\n", color.CyanString("docker scout quickview%s", image))
62+
_, _ = fmt.Fprint(out, " To sign in, use the `docker login` command or sign in via Docker Desktop\n")
63+
} else {
64+
_, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations → %s\n", color.CyanString("docker scout quickview%s", image))
65+
}
6066
}
6167

6268
func pulledImageFromArgs(args []string) string {
@@ -74,3 +80,25 @@ func pulledImageFromArgs(args []string) string {
7480
}
7581
return image
7682
}
83+
84+
// hubLoggedIn checks whether the user has credentials configured
85+
// for Docker Hub. This can be an expensive operation, so use it
86+
// mindfully.
87+
func hubLoggedIn() bool {
88+
result := make(chan bool)
89+
go func() {
90+
hubAuth, err := config.LoadDefaultConfigFile(nil).GetAuthConfig("index.docker.io")
91+
if err != nil {
92+
// preserve original behaviour if we fail to fetch creds
93+
result <- true
94+
}
95+
result <- hubAuth.Username != ""
96+
}()
97+
select {
98+
case loggedIn := <-result:
99+
return loggedIn
100+
case <-time.After(100 * time.Millisecond):
101+
// preserve original behaviour if we time out
102+
return true
103+
}
104+
}

0 commit comments

Comments
 (0)