forked from keybase/client
/
cmd_log_send.go
208 lines (183 loc) · 5.12 KB
/
cmd_log_send.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"encoding/json"
"fmt"
"path/filepath"
"os"
"github.com/keybase/cli"
"github.com/keybase/client/go/install"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"golang.org/x/net/context"
)
const (
defaultBytes = 1024 * 1024 * 16
maxBytes = 1024 * 1024 * 128
)
func NewCmdLogSend(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "send",
Usage: "Send recent debug logs to keybase",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdLogSend{Contextified: libkb.NewContextified(g)}, "send", c)
cl.SetForkCmd(libcmdline.NoFork)
cl.SetLogForward(libcmdline.LogForwardNone)
},
Flags: []cli.Flag{
cli.IntFlag{
Name: "n",
Usage: "Number of bytes in each log file to read",
},
cli.BoolFlag{
Name: "no-confirm",
Usage: "Send logs without confirming",
},
},
}
}
type CmdLogSend struct {
libkb.Contextified
numBytes int
noConfirm bool
feedback string
}
func (c *CmdLogSend) Run() error {
if !c.noConfirm {
if err := c.confirm(); err != nil {
return err
}
if err := c.getFeedback(); err != nil {
return err
}
}
// if this fails for any reason, it is not a fatal error.
// highly possible that the service isn't running and thus
// can't get status.
c.G().Log.Debug("attempting retrieval of keybase service status")
var statusJSON string
statusCmd := &CmdStatus{Contextified: libkb.NewContextified(c.G())}
status, err := statusCmd.load()
if err != nil {
c.G().Log.Info("ignoring error getting keybase status: %s", err)
statusJSON = c.errJSON(err)
} else {
json, err := json.Marshal(status)
if err != nil {
c.G().Log.Info("ignoring status json marshal error: %s", err)
statusJSON = c.errJSON(err)
} else {
statusJSON = string(json)
}
}
err = c.pokeUI()
if err != nil {
c.G().Log.Info("ignoring UI logs: %s", err)
}
logs := c.logFiles(status)
// So far, install logs are Windows only
if logs.Install != "" {
defer os.Remove(logs.Install)
}
logSendContext := libkb.LogSendContext{
Contextified: libkb.NewContextified(c.G()),
Logs: logs,
}
id, err := logSendContext.LogSend(statusJSON, c.feedback, true, c.numBytes)
if err != nil {
return err
}
c.outputInstructions(id)
return nil
}
func (c *CmdLogSend) pokeUI() error {
cli, err := GetLogsendClient(c.G())
if err != nil {
return err
}
return cli.PrepareLogsend(context.Background())
}
func (c *CmdLogSend) confirm() error {
ui := c.G().UI.GetTerminalUI()
ui.Printf("This command will send recent keybase log entries to keybase.io\n")
ui.Printf("for debugging purposes only.\n\n")
ui.Printf("These logs don’t include your private keys or encrypted data,\n")
ui.Printf("but they will include filenames and other metadata keybase normally\n")
ui.Printf("can’t read, for debugging purposes.\n\n")
return ui.PromptForConfirmation("Continue sending logs to keybase.io?")
}
func (c *CmdLogSend) getFeedback() error {
ui := c.G().UI.GetTerminalUI()
var err error
for err == nil {
in, err := ui.Prompt(0, "Enter feedback (or ENTER to send): ")
if err != nil {
return err
}
if in != "" {
c.feedback = c.feedback + in + "\n"
} else {
break
}
}
return nil
}
func (c *CmdLogSend) outputInstructions(id string) {
ui := c.G().UI.GetTerminalUI()
ui.Printf("\n\n------------\n")
ui.Printf("Success! Your log ID is:\n\n")
ui.Printf(" %s\n\n", id)
ui.Printf("Here's a URL to submit new bug reports containing this ID:\n\n")
ui.Output(" https://github.com/keybase/client/issues/new?body=[write%20something%20useful%20and%20descriptive%20here]%0A%0Amy%20log%20id:%20" + id)
ui.Printf("\n\nThanks!\n")
ui.Printf("------------\n\n")
}
func (c *CmdLogSend) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) > 0 {
return UnexpectedArgsError("log send")
}
c.noConfirm = ctx.Bool("no-confirm")
c.numBytes = ctx.Int("n")
if c.numBytes < 1 {
c.numBytes = defaultBytes
} else if c.numBytes > maxBytes {
c.numBytes = maxBytes
}
return nil
}
func (c *CmdLogSend) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}
func (c *CmdLogSend) logFiles(status *fstatus) libkb.Logs {
logDir := c.G().Env.GetLogDir()
installLogPath, err := install.InstallLogPath()
if err != nil {
c.G().Log.Errorf("Error (InstallLogPath): %s", err)
}
if status != nil {
return libkb.Logs{
Desktop: status.Desktop.Log,
Kbfs: status.KBFS.Log,
Service: status.Service.Log,
Updater: status.Updater.Log,
Start: status.Start.Log,
Install: installLogPath,
System: install.SystemLogPath(),
}
}
return libkb.Logs{
Desktop: filepath.Join(logDir, libkb.DesktopLogFileName),
Kbfs: filepath.Join(logDir, libkb.KBFSLogFileName),
Service: filepath.Join(logDir, libkb.ServiceLogFileName),
Updater: filepath.Join(logDir, libkb.UpdaterLogFileName),
Start: filepath.Join(logDir, libkb.StartLogFileName),
Install: installLogPath,
}
}
func (c *CmdLogSend) errJSON(err error) string {
return fmt.Sprintf("{\"Error\":%q}", err)
}