-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_links.go
99 lines (78 loc) · 1.81 KB
/
cmd_links.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
package claw
import (
"flag"
"fmt"
"io"
"log"
"os/user"
"path"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
func cmdLinks(args []string, stdout, stderr io.Writer) int {
var app linksApp
if err := app.fromArgs(args, stdout, stderr); err != nil {
return 2
}
if err := app.run(); err != nil {
fmt.Fprintf(stderr, "Runtime error: %v\n", err)
return 1
}
return 0
}
type linksApp struct {
dbpath string
logger *log.Logger
}
func (app *linksApp) fromArgs(args []string, stdout, stderr io.Writer) error {
flags := flag.NewFlagSet("links", flag.ContinueOnError)
flags.SetOutput(stderr)
flags.StringVar(
&app.dbpath, "dbpath", "", "Database file",
)
if err := flags.Parse(args); err != nil {
return err
}
app.logger = log.New(stderr, "", log.LstdFlags)
if app.dbpath == "" {
u, err := user.Current()
if err != nil {
app.logger.Fatalf("You don't exist: %s", err)
}
app.dbpath = path.Join(u.HomeDir, "/Library/Group Containers/9K33E3U3T4.net.shinyfrog.bear/Application Data/database.sqlite")
}
return nil
}
func (app *linksApp) run() error {
db, err := sqlx.Open("sqlite3", app.dbpath)
if err != nil {
return err
}
defer db.Close()
// Put the database in read only mode for extra safety.
if _, err = db.Exec("PRAGMA query_only = true;"); err != nil {
return err
}
notes, err := fetchNotes(db)
if err != nil {
return err
}
links, err := fetchLinks(db)
if err != nil {
return err
}
nmap := make(map[int]Note)
for _, n := range notes {
nmap[n.PK] = n
}
for _, link := range links {
a := nmap[link.ByNote]
b := nmap[link.Note]
if b.Trashed {
app.logger.Printf("[%d]%s -> [%d]%s [trash]", a.PK, a.Title.String, b.PK, b.Title.String)
} else {
app.logger.Printf("[%d]%s -> [%d]%s", a.PK, a.Title.String, b.PK, b.Title.String)
}
}
return nil
}