/
cmd_contacts_debug.go
159 lines (139 loc) · 3.27 KB
/
cmd_contacts_debug.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
// Copyright 2019 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
//go:build !production
// +build !production
package client
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
"golang.org/x/net/context"
)
// Devel commands for testing contact syncing.
func NewCmdContacts(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "contacts",
Usage: "commands for testing contact sync on desktop",
ArgumentHelp: "[arguments...]",
Subcommands: []cli.Command{
NewCmdContactLookup(cl, g),
NewCmdSaveContacts(cl, g),
},
}
}
// ------------------------
// `keybase contacts lookup`
type CmdContactLookup struct {
libkb.Contextified
}
func NewCmdContactLookup(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
cmd := &CmdContactLookup{
Contextified: libkb.NewContextified(g),
}
return cli.Command{
Name: "lookup",
Usage: "resolve contact list",
ArgumentHelp: "",
Action: func(c *cli.Context) {
cl.ChooseCommand(cmd, "lookup", c)
},
}
}
func (c *CmdContactLookup) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) != 0 {
return errors.New("command takes no arguments")
}
return nil
}
func (c *CmdContactLookup) Run() error {
cli, err := GetContactsClient(c.G())
if err != nil {
return err
}
bytes, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
var contacts []keybase1.Contact
err = json.Unmarshal(bytes, &contacts)
if err != nil {
return err
}
ret, err := cli.LookupContactList(context.Background(), keybase1.LookupContactListArg{
Contacts: contacts,
})
if err != nil {
return err
}
s, err := json.MarshalIndent(ret, "", " ")
if err != nil {
return err
}
c.G().UI.GetTerminalUI().Printf("%s", s)
return nil
}
func (c *CmdContactLookup) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}
// ------------------------
// `keybase contacts save`
type CmdSaveContacts struct {
libkb.Contextified
}
func NewCmdSaveContacts(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
cmd := &CmdSaveContacts{
Contextified: libkb.NewContextified(g),
}
return cli.Command{
Name: "save",
Usage: "save contact list",
ArgumentHelp: "",
Action: func(c *cli.Context) {
cl.ChooseCommand(cmd, "save", c)
},
}
}
func (c *CmdSaveContacts) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) != 0 {
return errors.New("command takes no arguments")
}
return nil
}
func (c *CmdSaveContacts) Run() error {
cli, err := GetContactsClient(c.G())
if err != nil {
return err
}
bytes, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
var contacts []keybase1.Contact
err = json.Unmarshal(bytes, &contacts)
if err != nil {
return err
}
_, err = cli.SaveContactList(context.Background(), keybase1.SaveContactListArg{
Contacts: contacts,
})
if err != nil {
return err
}
fmt.Fprintf(c.G().UI.GetTerminalUI().ErrorWriter(), "Contacts saved.\n")
return nil
}
func (c *CmdSaveContacts) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}