-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
51 lines (44 loc) · 1.27 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
package cli
import (
"fmt"
"time"
"github.com/99designs/keyring"
"github.com/adrg/xdg"
"github.com/zoni/pingrep/internal/pinboard"
)
type updateCmd struct {
Verbose bool `short:"v" help:"Show verbose output."`
}
func (cmd *updateCmd) Run(_ *globals, kr keyring.Keyring) error {
path, err := xdg.DataFile(fmt.Sprintf("%s/bookmarks.json", programName))
if err != nil {
return fmt.Errorf("create data dir: %w", err)
}
client, err := createPinboardClient(kr)
if err != nil {
return fmt.Errorf("construct pinboard client: %w", err)
}
localTime, err := getLastUpdateTimestamp(path)
if err != nil {
return fmt.Errorf("get last update timestamp: %w", err)
}
remoteTime, err := client.LastUpdate()
if err != nil {
return fmt.Errorf("get last update timestamp from api: %w", err)
}
if !localTime.Before(remoteTime) {
if cmd.Verbose {
fmt.Printf("Bookmarks already up to date (last changed %s)\n", remoteTime.Format(time.RFC3339))
}
return nil
}
collection, err := pinboard.FromAPI(client)
if err != nil {
return fmt.Errorf("read pins from api: %w", err)
}
if err := collection.Save(path); err != nil {
return fmt.Errorf("save new bookmarks: %w", err)
}
fmt.Printf("Bookmarks updated (last changed %s)\n", remoteTime.Format(time.RFC3339))
return nil
}