-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
53 lines (47 loc) · 1.04 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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
var (
configPath = "/etc/sysconfig/gobsips"
)
type Config struct {
Username string
Password string
ListenHost string
ListenPort string
}
func loadConfig() Config {
config := Config{}
if data, err := os.ReadFile(configPath); err == nil {
scanner := bufio.NewScanner(strings.NewReader(string(data)))
for scanner.Scan() {
parts := strings.SplitN(scanner.Text(), "=", 2)
if len(parts) == 2 {
key, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
switch key {
case "USERNAME":
config.Username = value
case "PASSWORD":
config.Password = value
case "LISTEN_HOST":
config.ListenHost = value
case "LISTEN_PORT":
config.ListenPort = value
}
}
}
}
return config
}
func saveConfig(config Config) error {
content := fmt.Sprintf(`USERNAME=%s
PASSWORD=%s
LISTEN_HOST=%s
LISTEN_PORT=%s
`, config.Username, config.Password, config.ListenHost, config.ListenPort)
return os.WriteFile(configPath, []byte(content), 0644)
}