Skip to content

Commit a63d588

Browse files
Add whoami and issues browse commands, refactor output messages
- Add new `auth whoami` command to display authenticated user info - Add new `issues browse` command for opening issues in browser - Add GetViewer API method to fetch authenticated user details - Refactor output messages to use pterm.Print* instead of pterm.Info for consistency across auth, config, issues, and repo commands - Update dependencies (pterm, testify, bubbletea, lipgloss)
1 parent b5bfba0 commit a63d588

23 files changed

Lines changed: 15154 additions & 41 deletions

File tree

command/auth/auth.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/deepsourcelabs/cli/command/auth/logout"
88
"github.com/deepsourcelabs/cli/command/auth/refresh"
99
"github.com/deepsourcelabs/cli/command/auth/status"
10+
"github.com/deepsourcelabs/cli/command/auth/whoami"
1011
)
1112

1213
// Options holds the metadata.
@@ -22,5 +23,6 @@ func NewCmdAuth() *cobra.Command {
2223
cmd.AddCommand(logout.NewCmdLogout())
2324
cmd.AddCommand(refresh.NewCmdRefresh())
2425
cmd.AddCommand(status.NewCmdStatus())
26+
cmd.AddCommand(whoami.NewCmdWhoAmI())
2527
return cmd
2628
}

command/auth/logout/logout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ func (opts *LogoutOptions) Run() error {
5353
return err
5454
}
5555
}
56-
pterm.Info.Println("Logged out from DeepSource (deepsource.io)")
56+
pterm.Println("Logged out from DeepSource (deepsource.io)")
5757
return nil
5858
}

command/auth/refresh/refresh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ func (opts *RefreshOptions) Run() error {
7070
fmt.Println("Error in writing authentication data to a file. Exiting...")
7171
return err
7272
}
73-
pterm.Info.Println("Authentication successfully refreshed.")
73+
pterm.Println("Authentication successfully refreshed.")
7474
return nil
7575
}

command/auth/status/status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func (opts *AuthStatusOptions) Run() error {
5050

5151
// Check if the token has already expired
5252
if !cfg.IsExpired() {
53-
pterm.Info.Printf("Logged in to DeepSource as %s.\n", cfg.User)
53+
pterm.Printf("Logged in to DeepSource as %s.\n", cfg.User)
5454
} else {
55-
pterm.Info.Println("The authentication has expired. Run \"deepsource auth refresh\" to refresh the credentials.")
55+
pterm.Println("The authentication has expired. Run \"deepsource auth refresh\" to refresh the credentials.")
5656
}
5757
return nil
5858
}

command/auth/whoami/whoami.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

command/config/validate/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (o *Options) Run() error {
4343
}
4444

4545
// Just an info
46-
pterm.Info.Println("DeepSource config (.deepsource.toml) is mostly present in the root directory of the project.")
46+
pterm.Println("DeepSource config (.deepsource.toml) is mostly present in the root directory of the project.")
4747
fmt.Println()
4848

4949
// Extract the path of DeepSource config

0 commit comments

Comments
 (0)