package config import ( "fmt" "io/ioutil" "regexp" "strings" ) //initProperties xxx func initProperties(serverFolder string) []ServerConfig { properties := []ServerConfig{} b, err := ioutil.ReadFile(serverFolder + "/server.properties") // just pass the file name if err != nil { fmt.Print(err) } str := string(b) // convert content to a 'string' lines := strings.Split(str, "\n") for _, line := range lines { regex := regexp.MustCompile("^(.*)=(.*[^=]*)$") regexSubmatch := regex.FindStringSubmatch(line) if len(regexSubmatch) > 0 { item := ServerConfig{ Key: strings.TrimSpace(regexSubmatch[1]), Value: strings.TrimSpace(regexSubmatch[2]), } properties = append(properties, item) } } return properties } //ReadAllProperties is exported function func (mch *Handler) ReadAllProperties() []ServerConfig { return mch.properties } //ChangeProperties is exported function func (mch *Handler) ChangeProperties(inputKey string, inputValue string) bool { for i, item := range mch.properties { if strings.ToLower(inputKey) == strings.ToLower(item.Key) { mch.properties[i].Value = inputValue } } return true } //SaveProperties is exported function func (mch *Handler) SaveProperties() bool { TXT := "#Minecraft server properties" for _, line := range mch.properties { TXT += line.Key + "=" + line.Value + "\n" } err := ioutil.WriteFile(mch.serverFolder+"/server.properties", []byte(TXT), 0777) if err != nil { fmt.Println(err) } return true } //There is no delete or add function because it is not necessary :)