-
Notifications
You must be signed in to change notification settings - Fork 8
/
import.go
41 lines (35 loc) · 924 Bytes
/
import.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
package main
// Copyright (C) 2013 Alexander Bauer, Luke Evers, Dylan Whichard,
// and contributors; (GPLv3) see LICENSE or doc.go
import (
"encoding/json"
"io"
"os"
)
// Import reads a slice of JSON-encoded Nodes from the given io.Reader
// and adds them to the database. Cache-related fields such as
// RetrieveTime are discarded.
func Import(r io.Reader) (err error) {
// Unmarshal the Nodes from the given io.Reader.
nodes := make([]*Node, 0)
err = json.NewDecoder(r).Decode(&nodes)
if err != nil {
return
}
// Insert them into the database as new. Timestamps will be the
// current time.
err = Db.AddNodes(nodes)
return
}
// ImportFile opens the given file and imports JSON-encoded Nodes from
// it.
func ImportFile(path string) (err error) {
// Open the file in readonly mode.
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
// Pass it along to Import.
return Import(f)
}