-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
executable file
·119 lines (110 loc) · 2.52 KB
/
config.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
111
112
113
114
115
116
117
118
119
package config
import (
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"github.com/vinser/flibgolite/pkg/locales"
"github.com/vinser/flibgolite/pkg/rlog"
"gopkg.in/yaml.v3"
)
// See config.yml for comments about this struct
type Config struct {
Library struct {
STOCK_DIR string `yaml:"STOCK"`
TRASH_DIR string `yaml:"TRASH"`
NEW_DIR string `yaml:"NEW"`
}
Database struct {
DSN string `yaml:"DSN"`
INIT_SCRIPT string `yaml:"INIT_SCRIPT"`
DROP_SCRIPT string `yaml:"DROP_SCRIPT"`
POLL_DELAY int `yaml:"POLL_DELAY"`
MAX_SCAN_THREADS int `yaml:"MAX_SCAN_THREADS"`
}
Genres struct {
TREE_FILE string `yaml:"TREE_FILE"`
}
Logs struct {
OPDS string `yaml:"OPDS"`
SCAN string `yaml:"SCAN"`
LEVEL string `yaml:"LEVEL"`
}
OPDS struct {
PORT int `yaml:"PORT"`
TITLE string `yaml:"TITLE"`
PAGE_SIZE int `yaml:"PAGE_SIZE"`
}
locales.Locales
}
func makeAbs(path string) string {
if path == "" {
return ""
}
if filepath.IsAbs(path) {
return path
}
execpath, err := os.Executable()
if err != nil {
return "."
}
return filepath.Join(filepath.Dir(execpath), path)
}
func LoadConfig() *Config {
var (
b []byte
err error
)
expath, _ := os.Executable()
dir := filepath.Dir(expath)
configFile := filepath.Join(dir, "config", "config.yml")
b, err = os.ReadFile(configFile)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
err := os.MkdirAll(filepath.Dir(configFile), 0775)
if err != nil && !os.IsExist(err) {
log.Fatal(err)
}
err = os.WriteFile(configFile, []byte(CONFIG_YML), 0775)
if err != nil {
log.Fatal(err)
}
b, err = os.ReadFile(configFile)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
c := &Config{}
if err := yaml.Unmarshal([]byte(b), c); err != nil {
log.Fatal(err)
}
c.Library.STOCK_DIR = makeAbs(c.Library.STOCK_DIR)
c.Library.TRASH_DIR = makeAbs(c.Library.TRASH_DIR)
if len(c.Library.NEW_DIR) > 0 {
c.Library.NEW_DIR = makeAbs(c.Library.NEW_DIR)
}
c.Locales.DIR = makeAbs(c.Locales.DIR)
c.Genres.TREE_FILE = makeAbs(c.Genres.TREE_FILE)
c.Database.DSN = makeAbs(c.Database.DSN)
c.Logs.OPDS = makeAbs(c.Logs.OPDS)
c.Logs.SCAN = makeAbs(c.Logs.SCAN)
//
return c
}
func (c *Config) InitLogs(needOpds bool) (stockLog, opdsLog *rlog.Log) {
stockLog = nil
opdsLog = nil
stockLog = rlog.NewLog(c.Logs.SCAN, c.Logs.LEVEL)
if needOpds {
if c.Logs.SCAN == c.Logs.OPDS {
opdsLog = stockLog
} else {
opdsLog = rlog.NewLog(c.Logs.OPDS, c.Logs.LEVEL)
}
}
return
}