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

Commit

Permalink
scout hint: tell user logging in is required
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
laurazard committed Aug 22, 2023
1 parent d22f3a8 commit 397d109
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions cli/mobycli/scout_suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/docker/cli/cli/config"
"github.com/docker/compose/v2/pkg/utils"

"github.com/fatih/color"
Expand Down Expand Up @@ -53,10 +55,14 @@ func displayScoutQuickViewSuggestMsg(image string) {
}
out := os.Stderr
b := color.New(color.Bold)
_, _ = fmt.Fprintln(out)
_, _ = b.Fprintln(out, "What's Next?")
_, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations → %s", color.CyanString("docker scout quickview%s", image))
_, _ = fmt.Fprintln(out)

_, _ = b.Fprintln(out, "\nWhat's Next?")
if !hubLoggedIn() {
_, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations (login required) → %s\n", color.CyanString("docker scout quickview%s", image))
_, _ = fmt.Fprint(out, " To sign in, use the `docker login` command or sign in via Docker Desktop\n")
} else {
_, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations → %s\n", color.CyanString("docker scout quickview%s", image))
}
}

func pulledImageFromArgs(args []string) string {
Expand All @@ -74,3 +80,25 @@ func pulledImageFromArgs(args []string) string {
}
return image
}

// hubLoggedIn checks whether the user has credentials configured
// for Docker Hub. This can be an expensive operation, so use it
// mindfully.
func hubLoggedIn() bool {
result := make(chan bool)
go func() {
hubAuth, err := config.LoadDefaultConfigFile(nil).GetAuthConfig("index.docker.io")
if err != nil {
// preserve original behaviour if we fail to fetch creds
result <- true
}
result <- hubAuth.Username != ""
}()
select {
case loggedIn := <-result:
return loggedIn
case <-time.After(100 * time.Millisecond):
// preserve original behaviour if we time out
return true
}
}

0 comments on commit 397d109

Please sign in to comment.