forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_search.go
153 lines (133 loc) · 3.55 KB
/
cmd_search.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"strings"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol"
jsonw "github.com/keybase/go-jsonw"
)
type CmdSearch struct {
query string
json bool
}
func (c *CmdSearch) ParseArgv(ctx *cli.Context) error {
c.query = strings.Join(ctx.Args(), " ")
if c.query == "" {
return fmt.Errorf("Search query must not be empty.")
}
c.json = ctx.Bool("json")
return nil
}
func (c *CmdSearch) Run() (err error) {
cli, err := GetUserClient()
if err != nil {
return err
}
if err = RegisterProtocols(nil); err != nil {
return err
}
results, err := cli.Search(context.TODO(), keybase1.SearchArg{Query: c.query})
if err != nil {
return err
}
userSummaries, err := UserSummariesForSearchResults(results)
if err != nil {
return err
}
return c.showResults(userSummaries)
}
func UserSummariesForSearchResults(results []keybase1.SearchResult) ([]keybase1.UserSummary, error) {
cli, err := GetUserClient()
if err != nil {
return nil, err
}
uids := make([]keybase1.UID, len(results))
for i := range results {
uids[i] = results[i].Uid
}
userSummaries, err := cli.LoadUncheckedUserSummaries(context.TODO(), keybase1.LoadUncheckedUserSummariesArg{Uids: uids})
if err != nil {
return nil, err
}
return userSummaries, nil
}
func (c *CmdSearch) showResults(results []keybase1.UserSummary) error {
if c.json {
return c.showJSONResults(results)
}
return c.showRegularResults(results)
}
func (c *CmdSearch) showRegularResults(results []keybase1.UserSummary) error {
if len(results) == 0 {
GlobUI.Println("No results.")
return nil
}
for _, user := range results {
GlobUI.Printf("%s", user.Username)
for _, social := range user.Proofs.Social {
GlobUI.Printf(" %s:%s", social.ProofType, social.ProofName)
}
for _, web := range user.Proofs.Web {
for _, protocol := range web.Protocols {
GlobUI.Printf(" %s://%s", protocol, web.Hostname)
}
}
GlobUI.Println()
}
return nil
}
func (c *CmdSearch) showJSONResults(results []keybase1.UserSummary) error {
output := jsonw.NewArray(len(results))
for userIndex, user := range results {
userBlob := jsonw.NewDictionary()
userBlob.SetKey("username", jsonw.NewString(user.Username))
for _, social := range user.Proofs.Social {
userBlob.SetKey(social.ProofType, jsonw.NewString(social.ProofName))
}
if len(user.Proofs.Web) > 0 {
var webProofs []string
for _, webProof := range user.Proofs.Web {
for _, protocol := range webProof.Protocols {
webProofs = append(webProofs, libkb.MakeURI(protocol, webProof.Hostname))
}
}
websites := jsonw.NewArray(len(webProofs))
for i, wp := range webProofs {
websites.SetIndex(i, jsonw.NewString(wp))
}
userBlob.SetKey("websites", websites)
}
output.SetIndex(userIndex, userBlob)
}
GlobUI.Println(output.MarshalPretty())
return nil
}
func NewCmdSearch(cl *libcmdline.CommandLine) cli.Command {
return cli.Command{
Name: "search",
ArgumentHelp: "<query>",
Usage: "Search for keybase users",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "j, json",
Usage: "Output as JSON.",
},
},
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdSearch{}, "search", c)
},
}
}
func (c *CmdSearch) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
GpgKeyring: true,
KbKeyring: true,
API: true,
}
}