-
Notifications
You must be signed in to change notification settings - Fork 32
/
invites_list.go
105 lines (89 loc) · 2.6 KB
/
invites_list.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package cmd
import (
"context"
"fmt"
"os"
"text/tabwriter"
"time"
"github.com/urfave/cli"
"github.com/arigatomachine/cli/api"
"github.com/arigatomachine/cli/config"
"github.com/arigatomachine/cli/errs"
"github.com/arigatomachine/cli/identity"
)
func invitesList(ctx *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}
client := api.NewClient(cfg)
org, err := client.Orgs.GetByName(context.Background(), ctx.String("org"))
if err != nil {
return errs.NewExitError("Could not retrieve org information.")
}
if org == nil {
return errs.NewExitError("Org not found.")
}
var states []string
if ctx.Bool("approved") {
states = []string{"approved"}
} else {
states = []string{"pending", "associated", "accepted"}
}
invites, err := client.Invites.List(context.Background(), org.ID, states)
if err != nil {
return errs.NewExitError("Failed to retrieve invites, please try again.")
}
if len(invites) < 1 {
fmt.Println("No invites found.")
return nil
}
inviteUserIDs := make(map[identity.ID]bool)
for _, invite := range invites {
if invite.Body.InviteeID != nil {
inviteUserIDs[*invite.Body.InviteeID] = true
}
if invite.Body.ApproverID != nil {
inviteUserIDs[*invite.Body.ApproverID] = true
}
inviteUserIDs[*invite.Body.InviterID] = true
}
var profileIDs []identity.ID
for id := range inviteUserIDs {
profileIDs = append(profileIDs, id)
}
// Lookup profiles of those who were invited
profiles, err := client.Profiles.ListByID(context.Background(), profileIDs)
if err != nil {
return errs.NewExitError("Failed to retrieve invites, please try again.")
}
usernameByID := make(map[string]string)
for _, profile := range *profiles {
usernameByID[profile.ID.String()] = profile.Body.Username
}
fmt.Println("")
if ctx.Bool("approved") {
fmt.Println("Listing approved invitations for the " + ctx.String("org") + " org")
} else {
fmt.Println("Listing all pending and accepted invitations for the " + ctx.String("org") + " org")
}
fmt.Println("")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 8, ' ', 0)
fmt.Fprintln(w, "EMAIL\tUSERNAME\tSTATE\tINVITED BY\tCREATION DATE")
fmt.Fprintln(w, " \t \t \t ")
for _, invite := range invites {
inviter := usernameByID[invite.Body.InviterID.String()]
if inviter == "" {
continue
}
identity := invite.Body.Email
invitee := "-"
if invite.Body.InviteeID != nil {
invitee = usernameByID[invite.Body.InviteeID.String()]
}
fmt.Fprintln(w, identity+"\t"+invitee+"\t"+invite.Body.State+"\t"+inviter+"\t"+invite.Body.Created.Format(time.RFC3339))
}
w.Flush()
fmt.Println("")
return nil
}