-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
128 lines (117 loc) · 2.89 KB
/
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
package main
import (
"fmt"
"os"
"strings"
"github.com/steverusso/lockbook-x/go-lockbook"
)
// Investigative commands mainly intended for devs.
type debugCmd struct {
finfo *debugFinfoCmd
validate *debugValidateCmd
whoami *debugWhoamiCmd
}
func (d *debugCmd) run(core lockbook.Core) error {
switch {
case d.finfo != nil:
return d.finfo.run(core)
case d.validate != nil:
return d.validate.run(core)
case d.whoami != nil:
return d.whoami.run(core)
default:
return nil
}
}
// View info about a target file.
type debugFinfoCmd struct {
// The target can be a file path, UUID, or UUID prefix.
//
// clap:arg_required
target string
}
func (c *debugFinfoCmd) run(core lockbook.Core) error {
id, err := idFromSomething(core, c.target)
if err != nil {
return fmt.Errorf("trying to get id from %q: %w", c.target, err)
}
f, err := core.FileByID(id)
if err != nil {
return fmt.Errorf("getting file %q: %w", id, err)
}
acct, err := core.GetAccount()
if err != nil {
return fmt.Errorf("getting account: %w", err)
}
printFile(&f, acct.Username)
return nil
}
func printFile(f *lockbook.File, myName string) {
// Build the text that will contain share info.
shares := ""
for _, sh := range f.Shares {
sharedBy := "@" + sh.SharedBy
if sh.SharedBy == myName {
sharedBy = "me"
}
sharedWith := "@" + sh.SharedWith
if sh.SharedWith == myName {
sharedWith = "me"
}
mode := strings.ToLower(sh.Mode.String())
shares += fmt.Sprintf("\n %s -> %s (%s)", sharedBy, sharedWith, mode)
}
data := [...][2]string{
{"name", f.Name},
{"id", f.ID.String()},
{"parent", f.Parent.String()},
{"type", strings.ToLower(lockbook.FileTypeString(f.Type))},
{"lastmod", fmt.Sprintf("%v", f.Lastmod)},
{"lastmod_by", f.LastmodBy},
{fmt.Sprintf("shares (%d)", len(f.Shares)), shares},
}
// Determine widest key name.
nameWidth := 0
for i := range data {
n := len(data[i][0])
if n > nameWidth {
nameWidth = n
}
}
for i := range data {
fmt.Printf(" %-*s : %s\n", nameWidth, data[i][0], data[i][1])
}
}
// Find invalid states within your lockbook.
type debugValidateCmd struct{}
func (debugValidateCmd) run(core lockbook.Core) error {
warnings, err := core.Validate()
if err != nil {
return fmt.Errorf("running validate: %w", err)
}
count := len(warnings)
if count == 0 {
return nil
}
suffix := ""
if count > 1 {
suffix = "s"
}
fmt.Fprintf(os.Stderr, "\033[1;33m%d warning%s found:\033[0m\n", count, suffix)
for _, w := range warnings {
fmt.Fprintf(os.Stderr, " %s\n", w)
}
return nil
}
// Print user information for this lockbook.
type debugWhoamiCmd struct{}
func (debugWhoamiCmd) run(core lockbook.Core) error {
acct, err := core.GetAccount()
if err != nil {
return fmt.Errorf("getting account: %w", err)
}
fmt.Printf("data-dir: %s\n", core.WriteablePath())
fmt.Printf("username: %s\n", acct.Username)
fmt.Printf("server: %s\n", acct.APIURL)
return nil
}