This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
files.go
110 lines (97 loc) · 2.41 KB
/
files.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
package golem
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/tkerber/golem/xdg"
)
// files keeps track of all files golem uses.
type files struct {
configDir string
cacheDir string
cookies string
rc string
searchEngines string
quickmarks string
bookmarks string
histfile string
downloadDir string
filterlistDir string
}
// configFiles is an array of all of golems config files.
var configFiles = []string{
"golemrc",
"searchengines",
"quickmarks",
"bookmarks",
}
// newFiles initializes the files golem uses.
func (g *Golem) newFiles() (*files, error) {
downloads := xdg.GetUserDownloadDir()
// Default to "$HOME/Downloads"
if downloads == "" {
downloads = filepath.Join(xdg.GetHomeDir(), "Downloads")
}
configDir := xdg.GetUserConfigDir()
configDir = filepath.Join(configDir, "golem", g.profile)
err := os.MkdirAll(configDir, 0700)
if err != nil {
return nil, err
}
filterlistDir := filepath.Join(configDir, "filterlists")
err = os.MkdirAll(filterlistDir, 0700)
if err != nil {
return nil, err
}
cacheDir := xdg.GetUserCacheDir()
cacheDir = filepath.Join(cacheDir, "golem", g.profile)
err = os.MkdirAll(cacheDir, 0700)
if err != nil {
return nil, err
}
cookies := filepath.Join(configDir, "cookies")
configFiles, err := initConfigFiles(configDir)
if err != nil {
return nil, err
}
return &files{
configDir,
cacheDir,
cookies,
configFiles[0],
configFiles[1],
configFiles[2],
configFiles[3],
filepath.Join(configDir, "history"),
downloads,
filterlistDir,
}, nil
}
// rcFiles returns a list of all files golem should use as rc files.
func (fs *files) rcFiles() []string {
return []string{fs.rc, fs.searchEngines, fs.quickmarks, fs.bookmarks}
}
// initConfigFiles ensures all config files exist in the specified config
// dir.
func initConfigFiles(configDir string) ([]string, error) {
locations := make([]string, len(configFiles))
for i, file := range configFiles {
locations[i] = filepath.Join(configDir, file)
// If the config file does not exist, we create it with the default
// content.
_, err := os.Stat(locations[i])
if err != nil && os.IsNotExist(err) {
defaultCont, err := Asset(file)
if err != nil {
return nil, err
}
err = ioutil.WriteFile(locations[i], defaultCont, 0600)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
}
return locations, nil
}