|
| 1 | +package whoami |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/MakeNowJust/heredoc" |
| 9 | + "github.com/deepsourcelabs/cli/config" |
| 10 | + "github.com/deepsourcelabs/cli/internal/cli/args" |
| 11 | + "github.com/deepsourcelabs/cli/internal/cli/style" |
| 12 | + authsvc "github.com/deepsourcelabs/cli/internal/services/auth" |
| 13 | + "github.com/pterm/pterm" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +type WhoAmIOptions struct{} |
| 18 | + |
| 19 | +// NewCmdWhoAmI shows the authenticated user and accounts. |
| 20 | +func NewCmdWhoAmI() *cobra.Command { |
| 21 | + doc := heredoc.Docf(` |
| 22 | + Show the authenticated user and available accounts. |
| 23 | +
|
| 24 | + Use %[1]s to view the current user. |
| 25 | + `, style.Cyan("deepsource auth whoami")) |
| 26 | + |
| 27 | + cmd := &cobra.Command{ |
| 28 | + Use: "whoami", |
| 29 | + Short: "Show the current user", |
| 30 | + Long: doc, |
| 31 | + Args: args.NoArgs, |
| 32 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 33 | + opts := WhoAmIOptions{} |
| 34 | + return opts.Run(cmd.Context()) |
| 35 | + }, |
| 36 | + } |
| 37 | + return cmd |
| 38 | +} |
| 39 | + |
| 40 | +func (opts *WhoAmIOptions) Run(ctx context.Context) error { |
| 41 | + svc := authsvc.NewService(config.DefaultManager()) |
| 42 | + cfg, err := svc.LoadConfig() |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("Error while reading DeepSource CLI config : %v", err) |
| 45 | + } |
| 46 | + if err := cfg.VerifyAuthentication(); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + user, err := svc.GetViewer(ctx, cfg) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + fullName := strings.TrimSpace(strings.Join([]string{user.FirstName, user.LastName}, " ")) |
| 56 | + if fullName == "" { |
| 57 | + fullName = "-" |
| 58 | + } |
| 59 | + |
| 60 | + pterm.DefaultSection.Println("User") |
| 61 | + userSummary := [][]string{ |
| 62 | + {"Name", fullName}, |
| 63 | + {"Email", user.Email}, |
| 64 | + {"ID", user.ID}, |
| 65 | + } |
| 66 | + pterm.DefaultTable.WithData(userSummary).WithBoxed(false).Render() |
| 67 | + |
| 68 | + if len(user.Accounts) == 0 { |
| 69 | + pterm.Println("Accounts: none") |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + pterm.Println("") |
| 74 | + pterm.DefaultSection.Println("Accounts") |
| 75 | + accountsTable := [][]string{{"Account", "Type", "VCS", "ID"}} |
| 76 | + for _, account := range user.Accounts { |
| 77 | + label := strings.TrimSpace(account.Login) |
| 78 | + if label == "" { |
| 79 | + label = "-" |
| 80 | + } |
| 81 | + accountsTable = append(accountsTable, []string{ |
| 82 | + label, |
| 83 | + account.Type, |
| 84 | + account.VCSProvider, |
| 85 | + account.ID, |
| 86 | + }) |
| 87 | + } |
| 88 | + pterm.DefaultTable.WithHasHeader(true).WithData(accountsTable).Render() |
| 89 | + return nil |
| 90 | +} |
0 commit comments