forked from keybase/client
/
cmd_log_send.go
163 lines (142 loc) · 4.2 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
// 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/libcmdline"
"github.com/keybase/client/go/libkb"
)
const (
defaultLines = 1e5
maxLines = 1e6
)
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 lines in each log file",
},
},
}
}
type CmdLogSend struct {
libkb.Contextified
numLines int
}
func (c *CmdLogSend) Run() error {
if err := c.confirm(); 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)
}
}
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.numLines)
if err != nil {
return err
}
c.outputInstructions(id)
return nil
}
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) 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.numLines = ctx.Int("n")
if c.numLines < 1 {
c.numLines = defaultLines
} else if c.numLines > maxLines {
c.numLines = maxLines
}
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 := GetInstallLogPath()
if err != nil {
c.G().Log.Errorf("Error in GetInstallLogPath: %s", err)
installLogPath = ""
}
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,
}
}
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)
}