-
Notifications
You must be signed in to change notification settings - Fork 224
/
invite.go
77 lines (59 loc) · 1.64 KB
/
invite.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
package orgs
import (
"bytes"
"context"
"fmt"
"io"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newInvite() *cobra.Command {
const (
long = `Invite a user, by email, to join organization. The invitation will be
sent, and the user will be pending until they respond. See also orgs revoke.
`
short = "Invite user (by email) to organization"
usage = "invite [slug] [email]"
)
cmd := command.New(usage, short, long, runInvite,
command.RequireSession)
cmd.Args = cobra.MaximumNArgs(2)
return cmd
}
func runInvite(ctx context.Context) error {
client := client.FromContext(ctx).API()
org, err := OrgFromFirstArgOrSelect(ctx)
if err != nil {
return nil
}
email, err := emailFromSecondArgOrPrompt(ctx)
if err != nil {
return nil
}
inv, err := client.CreateOrganizationInvite(ctx, org.ID, email)
if err != nil {
return fmt.Errorf("failed inviting %s to %s: %w", email, org.Name, err)
}
cfg := config.FromContext(ctx)
io := iostreams.FromContext(ctx)
if cfg.JSONOutput {
_ = render.JSON(io.Out, inv)
return nil
}
var b bytes.Buffer
printInvite(&b, inv, true)
b.WriteTo(io.Out)
return nil
}
func printInvite(w io.Writer, in *api.Invitation, headers bool) {
if headers {
fmt.Fprintf(w, "%-20s %-20s %-10s\n", "Org", "Email", "Redeemed")
fmt.Fprintf(w, "%-20s %-20s %-10s\n", "----", "----", "----")
}
fmt.Fprintf(w, "%-20s %-20s %-10t\n", in.Organization.Slug, in.Email, in.Redeemed)
}