73 lines
1.3 KiB
Go
Raw Normal View History

2019-01-04 13:02:17 +01:00
package worldconfig
import (
"bufio"
"os"
2019-01-13 16:37:03 +01:00
"strings"
2019-01-04 13:02:17 +01:00
)
const (
2019-01-13 16:37:03 +01:00
BACKEND_SQLITE3 string = "sqlite3"
BACKEND_FILES string = "files"
2019-01-04 13:02:17 +01:00
BACKEND_POSTGRES string = "postgresql"
)
const (
2019-02-07 08:02:13 +01:00
CONFIG_BACKEND string = "backend"
CONFIG_PLAYER_BACKEND string = "player_backend"
CONFIG_PSQL_CONNECTION string = "pgsql_connection"
CONFIG_PSQL_MAPSERVER string = "pgsql_mapserver_connection"
2019-01-04 13:02:17 +01:00
)
2019-01-04 10:44:41 +01:00
2019-01-04 16:10:21 +01:00
type PsqlConfig struct {
2019-01-13 16:37:03 +01:00
Host string
Port int
2019-01-04 16:10:21 +01:00
Username string
Password string
2019-01-13 16:37:03 +01:00
DbName string
2019-01-04 16:10:21 +01:00
}
2019-01-04 10:44:41 +01:00
type WorldConfig struct {
2019-01-04 13:02:17 +01:00
Backend string
PlayerBackend string
2019-01-04 15:02:25 +01:00
2019-02-07 08:02:13 +01:00
PsqlConnection string
MapObjectConnection string
2019-01-04 15:02:25 +01:00
}
2019-01-04 13:02:17 +01:00
func Parse(filename string) WorldConfig {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
cfg := WorldConfig{}
2019-01-04 15:02:25 +01:00
2019-01-04 13:02:17 +01:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
2019-01-05 17:21:24 +01:00
line := scanner.Text()
sepIndex := strings.Index(line, "=")
if sepIndex < 0 {
continue
2019-01-04 13:02:17 +01:00
}
2019-01-05 17:21:24 +01:00
valueStr := strings.Trim(line[sepIndex+1:], " ")
keyStr := strings.Trim(line[:sepIndex], " ")
switch keyStr {
case CONFIG_BACKEND:
cfg.Backend = valueStr
case CONFIG_PLAYER_BACKEND:
cfg.PlayerBackend = valueStr
case CONFIG_PSQL_CONNECTION:
2019-02-06 15:52:34 +01:00
cfg.PsqlConnection = valueStr
2019-01-28 14:47:53 +01:00
case CONFIG_PSQL_MAPSERVER:
2019-02-06 15:52:34 +01:00
cfg.MapObjectConnection = valueStr
2019-01-05 17:21:24 +01:00
}
2019-01-04 13:02:17 +01:00
}
2019-01-04 11:00:49 +01:00
2019-01-04 13:02:17 +01:00
return cfg
2019-01-04 11:00:49 +01:00
}