forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_status.go
188 lines (167 loc) · 4.92 KB
/
cmd_status.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
177
178
179
180
181
182
183
184
185
186
187
188
// 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"
)
const spacesPerIndent = 4
func indentSpace(level int) string {
return strings.Repeat(" ", level*spacesPerIndent)
}
type CmdStatus struct{}
func (v *CmdStatus) ParseArgv(ctx *cli.Context) error {
return nil
}
func (v *CmdStatus) Run() (err error) {
configCli, err := GetConfigClient(G)
if err != nil {
return err
}
currentStatus, err := configCli.GetCurrentStatus(context.TODO(), 0)
if err != nil {
return err
}
if !currentStatus.LoggedIn {
return fmt.Errorf("Not logged in.")
}
myUID := currentStatus.User.Uid
userCli, err := GetUserClient()
if err != nil {
return err
}
me, err := userCli.LoadUser(context.TODO(), keybase1.LoadUserArg{Uid: myUID})
if err != nil {
return err
}
publicKeys, err := userCli.LoadPublicKeys(context.TODO(), keybase1.LoadPublicKeysArg{Uid: myUID})
if err != nil {
return err
}
devCli, err := GetDeviceClient()
if err != nil {
return err
}
devs, err := devCli.DeviceList(context.TODO(), 0)
if err != nil {
return err
}
v.printExportedMe(me, publicKeys, devs)
return nil
}
func findSubkeys(parentID keybase1.KID, allKeys []keybase1.PublicKey) []keybase1.PublicKey {
ret := []keybase1.PublicKey{}
for _, key := range allKeys {
if keybase1.KIDFromString(key.ParentID).Equal(parentID) {
ret = append(ret, key)
}
}
return ret
}
func (v *CmdStatus) printExportedMe(me keybase1.User, publicKeys []keybase1.PublicKey, devices []keybase1.Device) error {
GlobUI.Printf("Username: %s\n", me.Username)
GlobUI.Printf("User ID: %s\n", me.Uid)
GlobUI.Printf("Device ID: %s\n", G.Env.GetDeviceID())
for _, device := range devices {
if device.DeviceID == G.Env.GetDeviceID() {
GlobUI.Printf("Device name: %s\n", device.Name)
}
}
if len(publicKeys) == 0 {
GlobUI.Printf("No public keys.\n")
return nil
}
GlobUI.Printf("Public keys:\n")
// Keep track of subkeys we print, so that if e.g. a subkey's parent is
// nonexistent, we can notice that we skipped it.
subkeysShown := make(map[keybase1.KID]bool)
for _, key := range publicKeys {
if !key.IsSibkey {
// Subkeys will be printed under their respective sibkeys.
continue
}
subkeys := findSubkeys(key.KID, publicKeys)
err := printKey(key, subkeys, 1)
if err != nil {
return err
}
for _, subkey := range subkeys {
subkeysShown[subkey.KID] = true
}
}
// Print errors for any subkeys we failed to show.
for _, key := range publicKeys {
if !key.IsSibkey && !subkeysShown[key.KID] {
errorStr := fmt.Sprintf("Dangling subkey: %s", key.KID)
G.Log.Error(errorStr) // %s in here angers `go vet`
}
}
return nil
}
func printKey(key keybase1.PublicKey, subkeys []keybase1.PublicKey, indent int) error {
if key.KID == "" {
return fmt.Errorf("Found a key with an empty KID.")
}
eldestStr := ""
if key.IsEldest {
eldestStr = " (eldest)"
}
GlobUI.Printf("%s%s%s\n", indentSpace(indent), key.KID, eldestStr)
if key.PGPFingerprint != "" {
GlobUI.Printf("%sPGP Fingerprint: %s\n", indentSpace(indent+1), libkb.PGPFingerprintFromHexNoError(key.PGPFingerprint).ToQuads())
GlobUI.Printf("%sPGP Identities:\n", indentSpace(indent+1))
for _, identity := range key.PGPIdentities {
commentStr := ""
if identity.Comment != "" {
commentStr = fmt.Sprintf(" (%s)", identity.Comment)
}
emailStr := ""
if identity.Email != "" {
emailStr = fmt.Sprintf(" <%s>", identity.Email)
}
GlobUI.Printf("%s%s%s%s\n", indentSpace(indent+2), identity.Username, commentStr, emailStr)
}
}
if key.DeviceID != "" || key.DeviceType != "" || key.DeviceDescription != "" {
GlobUI.Printf("%sDevice:\n", indentSpace(indent+1))
if key.DeviceID != "" {
GlobUI.Printf("%sID: %s\n", indentSpace(indent+2), key.DeviceID)
}
if key.DeviceType != "" {
GlobUI.Printf("%sType: %s\n", indentSpace(indent+2), key.DeviceType)
}
if key.DeviceDescription != "" {
GlobUI.Printf("%sDescription: %s\n", indentSpace(indent+2), key.DeviceDescription)
}
}
GlobUI.Printf("%sCreated: %s\n", indentSpace(indent+1), keybase1.FromTime(key.CTime))
GlobUI.Printf("%sExpires: %s\n", indentSpace(indent+1), keybase1.FromTime(key.ETime))
if subkeys != nil && len(subkeys) > 0 {
GlobUI.Printf("%sSubkeys:\n", indentSpace(indent+1))
for _, subkey := range subkeys {
printKey(subkey, nil, indent+2)
}
}
return nil
}
func NewCmdStatus(cl *libcmdline.CommandLine) cli.Command {
return cli.Command{
Name: "status",
Usage: "Show information about the current user",
Flags: []cli.Flag{},
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdStatus{}, "status", c)
},
}
}
func (v *CmdStatus) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}