forked from pasela/alfred-chrome-history
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
80 lines (66 loc) · 1.4 KB
/
query.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/arbal/alfred-brave-history/history"
"github.com/arbal/alfred-brave-history/utils"
)
func queryHistory(profile, url, title string, limit int) ([]history.Entry, error) {
histFile := historyFile{
Profile: profile,
Clone: false,
}
defer histFile.Close()
filePath, err := histFile.GetPath()
if err != nil {
return nil, err
}
his, err := history.Open(filePath)
if err != nil {
return nil, err
}
defer his.Close()
return his.Query(url, title, limit)
}
type historyFile struct {
Profile string
Clone bool
tempDir string
autoRemove bool
}
func (h *historyFile) initTempDir() error {
if !h.Clone || h.tempDir != "" {
return nil
}
tempDir, err := ioutil.TempDir("", "alfred-brave-history")
if err != nil {
return err
}
h.tempDir = tempDir
h.autoRemove = true
return nil
}
func (h *historyFile) Close() error {
if h.autoRemove && h.tempDir != "" {
return os.RemoveAll(h.tempDir)
}
return nil
}
func (h *historyFile) GetPath() (string, error) {
origFile, err := history.GetHistoryPath(h.Profile)
if err != nil {
return "", err
}
if !h.Clone {
return origFile, nil
}
if err := h.initTempDir(); err != nil {
return "", err
}
clonedFile := filepath.Join(h.tempDir, filepath.Base(origFile))
if _, err := utils.CopyFile(origFile, clonedFile); err != nil {
return "", err
}
return clonedFile, nil
}