-
Notifications
You must be signed in to change notification settings - Fork 56
/
users_tag.go
79 lines (62 loc) · 1.64 KB
/
users_tag.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:
Add a tag "foo" to a user:
$ src users tag -user-id=$(src users get -f '{{.ID}}' -username=alice) -tag=foo
Remove a tag "foo" to a user:
$ src users tag -user-id=$(src users get -f '{{.ID}}' -username=alice) -remove -tag=foo
Related examples:
List all users with the "foo" tag:
$ src users list -tag=foo
`
flagSet := flag.NewFlagSet("tag", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src users %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
userIDFlag = flagSet.String("user-id", "", `The ID of the user to tag. (required)`)
tagFlag = flagSet.String("tag", "", `The tag to set on the user. (required)`)
removeFlag = flagSet.Bool("remove", false, `Remove the tag. (default: add the tag`)
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 SetUserTag(
$user: ID!,
$tag: String!,
$present: Boolean!
) {
setTag(
node: $user,
tag: $tag,
present: $present
) {
alwaysNil
}
}`
_, err := client.NewRequest(query, map[string]interface{}{
"user": *userIDFlag,
"tag": *tagFlag,
"present": !*removeFlag,
}).Do(context.Background(), &struct{}{})
return err
}
// Register the command.
usersCommands = append(usersCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}