-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsafeimport.go
111 lines (103 loc) · 2.36 KB
/
vsafeimport.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
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"net/url"
"os"
"github.com/keep94/toolbox/db"
"github.com/keep94/toolbox/db/sqlite3_db"
"github.com/keep94/vsafe"
"github.com/keep94/vsafe/vsafedb"
"github.com/keep94/vsafe/vsafedb/for_sqlite"
_ "github.com/mattn/go-sqlite3"
)
var (
fDb string
fName string
fPassword string
)
type jsonEntry struct {
Url string
Title string
Desc string
UName string
Password string
Special string
}
func main() {
flag.Parse()
if fDb == "" || fName == "" || fPassword == "" {
fmt.Println("Need to specify -db, -name, and -password flag.")
flag.Usage()
return
}
rawdb, err := sql.Open("sqlite3", fDb)
if err != nil {
fmt.Printf("Unable to open database - %s\n", fDb)
return
}
dbase := sqlite3_db.New(rawdb)
defer dbase.Close()
store := for_sqlite.New(dbase)
doer := sqlite3_db.NewDoer(dbase)
var user vsafe.User
if err = store.UserByName(nil, fName, &user); err != nil {
fmt.Printf("Error retrieving user - %v\n", err)
return
}
var key *vsafe.Key
if key, err = user.VerifyPassword(fPassword); err != nil {
fmt.Printf("Error verifying user password - %v\n", err)
return
}
decoder := json.NewDecoder(os.Stdin)
var entryList []*jsonEntry
if err = decoder.Decode(&entryList); err != nil {
fmt.Printf("Error decoding json - %v\n", err)
return
}
err = doer.Do(func(t db.Transaction) error {
return doImport(t, store, entryList, key)
})
if err != nil {
fmt.Printf("Import failed - %v\n", err)
}
}
func doImport(
t db.Transaction,
store vsafedb.AddEntryRunner,
entryList []*jsonEntry,
key *vsafe.Key) error {
for i := range entryList {
if err := doSingleImport(t, store, entryList[i], key); err != nil {
return err
}
}
return nil
}
func doSingleImport(
t db.Transaction,
store vsafedb.AddEntryRunner,
jentry *jsonEntry,
key *vsafe.Key) error {
var entry vsafe.Entry
var err error
entry.Url, err = url.Parse(jentry.Url)
if err != nil {
return err
}
entry.Title = jentry.Title
entry.Desc = jentry.Desc
entry.UName = jentry.UName
entry.Password = jentry.Password
entry.Special = jentry.Special
_, err = vsafedb.AddEntry(store, t, key, &entry)
return err
}
func init() {
flag.StringVar(&fDb, "db", "", "Path to vsafe file")
flag.StringVar(&fName, "name", "", "User name")
flag.StringVar(&fPassword, "password", "password", "User password")
}