/
conf.go
76 lines (64 loc) · 1.19 KB
/
conf.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
package conf
import (
"os"
"github.com/BurntSushi/toml"
)
type Mysql struct {
Username string
Password string
Host string
Dbname string
Ipaddr string
Port int64
}
type File struct {
Hostpath string
Grouppath string
}
type Config struct {
Mode string
Mysqldb Mysql `toml:"mysql"`
Fileinfo File `toml:"file"`
Filepath string
}
func Newconfig(filename string) (*Config, error) {
config := &Config{}
config.Filepath = filename
_, err := toml.DecodeFile(config.Filepath, config)
if err != nil {
return &Config{}, err
}
return config, nil
}
func (c *Config) Getmode() string {
return c.Mode
}
func (c *Config) GetDBparams() Mysql {
return c.Mysqldb
}
func (c *Config) GetFileinfo() File {
return c.Fileinfo
}
func (c *Config) GetHostfile(filepath string) (*File, error) {
var fileinfo File
/*
filepath := c.Fileinfo.Hostpath
workdir, err := Getworkdir()
if err != nil {
return File{}, err
}
filepath = workdir + filepath
*/
_, err := toml.DecodeFile(filepath, fileinfo)
if err != nil {
return &File{}, err
}
return &fileinfo, nil
}
func Getworkdir() (string, error) {
pwd, err := os.Getwd()
if err != nil {
return "", err
}
return pwd, nil
}