-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.go
298 lines (280 loc) · 6.92 KB
/
share.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"bytes"
"errors"
"fmt"
"sort"
"strings"
"github.com/gofrs/uuid"
"github.com/steverusso/lockbook-x/go-lockbook"
)
// Sharing related commands.
type shareCmd struct {
create *shareCreateCmd
pending *sharePendingCmd
accept *shareAcceptCmd
reject *shareRejectCmd
}
func (s *shareCmd) run(core lockbook.Core) error {
switch {
case s.create != nil:
return s.create.run(core)
case s.pending != nil:
return s.pending.run(core)
case s.accept != nil:
return s.accept.run(core)
case s.reject != nil:
return s.reject.run(core)
default:
return nil
}
}
// Share a file with another lockbook user.
type shareCreateCmd struct {
// The other user will not be able to edit the file.
//
// clap:opt ro
readOnly bool
// The path or ID of the lockbook file you'd like to share.
//
// clap:arg_required
target string
// The username of the other lockbook user.
//
// clap:arg_required
username string
}
func (c *shareCreateCmd) 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)
}
mode := lockbook.ShareModeWrite
if c.readOnly {
mode = lockbook.ShareModeRead
}
if err = core.ShareFile(id, c.username, mode); err != nil {
return fmt.Errorf("sharing file: %w", err)
}
fmt.Printf("\033[1;32mdone!\033[0m file %q will be shared next time you sync.\n", id)
return nil
}
// List pending shares.
type sharePendingCmd struct {
// Print full UUIDs instead of prefixes.
//
// clap:opt ids
fullIDs bool
}
func (c *sharePendingCmd) run(core lockbook.Core) error {
pendingShares, err := core.GetPendingShares()
if err != nil {
return fmt.Errorf("getting pending shares: %w", err)
}
if len(pendingShares) == 0 {
fmt.Println("no pending shares")
return nil
}
shareInfos := filesToShareInfos(pendingShares)
fmt.Println(shareInfoTable(shareInfos, c.fullIDs))
return nil
}
// Accept a pending share.
type shareAcceptCmd struct {
// The ID or ID prefix of a pending share.
//
// clap:arg_required
target string
// Where to place this in your file tree.
//
// clap:arg_required
dest string
// Name this file something else.
newName string
}
func (c *shareAcceptCmd) run(core lockbook.Core) error {
pendingShares, err := core.GetPendingShares()
if err != nil {
return fmt.Errorf("getting pending shares: %w", err)
}
if len(pendingShares) == 0 {
return errors.New("no pending shares to accept")
}
match, err := getOnePendingShareMatch(pendingShares, c.target)
if err != nil {
return err
}
parentID := lockbook.FileID{}
// If the destination is a valid UUID, it must be of an existing directory.
if destID := uuid.FromStringOrNil(c.dest); !destID.IsNil() {
f, err := core.FileByID(destID)
if err != nil {
return fmt.Errorf("file by id %q: %w", destID, err)
}
if !f.IsDir() {
return fmt.Errorf("destination id %q isn't a folder", destID)
}
parentID = destID
} else {
// If the destination path exists, it must be a directory. The link will be
// dropped in it.
f, exists, err := lockbook.MaybeFileByPath(core, c.dest)
if err != nil {
return fmt.Errorf("file by path %q: %w", c.dest, err)
}
if !exists {
// If the destination path doesn't exist, then it's just treated as a
// non-existent directory path. The user can choose a name with the `--rename`
// flag.
fPath := c.dest
if fPath[len(fPath)-1] != '/' {
fPath += "/"
}
newFile, err := core.CreateFileAtPath(fPath)
if err != nil {
return fmt.Errorf("creating file at path %q: %w", fPath, err)
}
parentID = newFile.ID
} else {
if !f.IsDir() {
return fmt.Errorf("existing destination path %q is a doc, must be a folder", c.dest)
}
parentID = f.ID
}
}
name := c.newName
if name == "" {
name = match.Name
}
if name[len(name)-1] == '/' {
name = name[:len(name)-1] // Prevent "name contains slash" error.
}
_, err = core.CreateFile(name, parentID, lockbook.FileTypeLink{Target: match.ID})
if err != nil {
return fmt.Errorf("creating link: %w", err)
}
return nil
}
// Reject a pending share.
type shareRejectCmd struct {
// The ID or ID prefix of a pending share.
//
// clap:arg_required
target string
}
func (c *shareRejectCmd) run(core lockbook.Core) error {
pendingShares, err := core.GetPendingShares()
if err != nil {
return fmt.Errorf("getting pending shares: %w", err)
}
if len(pendingShares) == 0 {
return errors.New("no pending shares to delete")
}
match, err := getOnePendingShareMatch(pendingShares, c.target)
if err != nil {
return err
}
if err = core.DeletePendingShare(match.ID); err != nil {
return fmt.Errorf("delete pending share: %w", err)
}
return nil
}
func getOnePendingShareMatch(shares []lockbook.File, id string) (lockbook.File, error) {
matches := []lockbook.File{}
for _, f := range shares {
if strings.HasPrefix(f.ID.String(), id) {
matches = append(matches, f)
}
}
n := len(matches)
if len(matches) == 0 {
desc := ""
if t := uuid.FromStringOrNil(id); !t.IsNil() {
desc = " prefix"
}
return lockbook.File{}, fmt.Errorf("no pending share found with id%s %s", desc, id)
}
if n > 1 {
return lockbook.File{}, fmt.Errorf(
"id prefix %q matched %d pending shares:\n%s",
id, n, shareInfoTable(filesToShareInfos(matches), true),
)
}
return matches[0], nil
}
type shareInfo struct {
id lockbook.FileID
from string
name string
mode string
}
func filesToShareInfos(files []lockbook.File) []shareInfo {
infos := make([]shareInfo, len(files))
for i, f := range files {
from, mode := "", ""
if len(f.Shares) > 0 {
from = f.Shares[0].SharedBy
mode = strings.ToLower(f.Shares[0].Mode.String())
}
infos[i] = shareInfo{
id: f.ID,
from: from,
name: f.Name,
mode: mode,
}
}
sort.SliceStable(infos, func(i, j int) bool {
a, b := infos[i], infos[j]
if a.from != b.from {
return a.from < b.from
}
if a.name != b.name {
return a.name < b.name
}
return bytes.Compare(a.id[:], b.id[:]) < 0
})
return infos
}
func shareInfoTable(infos []shareInfo, isFullIDs bool) (ret string) {
// Determine each column's max width.
wID := idPrefixLen
if isFullIDs && len(infos) > 0 {
wID = len(infos[0].id)
}
wFrom := 0
wName := 0
wMode := 0
for i := range infos {
if n := len(infos[i].mode); n > wMode {
wMode = n
}
if n := len(infos[i].from); n > wFrom {
wFrom = n
}
if n := len(infos[i].name); n > wName {
wName = n
}
}
// Print the table column headers.
ret += fmt.Sprintf(" %-*s | %-*s | %-*s | file\n", wID, "id", wMode, "mode", wFrom, "from")
ret += fmt.Sprintf("-%s-+-%s-+-%s-+-%s-\n",
strings.Repeat("-", wID),
strings.Repeat("-", wMode),
strings.Repeat("-", wFrom),
strings.Repeat("-", wName),
)
// Print the table rows of pending share infos.
for i, info := range infos {
ret += fmt.Sprintf(
" %-*s | %-*s | %-*s | %s",
wID, info.id[:wID],
wMode, info.mode,
wFrom, info.from,
info.name,
)
if i != len(infos)-1 {
ret += "\n"
}
}
return
}