/
cmd_clear_advertisements.go
158 lines (142 loc) · 4.57 KB
/
cmd_clear_advertisements.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package client
import (
"fmt"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/keybase1"
"golang.org/x/net/context"
)
type CmdChatClearCommands struct {
libkb.Contextified
filter bool
adType chat1.BotCommandsAdvertisementTyp
teamName string
convResolvingRequest chatConversationResolvingRequest
}
func NewCmdChatClearCommandsRunner(g *libkb.GlobalContext) *CmdChatClearCommands {
return &CmdChatClearCommands{
Contextified: libkb.NewContextified(g),
}
}
func newCmdChatClearCommands(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
flags := getConversationResolverFlags()
flags = append(flags, cli.StringFlag{
Name: "team-name",
Usage: "Specify a team",
}, cli.StringFlag{
Name: "type",
Usage: "Specify an advertisement type. The valid values are \"public\", \"teammembers\", \"teamconvs\", \"conv\"",
})
return cli.Command{
Name: "clear-commands",
Usage: "Clear any advertised commands for the logged-in user.",
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdChatClearCommandsRunner(g), "clear-commands", c)
cl.SetNoStandalone()
cl.SetLogForward(libcmdline.LogForwardNone)
},
Flags: flags,
Description: chatClearCommandsDoc,
}
}
func (c *CmdChatClearCommands) Run() error {
ctx := context.Background()
client, err := GetChatLocalClient(c.G())
if err != nil {
return err
}
var filter *chat1.ClearBotCommandsFilter
if c.filter {
var teamName *string
var convID *chat1.ConversationID
switch c.adType {
case chat1.BotCommandsAdvertisementTyp_PUBLIC:
case chat1.BotCommandsAdvertisementTyp_TLFID_CONVS, chat1.BotCommandsAdvertisementTyp_TLFID_MEMBERS:
teamName = &c.teamName
case chat1.BotCommandsAdvertisementTyp_CONV:
resolver, err := newChatConversationResolver(c.G())
if err != nil {
return err
}
if err = annotateResolvingRequest(c.G(), &c.convResolvingRequest); err != nil {
return err
}
conversation, _, err := resolver.Resolve(ctx, c.convResolvingRequest, chatConversationResolvingBehavior{
CreateIfNotExists: false,
MustNotExist: false,
Interactive: false,
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
})
if err != nil {
return err
}
filterConvID := conversation.GetConvID()
convID = &filterConvID
}
filter = &chat1.ClearBotCommandsFilter{
Typ: c.adType,
TeamName: teamName,
ConvID: convID,
}
}
if _, err = client.ClearBotCommandsLocal(context.Background(), filter); err != nil {
return err
}
dui := c.G().UI.GetDumbOutputUI()
_, err = dui.Printf("Cleared bot commands successfully.\n")
return err
}
func (c *CmdChatClearCommands) ParseArgv(ctx *cli.Context) (err error) {
if len(ctx.Args()) > 0 {
return UnexpectedArgsError("clear-commands")
}
typString := ctx.String("type")
if typString != "" {
c.filter = true
c.adType, err = chat1.GetAdvertTyp(typString)
if err != nil {
return err
}
c.teamName = ctx.String("team-name")
channel := ctx.String("channel")
switch c.adType {
case chat1.BotCommandsAdvertisementTyp_PUBLIC:
if c.teamName != "" {
return fmt.Errorf("--team-name is unexpected for type %q", typString)
} else if channel != "" {
return fmt.Errorf("--channel is unexpected for type %q", typString)
}
case chat1.BotCommandsAdvertisementTyp_TLFID_CONVS, chat1.BotCommandsAdvertisementTyp_TLFID_MEMBERS:
if c.teamName == "" {
return fmt.Errorf("--team-name required for type %q", typString)
} else if channel != "" {
return fmt.Errorf("--channel is unexpected for type %q", typString)
}
case chat1.BotCommandsAdvertisementTyp_CONV:
if c.teamName == "" {
return fmt.Errorf("--team-name required for type %q", typString)
}
if c.convResolvingRequest, err = parseConversationResolvingRequest(ctx, c.teamName); err != nil {
return err
}
}
} else if ctx.NumFlags() != 0 {
return fmt.Errorf("no flags are expected when a --type is not specified")
}
return nil
}
func (c *CmdChatClearCommands) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}
const chatClearCommandsDoc = `"keybase chat clear-commands" allows you to clear advertised commands for the logged-in user
EXAMPLES:
Clear all commands advertised by the logged-in user:
keybase chat clear-commands
Clear all commands advertised for a specific conversation by the logged-in user:
keybase chat clear-commands --type "conv" --team-name treehouse --channel random
`