-
Notifications
You must be signed in to change notification settings - Fork 3
/
restore_client.go
83 lines (66 loc) · 1.98 KB
/
restore_client.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
package restore
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"github.com/1set/gut/yos"
"github.com/MattHodge/go-teedy/teedy"
)
type Client struct {
client *teedy.Client
rootBackupDirectory string
rootTagBackupDirectory string
rootDocumentBackupDirectory string
tagJSONFileBaseName string
documentJSONFileBaseName string
documentAttachmentDirectory string
}
func NewRestoreClient(client *teedy.Client, directory string) *Client {
return &Client{
client: client,
rootBackupDirectory: directory,
rootTagBackupDirectory: filepath.Join(directory, "tags"),
rootDocumentBackupDirectory: filepath.Join(directory, "documents"),
tagJSONFileBaseName: "tag.json",
documentJSONFileBaseName: "document.json",
documentAttachmentDirectory: "files",
}
}
type DocumentRestoreStatus struct {
Title string
Id string
OldId string
}
type Status struct {
Documents []*DocumentRestoreStatus
}
// searchDirectoryForFiles searches a directory and its subdirectories for files with a specific name
func searchDirectoryForFiles(directory, filename string) ([]*yos.FilePathInfo, error) {
dirs, err := yos.ListDir(directory)
if err != nil {
return nil, fmt.Errorf("got error listing directory: %w", err)
}
var result []*yos.FilePathInfo
for _, dir := range dirs {
files, err := yos.ListFile(dir.Path)
if err != nil {
return nil, fmt.Errorf("got error listing files: %w", err)
}
for _, f := range files {
if f.Info.Name() == filename {
result = append(result, f)
}
}
}
return result, nil
}
// loadBackupFile takes a slice of files and an interface, loads the json from disk and returns a slice of interfaces
func loadBackupFile(file *yos.FilePathInfo, i interface{}) error {
dat, err := ioutil.ReadFile(file.Path)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
fmt.Printf("Loaded backup file %s\n", file.Path)
return json.Unmarshal(dat, i)
}