-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
284 lines (261 loc) · 6.54 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/gofrs/uuid"
"github.com/steverusso/lockbook-x/go-lockbook"
)
const idPrefixLen = 8
//go:generate goclap -type lbcli
// An unofficial lockbook cli.
type lbcli struct {
acct *acctCmd
cat *catCmd
debug *debugCmd
export *exportCmd
imprt *importCmd
jot *jotCmd
ls *lsCmd
mkdir *mkdirCmd
mkdoc *mkdocCmd
mv *mvCmd
rename *renameCmd
rm *rmCmd
share *shareCmd
sync *syncCmd
usage *usageCmd
write *writeCmd
}
// Get updates from the server and push changes.
type syncCmd struct {
// Show last synced and which operations a sync would perform.
//
// clap:opt status,s
status bool
// Output every sync step and progress.
//
// clap:opt verbose,v
verbose bool
}
func (c *syncCmd) run(core lockbook.Core) error {
if c.status {
if err := printSyncStatus(core); err != nil {
return fmt.Errorf("getting sync status: %w", err)
}
return nil
}
var syncProgress func(lockbook.SyncProgress)
if c.verbose {
syncProgress = func(sp lockbook.SyncProgress) {
fmt.Printf("(%d/%d) %s\n", sp.Progress, sp.Total, sp.Msg)
}
}
err := core.SyncAll(syncProgress)
if err != nil {
return fmt.Errorf("syncing: %w", err)
}
if c.verbose {
fmt.Println("done")
}
return nil
}
func printSyncStatus(core lockbook.Core) error {
wc, err := core.CalculateWork()
if err != nil {
return fmt.Errorf("calculating work: %w", err)
}
for _, wu := range wc.WorkUnits {
pushOrPull := "pushed"
if wu.Type == lockbook.WorkUnitTypeServer {
pushOrPull = "pulled"
}
fmt.Printf("%s needs to be %s\n", wu.ID, pushOrPull)
}
lastSyncedAt, err := core.GetLastSyncedHumanString()
if err != nil {
return fmt.Errorf("getting last synced human string: %w", err)
}
fmt.Printf("last synced: %s\n", lastSyncedAt)
return nil
}
// Local and server disk utilization (uncompressed and compressed).
//
// clap:cmd_usage [-e]
type usageCmd struct {
// Show amounts in bytes instead of as human readable values.
//
// clap:opt exact,e
exact bool
}
func (c *usageCmd) run(core lockbook.Core) error {
u, err := core.GetUsage()
if err != nil {
return fmt.Errorf("getting usage: %w", err)
}
uu, err := core.GetUncompressedUsage()
if err != nil {
return fmt.Errorf("getting uncompressed usage: %w", err)
}
uncompressed := uu.Readable
serverUsage := u.ServerUsage.Readable
dataCap := u.DataCap.Readable
if c.exact {
uncompressed = fmt.Sprintf("%d B", uu.Exact)
serverUsage = fmt.Sprintf("%d B", u.ServerUsage.Exact)
dataCap = fmt.Sprintf("%d B", u.DataCap.Exact)
}
fmt.Printf("uncompressed file size: %s\n", uncompressed)
fmt.Printf("server utilization: %s\n", serverUsage)
fmt.Printf("server data cap: %s\n", dataCap)
return nil
}
func idFromSomething(core lockbook.Core, v string) (uuid.UUID, error) {
if id := uuid.FromStringOrNil(v); !id.IsNil() {
return id, nil
}
f, err := core.FileByPath(v)
if err == nil {
return f.ID, nil
}
if err, ok := asLbErr(err); ok && err.Code != lockbook.CodeFileNonexistent {
return uuid.Nil, fmt.Errorf("trying to get a file by path: %w", err)
}
// Not a full UUID and not a path, so that leaves UUID prefix.
files, err := core.ListMetadatas()
if err != nil {
return uuid.Nil, fmt.Errorf("listing metadatas to check ids: %w", err)
}
possibs := make([]lockbook.File, 0, 5)
for i := range files {
if strings.HasPrefix(files[i].ID.String(), v) {
possibs = append(possibs, files[i])
}
}
n := len(possibs)
if n == 0 {
return uuid.Nil, fmt.Errorf("value %q is not a path, uuid, or uuid prefix", v)
}
if n == 1 {
return possibs[0].ID, nil
}
// Multiple ID prefix matches.
errMsg := fmt.Sprintf("value %q is not a path and matches %d file ID prefixes:\n", v, n)
for _, f := range possibs {
pathOrErr, err := core.PathByID(f.ID)
if err != nil {
pathOrErr = fmt.Sprintf("error getting path: %v", err)
}
errMsg += fmt.Sprintf(" %s %s\n", f.ID, pathOrErr)
}
return uuid.Nil, errors.New(errMsg)
}
func asLbErr(err error) (*lockbook.Error, bool) {
var lberr *lockbook.Error
if errors.As(err, &lberr) {
return lberr, true
}
return nil, false
}
func isStdinPipe() bool {
fi, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
return fi.Mode()&os.ModeNamedPipe != 0
}
func isStdoutPipe() bool {
fi, err := os.Stdout.Stat()
if err != nil {
panic(err)
}
return fi.Mode()&os.ModeNamedPipe != 0
}
func hasAccount(core lockbook.Core) (bool, error) {
_, err := core.GetAccount()
if err, ok := asLbErr(err); ok && err.Code == lockbook.CodeAccountNonexistent {
return false, nil
}
if err != nil {
return false, fmt.Errorf("getting account: %v", err)
}
return true, nil
}
func run() error {
// Figure out data directory.
dataDir := os.Getenv("LOCKBOOK_PATH")
if dataDir == "" {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("getting user home dir: %v", err)
}
dataDir = filepath.Join(home, ".lockbook/lbcli")
}
// Initialize a new lockbook Core instance.
core, err := lockbook.NewCore(dataDir)
if err != nil {
return fmt.Errorf("initializing core: %v", err)
}
lb := lbcli{}
lb.Parse(os.Args)
// Make sure there's no account when initializing or restoring, and that there is an
// account for all other actions.
hasAcct, err := hasAccount(core)
if err != nil {
return err
}
if lb.acct != nil && (lb.acct.init != nil || lb.acct.restore != nil) {
if hasAcct {
return fmt.Errorf("an account already exists for data-dir %q", dataDir)
}
} else if !hasAcct {
return errors.New("no account! run 'acct init' or 'acct restore' to get started")
}
switch {
case lb.acct != nil:
return lb.acct.run(core)
case lb.cat != nil:
return lb.cat.run(core)
case lb.debug != nil:
return lb.debug.run(core)
case lb.export != nil:
return lb.export.run(core)
case lb.imprt != nil:
return lb.imprt.run(core)
case lb.jot != nil:
return lb.jot.run(core)
case lb.ls != nil:
return lb.ls.run(core)
case lb.mkdir != nil:
return lb.mkdir.run(core)
case lb.mkdoc != nil:
return lb.mkdoc.run(core)
case lb.mv != nil:
return lb.mv.run(core)
case lb.rename != nil:
return lb.rename.run(core)
case lb.rm != nil:
return lb.rm.run(core)
case lb.share != nil:
return lb.share.run(core)
case lb.sync != nil:
return lb.sync.run(core)
case lb.usage != nil:
return lb.usage.run(core)
case lb.write != nil:
return lb.write.run(core)
}
return nil
}
func main() {
if err := run(); err != nil {
fmt.Printf("\033[1;31merror:\033[0m %v\n", err)
if err, ok := asLbErr(err); ok && err.Trace != "" {
fmt.Println(err.Trace)
os.Exit(int(err.Code))
}
os.Exit(1)
}
}