forked from browserutils/kooky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
76 lines (62 loc) · 1.5 KB
/
find.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
//go:build !windows
// +build !windows
package uzbl
import (
"os"
"path/filepath"
"github.com/j178/kooky"
"github.com/j178/kooky/internal/cookies"
"github.com/j178/kooky/internal/netscape"
)
type uzblFinder struct{}
var _ kooky.CookieStoreFinder = (*uzblFinder)(nil)
func init() {
kooky.RegisterFinder(`uzbl`, &uzblFinder{})
}
func (f *uzblFinder) FindCookieStores() ([]kooky.CookieStore, error) {
roots, err := uzblRoots()
if err != nil {
return nil, err
}
files := []string{`session-cookies.txt`, `cookies.txt`}
var ret []kooky.CookieStore
lastRoot := len(roots) - 1
lastFile := len(files) - 1
for i, root := range roots {
for j, filename := range files {
ret = append(
ret,
&cookies.CookieJar{
CookieStore: &netscape.CookieStore{
DefaultCookieStore: cookies.DefaultCookieStore{
BrowserStr: `uzbl`,
IsDefaultProfileBool: i == lastRoot && j == lastFile,
FileNameStr: filepath.Join(root, `uzbl`, filename),
},
},
},
)
}
}
return ret, nil
}
func uzblRoots() ([]string, error) {
var ret []string
home, errHome := os.UserHomeDir()
// old location
// fallback
if errHome == nil {
ret = append(ret, filepath.Join(home, `.config`))
}
if dir, ok := os.LookupEnv(`XDG_CONFIG_HOME`); ok {
ret = append(ret, dir)
}
// new location
if errHome == nil {
ret = append(ret, filepath.Join(home, `.local`, `share`))
}
if dataDir, ok := os.LookupEnv(`XDG_DATA_HOME`); ok {
ret = append(ret, dataDir)
}
return ret, nil
}