@@ -20,8 +20,11 @@ import (
20
20
"fmt"
21
21
"os"
22
22
"strings"
23
+ "time"
23
24
25
+ "github.com/docker/cli/cli/config"
24
26
"github.com/docker/compose/v2/pkg/utils"
27
+ "github.com/docker/docker/registry"
25
28
26
29
"github.com/fatih/color"
27
30
)
@@ -53,10 +56,14 @@ func displayScoutQuickViewSuggestMsg(image string) {
53
56
}
54
57
out := os .Stderr
55
58
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 , "\n What'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
+ }
60
67
}
61
68
62
69
func pulledImageFromArgs (args []string ) string {
@@ -74,3 +81,27 @@ func pulledImageFromArgs(args []string) string {
74
81
}
75
82
return image
76
83
}
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