-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
66 lines (47 loc) · 1.03 KB
/
update.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
package main
import (
"context"
"errors"
"fmt"
"strings"
"sync"
nkcli "github.com/mdzz-club/nkcli/internal"
"github.com/urfave/cli/v2"
)
var (
errInvalidRelayUrl = errors.New("Invalid relay URL")
)
func updateAction(c *cli.Context) error {
dbpath := c.String("db")
relays := c.StringSlice("relay")
if relays == nil {
relays = DefaultRelays
}
for _, url := range relays {
if !strings.HasPrefix(url, "ws://") && !strings.HasPrefix(url, "wss://") {
return errors.Join(errInvalidRelayUrl, errors.New(url))
}
}
fmt.Print("Will use these relays to update your keys data:\n\n")
for _, url := range relays {
fmt.Printf(" %v\n", url)
}
db, err := nkcli.Open(dbpath)
if err != nil {
return err
}
defer db.Close()
list, err := db.List()
if err != nil {
return err
}
fmt.Printf("\nFound %v keys\n\n", len(list))
wg := new(sync.WaitGroup)
ctx := context.WithValue(c.Context, "db", db)
for _, item := range list {
wg.Add(1)
go nkcli.UpdateRelays(ctx, item, relays, wg)
}
wg.Wait()
return nil
}