-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
57 lines (51 loc) · 981 Bytes
/
func.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
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
)
type Config struct {
Server struct {
Address string `yaml:"address"`
}
}
func readConfig()(*Config,error){
config := &Config{}
Dir,err:=os.Executable()
log.Print(Dir)
if err != nil {
log.Fatal(err)
}
configPath:=getConfigDirectory()+"/"+configFileName
file,err:=os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
d := yaml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
func getConfigDirectory()string{
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
return dir
}
func writeConfigState(address string){
var config Config
config.Server.Address=address
d, err := yaml.Marshal(&config)
if err != nil {
log.Fatalf("error: %v", err)
}
err = ioutil.WriteFile(getConfigDirectory()+"/"+configFileName, d, 0644)
if err != nil {
log.Fatal(err)
}
}