Skip to content

Commit 788bc74

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 788bc74

File tree

4 files changed

+451
-12
lines changed

4 files changed

+451
-12
lines changed

cli/command/registry/whoami.go

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

0 commit comments

Comments
 (0)