MCPcopy Index your code
hub / github.com/docker/docker-agent / newDebugAuthCmd

Function newDebugAuthCmd

cmd/root/debug_auth.go:28–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26}
27
28func newDebugAuthCmd() *cobra.Command {
29 var jsonOutput bool
30
31 cmd := &cobra.Command{
32 Use: "auth",
33 Short: "Print Docker Desktop authentication information",
34 Args: cobra.NoArgs,
35 RunE: func(cmd *cobra.Command, _ []string) (commandErr error) {
36 ctx := cmd.Context()
37 telemetry.TrackCommand(ctx, "debug", []string{"auth"})
38 defer func() { // do not inline this defer so that commandErr is not resolved early
39 telemetry.TrackCommandError(ctx, "debug", []string{"auth"}, commandErr)
40 }()
41
42 w := cmd.OutOrStdout()
43
44 token := desktop.GetToken(ctx)
45 if token == "" {
46 if jsonOutput {
47 return json.NewEncoder(w).Encode(map[string]string{
48 "error": "no token found (is Docker Desktop running and are you logged in?)",
49 })
50 }
51 fmt.Fprintln(w, "No token found. Is Docker Desktop running and are you logged in?")
52 return nil
53 }
54
55 info, err := parseAuthInfo(token)
56 if err != nil {
57 return fmt.Errorf("failed to parse JWT: %w", err)
58 }
59
60 userInfo := desktop.GetUserInfo(ctx)
61 info.Username = userInfo.Username
62 info.Email = userInfo.Email
63
64 if jsonOutput {
65 enc := json.NewEncoder(w)
66 enc.SetIndent("", " ")
67 return enc.Encode(info)
68 }
69
70 printAuthInfoText(w, info)
71 return nil
72 },
73 }
74
75 cmd.Flags().BoolVar(&jsonOutput, "json", false, "Output in JSON format")
76
77 return cmd
78}
79
80func parseAuthInfo(token string) (*authInfo, error) {
81 parsed, _, err := jwt.NewParser().ParseUnverified(token, jwt.MapClaims{})

Callers 1

newDebugCmdFunction · 0.85

Calls 7

TrackCommandFunction · 0.92
TrackCommandErrorFunction · 0.92
GetTokenFunction · 0.92
GetUserInfoFunction · 0.92
parseAuthInfoFunction · 0.85
printAuthInfoTextFunction · 0.85
ContextMethod · 0.80

Tested by

no test coverage detected