-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
46 lines (38 loc) · 816 Bytes
/
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
package goem
import (
"os"
"path/filepath"
"github.com/pelletier/go-toml"
"github.com/tennashi/goem/shellpath"
)
type Config struct {
RootDir string `toml:"root_dir"`
Server ServerConfig `toml:"server"`
}
type ServerConfig struct {
Port string `toml:"port"`
}
func NewConfig(path string) *Config {
path = configPath(path)
file, err := os.Open(path)
if err != nil {
return nil
}
defer file.Close()
config := &Config{}
if err := toml.NewDecoder(file).Decode(config); err != nil {
return nil
}
config.RootDir = shellpath.Resolve(config.RootDir)
return config
}
func configPath(path string) string {
if path != "" {
return path
}
path, err := os.UserConfigDir()
if err != nil {
return filepath.Join(".", "config.toml")
}
return filepath.Join(path, "goemd", "config.toml")
}