-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_chat_search_regexp.go
176 lines (158 loc) · 4.86 KB
/
cmd_chat_search_regexp.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2018 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"errors"
"fmt"
"os"
"regexp"
"github.com/araddon/dateparse"
"github.com/keybase/cli"
"github.com/keybase/client/go/chat/search"
"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/gregor1"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
isatty "github.com/mattn/go-isatty"
"golang.org/x/net/context"
)
type CmdChatSearchRegexp struct {
libkb.Contextified
resolvingRequest chatConversationResolvingRequest
query string
opts chat1.SearchOpts
hasTTY bool
}
func NewCmdChatSearchRegexpRunner(g *libkb.GlobalContext) *CmdChatSearchRegexp {
return &CmdChatSearchRegexp{
Contextified: libkb.NewContextified(g),
}
}
func newCmdChatSearchRegexp(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
flags := append(getConversationResolverFlags(), chatSearchFlags...)
return cli.Command{
Name: "search-regexp",
Usage: "Search via regex within a conversation",
ArgumentHelp: "<conversation> <query>",
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdChatSearchRegexpRunner(g), "search-regexp", c)
cl.SetNoStandalone()
cl.SetLogForward(libcmdline.LogForwardNone)
},
Flags: append(flags, cli.BoolFlag{
Name: "r, regex",
Usage: "Make the given query a regex",
},
cli.IntFlag{
Name: "max-messages",
Value: 10000,
Usage: fmt.Sprintf("Specify the maximum number of messages to search. Maximum value is %d.", search.MaxAllowedSearchMessages),
},
),
}
}
func (c *CmdChatSearchRegexp) Run() (err error) {
ui := NewChatCLIUI(c.G())
protocols := []rpc.Protocol{
chat1.ChatUiProtocol(ui),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
if c.resolvingRequest.TlfName != "" {
if err = annotateResolvingRequest(c.G(), &c.resolvingRequest); err != nil {
return err
}
}
if err := CheckAndStartStandaloneChat(c.G(), c.resolvingRequest.MembersType); err != nil {
return err
}
resolver, err := newChatConversationResolver(c.G())
if err != nil {
return err
}
ctx := context.TODO()
conversation, _, err := resolver.Resolve(ctx, c.resolvingRequest, chatConversationResolvingBehavior{
CreateIfNotExists: false,
MustNotExist: false,
Interactive: c.hasTTY,
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
})
if err != nil {
return err
}
conversationInfo := conversation.Info
arg := chat1.SearchRegexpArg{
ConvID: conversationInfo.Id,
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
Query: c.query,
Opts: c.opts,
}
_, err = resolver.ChatClient.SearchRegexp(ctx, arg)
return err
}
func (c *CmdChatSearchRegexp) ParseArgv(ctx *cli.Context) (err error) {
if len(ctx.Args()) != 2 {
return errors.New("usage: keybase chat search-regexp <conversation> <query>")
}
// Get the TLF name from the first position arg
tlfName := ctx.Args().Get(0)
if c.resolvingRequest, err = parseConversationResolvingRequest(ctx, tlfName); err != nil {
return err
}
c.query = ctx.Args().Get(1)
c.opts.SentBy = ctx.String("sent-by")
c.opts.SentTo = ctx.String("sent-to")
sentBeforeStr := ctx.String("sent-before")
sentAfterStr := ctx.String("sent-after")
if sentBeforeStr != "" && sentAfterStr != "" {
return fmt.Errorf("Only one of sent-before and sent-after can be specified")
}
if sentBeforeStr != "" {
sentBefore, err := dateparse.ParseAny(sentBeforeStr)
if err != nil {
return err
}
c.opts.SentBefore = gregor1.ToTime(sentBefore)
}
if sentAfterStr != "" {
sentAfter, err := dateparse.ParseAny(sentAfterStr)
if err != nil {
return err
}
c.opts.SentAfter = gregor1.ToTime(sentAfter)
}
c.opts.MaxHits = ctx.Int("max-hits")
if c.opts.MaxHits > search.MaxAllowedSearchHits {
return fmt.Errorf("max-hits cannot exceed %d.", search.MaxAllowedSearchHits)
}
c.opts.MaxMessages = ctx.Int("max-messages")
if c.opts.MaxMessages > search.MaxAllowedSearchMessages {
return fmt.Errorf("max-messages cannot exceed %d.", search.MaxAllowedSearchMessages)
}
c.opts.AfterContext = ctx.Int("after-context")
c.opts.BeforeContext = ctx.Int("before-context")
if c.opts.AfterContext == 0 && c.opts.BeforeContext == 0 {
context := ctx.Int("context")
c.opts.BeforeContext = context
c.opts.AfterContext = context
}
c.opts.IsRegex = ctx.Bool("regex")
query := c.query
if !c.opts.IsRegex {
query = regexp.QuoteMeta(c.query)
}
if _, err := regexp.Compile(query); err != nil {
return err
}
c.hasTTY = isatty.IsTerminal(os.Stdin.Fd())
return nil
}
func (c *CmdChatSearchRegexp) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}