-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
91 lines (77 loc) · 1.97 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
package main
import (
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"go.hansaray.pw/lib/host"
log "go.hansaray.pw/lib/logger"
)
const configFileName = "pop3.toml"
var (
configFilePath string
configDirPath string
pop3Alias string
)
var Accounts map[string]Credentials
type Credentials struct {
Hostname string
Username string
Password string
Filepath string
}
func loadConfig() {
if len(pop3Alias) == 0 {
pop3Alias = "default"
}
if _, err := toml.DecodeFile(configFilePath, &Accounts); err != nil {
log.F("Config file: %s", err.Error())
}
var account = Accounts[pop3Alias]
if len(account.Hostname) != 0 {
if len(account.Username) != 0 {
if len(account.Password) != 0 {
Hostname = account.Hostname
Username = account.Username
Password = account.Password
if len(account.Filepath) != 0 {
Filepath = account.Filepath
}
} else {
log.F("Could not load pop3 account Password")
}
} else {
log.F("Could not load pop3 account Username")
}
} else {
log.F("Could not load pop3 account Hostname")
}
}
func UserConfigDir() string {
configPaths()
return configDirPath
}
func UserConfigPath() string {
configPaths()
return configFilePath
}
func configPaths() {
if len(configFilePath) == 0 {
var path string
path = host.UserConfigDir()
if len(path) == 0 {
log.F("Could not find default config path")
} else {
configDirPath = filepath.Join(path, "mail")
configFilePath = filepath.Join(configDirPath, configFileName)
}
}
}
// FileCreate creates the named file. If the file already exists an Error occurs,
// If the file does not exist, it is created with mode 0o600 (before umask).
// If successful, methods on the returned File can
// be used for I/O; the associated file descriptor has mode O_RDWR.
// The directory containing the file must already exist.
// If there is an error, it will be of type *PathError.
func FileCreate(name string) (*os.File, error) {
return os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0600)
}