Skip to content

Commit c365707

Browse files
Whoami: print as a table or json, add tenantID (#1431)
1 parent b06d693 commit c365707

File tree

5 files changed

+402
-390
lines changed

5 files changed

+402
-390
lines changed

src/cmd/cli/command/commands.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"io"
@@ -213,6 +214,7 @@ func SetupCommands(ctx context.Context, version string) {
213214
RootCmd.AddCommand(loginCmd)
214215

215216
// Whoami Command
217+
whoamiCmd.PersistentFlags().Bool("json", pkg.GetenvBool("DEFANG_JSON"), "print output in JSON format")
216218
RootCmd.AddCommand(whoamiCmd)
217219

218220
// Logout Command
@@ -434,13 +436,30 @@ var whoamiCmd = &cobra.Command{
434436
term.Debug("unable to get provider:", err)
435437
}
436438

437-
str, err := cli.Whoami(cmd.Context(), client, provider)
439+
jsonMode, _ := cmd.Flags().GetBool("json")
440+
441+
data, err := cli.Whoami(cmd.Context(), client, provider)
438442
if err != nil {
439443
return err
440444
}
441445

442-
term.Info(str)
443-
return nil
446+
if jsonMode {
447+
bytes, err := json.Marshal(data)
448+
if err != nil {
449+
return err
450+
}
451+
_, err = fmt.Println(string(bytes))
452+
return err
453+
} else {
454+
return term.Table([]cli.ShowAccountData{data}, []string{
455+
"Provider",
456+
"AccountID",
457+
"Tenant",
458+
"TenantID",
459+
"Subscription Tier",
460+
"Region",
461+
})
462+
}
444463
},
445464
}
446465

src/pkg/cli/whoami.go

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cli
33
import (
44
"context"
55

6-
"github.com/DefangLabs/defang/src/pkg"
76
"github.com/DefangLabs/defang/src/pkg/cli/client"
87
"github.com/DefangLabs/defang/src/pkg/term"
98

@@ -14,55 +13,30 @@ type ShowAccountData struct {
1413
client.AccountInfo
1514
SubscriberTier defangv1.SubscriptionTier
1615
Tenant string
16+
TenantID string
1717
}
1818

19-
func showAccountInfo(showData ShowAccountData) (string, error) {
20-
if showData.Provider == "" {
21-
showData.Provider = "Defang"
22-
}
23-
outputText := "WhoAmI - \n\tProvider: " + showData.Provider.Name()
24-
25-
if showData.AccountID != "" {
26-
outputText += "\n\tAccountID: " + showData.AccountID
27-
}
28-
29-
if showData.Tenant != "" {
30-
outputText += "\n\tTenant: " + showData.Tenant
31-
}
32-
33-
outputText += "\n\tSubscription Tier: " + pkg.SubscriptionTierToString(showData.SubscriberTier)
34-
35-
if showData.Region != "" {
36-
outputText += "\n\tRegion: " + showData.Region
37-
}
38-
39-
if showData.Details != "" {
40-
outputText += "\n\tDetails: " + showData.Details
41-
}
42-
43-
return outputText, nil
44-
}
45-
46-
func Whoami(ctx context.Context, fabric client.FabricClient, provider client.Provider) (string, error) {
19+
func Whoami(ctx context.Context, fabric client.FabricClient, provider client.Provider) (ShowAccountData, error) {
4720
showData := ShowAccountData{}
4821

4922
resp, err := fabric.WhoAmI(ctx)
5023
if err != nil {
51-
return "", err
24+
return ShowAccountData{}, err
5225
}
5326

5427
term.Debug("User ID: " + resp.UserId)
5528
showData.Region = resp.Region
5629
showData.SubscriberTier = resp.Tier
5730
showData.Tenant = resp.Tenant
31+
showData.TenantID = resp.TenantId
5832

5933
if provider != nil {
6034
account, err := provider.AccountInfo(ctx)
6135
if err != nil {
62-
return "", err
36+
return ShowAccountData{}, err
6337
}
6438
showData.AccountInfo = *account
6539
}
6640

67-
return showAccountInfo(showData)
41+
return showData, err
6842
}

src/pkg/cli/whoami_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@ func TestWhoami(t *testing.T) {
4444
}
4545

4646
// Playground provider is hardcoded to return "us-west-2" as the region
47-
want := "WhoAmI - \n\tProvider: Defang Playground\n\tAccountID: tenant-1\n\tTenant: tenant-1\n\tSubscription Tier: Pro\n\tRegion: us-west-2"
47+
want := ShowAccountData{
48+
AccountInfo: cliClient.AccountInfo{
49+
AccountID: "tenant-1",
50+
Provider: "defang",
51+
Region: "us-west-2",
52+
},
53+
SubscriberTier: defangv1.SubscriptionTier_PRO,
54+
Tenant: "tenant-1",
55+
TenantID: "",
56+
}
4857

4958
if got != want {
5059
t.Errorf("Whoami() = %v, \nwant: %v", got, want)

0 commit comments

Comments
 (0)