-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhoami.go
More file actions
54 lines (45 loc) · 1.35 KB
/
Copy pathwhoami.go
File metadata and controls
54 lines (45 loc) · 1.35 KB
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
48
49
50
51
52
53
54
package auth
import (
"fmt"
"time"
"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
"github.com/NodeOps-app/createos-cli/internal/api"
)
// NewWhoamiCommand creates the whoami command
func NewWhoamiCommand() *cli.Command {
return &cli.Command{
Name: "whoami",
Usage: "Show the currently authenticated user",
Action: func(c *cli.Context) error {
client, ok := c.App.Metadata[api.ClientKey].(*api.APIClient)
if !ok {
return fmt.Errorf("you're not signed in — run 'createos login' to get started")
}
u, err := client.GetUser()
if err != nil {
return err
}
greeting := "Hey, " + u.Email + "!"
if u.DisplayName != nil && *u.DisplayName != "" {
greeting = "Hey, " + *u.DisplayName + "!"
}
createdAt, err := time.Parse(time.RFC3339Nano, u.CreatedAt)
memberSince := u.CreatedAt
if err == nil {
memberSince = createdAt.Local().Format("January 2, 2006")
}
fmt.Println()
pterm.NewStyle(pterm.FgLightCyan, pterm.Bold).Printfln(" %s", greeting)
fmt.Println()
pterm.Printfln(" %s %s", pterm.Gray("Email "), u.Email)
pterm.Printfln(" %s %s", pterm.Gray("ID "), u.ID)
if u.PlanName != "" {
pterm.Printfln(" %s %s", pterm.Gray("Plan "), u.PlanName)
}
pterm.Printfln(" %s %s", pterm.Gray("Member since "), memberSince)
fmt.Println()
return nil
},
}
}