-
Notifications
You must be signed in to change notification settings - Fork 56
/
users_create.go
79 lines (66 loc) · 1.79 KB
/
users_create.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
package main
import (
"context"
"flag"
"fmt"
"github.com/sourcegraph/src-cli/internal/api"
)
func init() {
usage := `
Examples:
Create a user account:
$ src users create -username=alice -email=alice@example.com
`
flagSet := flag.NewFlagSet("create", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src users %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
usernameFlag = flagSet.String("username", "", `The new user's username. (required)`)
emailFlag = flagSet.String("email", "", `The new user's email address. (required)`)
resetPasswordURLFlag = flagSet.Bool("reset-password-url", false, `Print the reset password URL to manually send to the new user.`)
apiFlags = api.NewFlags(flagSet)
)
handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
client := cfg.apiClient(apiFlags, flagSet.Output())
query := `mutation CreateUser(
$username: String!,
$email: String!,
) {
createUser(
username: $username,
email: $email,
) {
resetPasswordURL
}
}`
var result struct {
CreateUser struct {
ResetPasswordURL string
}
}
if ok, err := client.NewRequest(query, map[string]interface{}{
"username": *usernameFlag,
"email": *emailFlag,
}).Do(context.Background(), &result); err != nil || !ok {
return err
}
fmt.Printf("User %q created.\n", *usernameFlag)
if *resetPasswordURLFlag && result.CreateUser.ResetPasswordURL != "" {
fmt.Println()
fmt.Printf("\tReset pasword URL: %s\n", result.CreateUser.ResetPasswordURL)
}
return nil
}
// Register the command.
usersCommands = append(usersCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}