Files
2025-11-24 11:18:08 +08:00

114 lines
1.9 KiB
Go

package handwork
import (
"bufio"
"fmt"
"os"
"strings"
)
type Conf struct {
Ip string
Port string
Auto bool
Clusterjoin bool
Key string
}
type MemoryConf struct {
Ip string
Port string
}
func trueorfalse(i string) bool {
if i == "true" {
return true
} else {
return false
}
}
func checkConf(i Conf) bool {
if i.Ip == "" || i.Port == "" || i.Key == "" {
return false
} else {
return true
}
}
var conf Conf
var memoryconf MemoryConf
func checkMemory(i MemoryConf) bool {
if i.Ip == "" || i.Port == "" {
return false
} else {
return true
}
}
func Hand() bool {
file, error := os.Open("./cluster.conf")
if error != nil {
fmt.Println("配置文件出错")
}
defer file.Close()
scanner := bufio.NewScanner(file)
conf.Auto = true
conf.Clusterjoin = false
var currentPart string
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
sectionName := line[1 : len(line)-1]
currentPart = sectionName
if sectionName != "serve" && sectionName != "selfmemory" {
panic("参数错误1")
}
continue
}
param := strings.Split(line, " ")
if len(param) != 2 {
panic("参数错误2")
}
key := param[0]
value := param[1]
switch currentPart {
case "serve":
if key == "ip" {
conf.Ip = value
} else if key == "port" {
conf.Port = value
} else if key == "key" {
conf.Key = value
} else if key == "autoconf" {
conf.Auto = trueorfalse(value)
} else if key == "clusterjoin" {
conf.Clusterjoin = trueorfalse(value)
}
case "selfmemory":
if key == "ip" {
memoryconf.Ip = value
} else if key == "port" {
memoryconf.Port = value
}
}
}
if checkConf(conf) && checkMemory(memoryconf) {
fmt.Println("自检通过")
if conf.Clusterjoin {
go Sendhand(conf.Clusterjoin)
}
return true
} else {
fmt.Println("参数错误")
return false
}
}