-
Notifications
You must be signed in to change notification settings - Fork 238
/
whoami.go
47 lines (36 loc) · 995 Bytes
/
whoami.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package auth
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/render"
)
func newWhoAmI() *cobra.Command {
const (
long = `Displays the users email address/service identity currently
authenticated and in use.
`
short = "Show the currently authenticated user"
)
return command.New("whoami", long, short, runWhoAmI,
command.RequireSession)
}
func runWhoAmI(ctx context.Context) error {
client := client.FromContext(ctx).API()
user, err := client.GetCurrentUser(ctx)
if err != nil {
return fmt.Errorf("failed retrieving current user: %w", err)
}
io := iostreams.FromContext(ctx)
cfg := config.FromContext(ctx)
if cfg.JSONOutput {
_ = render.JSON(io.Out, map[string]string{"email": user.Email})
} else {
fmt.Fprintln(io.Out, user.Email)
}
return nil
}