Skip to content

Commit d77ab09

Browse files
cli/command/registry: add whoami command
Add a new 'docker whoami' command that displays the username of the currently logged-in user for Docker registries. The command follows existing patterns from login/logout commands. Features: - Display username for Docker Hub by default - Support specifying a custom registry as an argument - Add --all flag to list all authenticated registries with usernames - Proper error handling for non-authenticated registries - Comprehensive test coverage with 13 test cases Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent 0b9d198 commit d77ab09

File tree

2 files changed

+434
-0
lines changed

2 files changed

+434
-0
lines changed

cli/command/registry/whoami.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package registry
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/docker/cli/cli"
8+
"github.com/docker/cli/cli/command"
9+
"github.com/docker/cli/cli/config/credentials"
10+
"github.com/docker/cli/internal/commands"
11+
"github.com/docker/cli/internal/registry"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func init() {
16+
commands.Register(newWhoamiCommand)
17+
}
18+
19+
type whoamiOptions struct {
20+
serverAddress string
21+
all bool
22+
}
23+
24+
// newWhoamiCommand creates a new `docker whoami` command
25+
func newWhoamiCommand(dockerCLI command.Cli) *cobra.Command {
26+
var opts whoamiOptions
27+
28+
cmd := &cobra.Command{
29+
Use: "whoami [SERVER]",
30+
Short: "Display the username of the currently logged in user",
31+
Long: "Display the username of the currently logged in user.\nDefaults to Docker Hub if no server is specified.",
32+
Args: cli.RequiresMaxArgs(1),
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
if len(args) > 0 {
35+
opts.serverAddress = args[0]
36+
}
37+
return runWhoami(cmd.Context(), dockerCLI, opts)
38+
},
39+
Annotations: map[string]string{
40+
"category-top": "10",
41+
},
42+
ValidArgsFunction: cobra.NoFileCompletions,
43+
DisableFlagsInUseLine: true,
44+
}
45+
46+
flags := cmd.Flags()
47+
flags.BoolVar(&opts.all, "all", false, "Display usernames for all authenticated registries")
48+
49+
return cmd
50+
}
51+
52+
func runWhoami(ctx context.Context, dockerCLI command.Cli, opts whoamiOptions) error {
53+
maybePrintEnvAuthWarning(dockerCLI)
54+
55+
if opts.all {
56+
return runWhoamiAll(dockerCLI)
57+
}
58+
return runWhoamiSingle(dockerCLI, opts)
59+
}
60+
61+
func runWhoamiSingle(dockerCLI command.Cli, opts whoamiOptions) error {
62+
serverAddress := opts.serverAddress
63+
if serverAddress == "" || serverAddress == registry.DefaultNamespace {
64+
serverAddress = registry.IndexServer
65+
} else {
66+
serverAddress = credentials.ConvertToHostname(serverAddress)
67+
}
68+
69+
authConfig, err := dockerCLI.ConfigFile().GetAuthConfig(serverAddress)
70+
if err != nil {
71+
return err
72+
}
73+
74+
if authConfig.Username == "" {
75+
registryName := "Docker Hub"
76+
if opts.serverAddress != "" && opts.serverAddress != registry.DefaultNamespace {
77+
registryName = serverAddress
78+
}
79+
return fmt.Errorf("not logged in to %s", registryName)
80+
}
81+
82+
fmt.Fprintln(dockerCLI.Out(), authConfig.Username)
83+
return nil
84+
}
85+
86+
func runWhoamiAll(dockerCLI command.Cli) error {
87+
credentials, err := dockerCLI.ConfigFile().GetAllCredentials()
88+
if err != nil {
89+
return err
90+
}
91+
92+
if len(credentials) == 0 {
93+
return fmt.Errorf("not logged in to any registries")
94+
}
95+
96+
for registry, authConfig := range credentials {
97+
if authConfig.Username != "" {
98+
fmt.Fprintf(dockerCLI.Out(), "%s: %s\n", registry, authConfig.Username)
99+
}
100+
}
101+
102+
return nil
103+
}

0 commit comments

Comments
 (0)