-
Notifications
You must be signed in to change notification settings - Fork 3
/
filepath.go
78 lines (72 loc) · 1.54 KB
/
filepath.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
package main
import (
"errors"
"io"
"os"
"path/filepath"
"sync"
)
var (
workingDir string
workingDirOnce sync.Once
)
func getPalaceDir() string {
workingDirOnce.Do(func() {
homeDir, err := os.UserHomeDir()
if err != nil {
logFatal(err)
}
workingDir = filepath.Join(homeDir, ".moonpalace")
stat, err := os.Stat(workingDir)
switch {
case err == nil:
if !stat.IsDir() {
logFatal(errors.New(workingDir + " is not a directory"))
}
case os.IsNotExist(err):
if err = os.MkdirAll(workingDir, 0755); err != nil {
logFatal(err)
}
default:
logFatal(err)
}
})
return workingDir
}
func getPalaceSqlite() string {
palaceDir := getPalaceDir()
sqlitePath := filepath.Join(palaceDir, "moonpalace.sqlite")
if _, err := os.Stat(sqlitePath); err != nil {
if os.IsNotExist(err) {
if _, err = os.Create(sqlitePath); err != nil {
logFatal(err)
}
} else {
logFatal(err)
}
}
return sqlitePath
}
func getPalaceServerErrorLog() io.Writer {
palaceDir := getPalaceDir()
serverErrorLogPath := filepath.Join(palaceDir, "server_error.log")
file, err := os.OpenFile(serverErrorLogPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
logFatal(err)
}
return file
}
func getConfig() io.Reader {
palaceDir := getPalaceDir()
configPath := filepath.Join(palaceDir, "config.yaml")
open:
file, err := os.Open(configPath)
if err != nil {
if filepath.Ext(configPath) == ".yaml" && os.IsNotExist(err) {
configPath = filepath.Join(palaceDir, "config.yml")
goto open
}
return nil
}
return file
}