mapserver/worldconfig/parse.go

99 lines
1.8 KiB
Go
Raw Normal View History

2019-01-04 15:02:17 +03:00
package worldconfig
import (
"bufio"
2019-01-13 18:37:03 +03:00
"fmt"
2019-01-04 15:02:17 +03:00
"os"
2019-01-04 18:30:18 +03:00
"strconv"
2019-01-13 18:37:03 +03:00
"strings"
2019-01-04 15:02:17 +03:00
)
const (
2019-01-13 18:37:03 +03:00
BACKEND_SQLITE3 string = "sqlite3"
BACKEND_FILES string = "files"
2019-01-04 15:02:17 +03:00
BACKEND_POSTGRES string = "postgresql"
)
const (
2019-01-13 18:37:03 +03:00
CONFIG_BACKEND string = "backend"
CONFIG_PLAYER_BACKEND string = "player_backend"
CONFIG_PSQL_CONNECTION string = "pgsql_connection"
2019-01-04 17:02:25 +03:00
CONFIG_PSQL_PLAYER_CONNECTION string = "pgsql_player_connection"
2019-01-04 15:02:17 +03:00
)
2019-01-04 12:44:41 +03:00
2019-01-04 18:10:21 +03:00
type PsqlConfig struct {
2019-01-13 18:37:03 +03:00
Host string
Port int
2019-01-04 18:10:21 +03:00
Username string
Password string
2019-01-13 18:37:03 +03:00
DbName string
2019-01-04 18:10:21 +03:00
}
2019-01-04 12:44:41 +03:00
type WorldConfig struct {
2019-01-04 15:02:17 +03:00
Backend string
PlayerBackend string
2019-01-04 17:02:25 +03:00
2019-01-13 18:37:03 +03:00
PsqlConnection PsqlConfig
2019-01-05 19:21:24 +03:00
PsqlPlayerConnection PsqlConfig
2019-01-04 17:02:25 +03:00
}
2019-01-05 19:21:24 +03:00
func parseConnectionString(str string) PsqlConfig {
2019-01-04 18:30:18 +03:00
cfg := PsqlConfig{}
2019-01-05 19:21:24 +03:00
pairs := strings.Split(str, " ")
2019-01-04 18:30:18 +03:00
for _, pair := range pairs {
fmt.Println(pair)
kv := strings.Split(pair, "=")
switch kv[0] {
case "host":
cfg.Host = kv[1]
case "port":
cfg.Port, _ = strconv.Atoi(kv[1])
case "user":
cfg.Host = kv[1]
case "password":
cfg.Password = kv[1]
case "dbname":
cfg.DbName = kv[1]
}
}
2019-01-05 19:21:24 +03:00
return cfg
2019-01-04 12:44:41 +03:00
}
2019-01-04 15:02:17 +03:00
func Parse(filename string) WorldConfig {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
cfg := WorldConfig{}
2019-01-04 17:02:25 +03:00
2019-01-04 15:02:17 +03:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
2019-01-05 19:21:24 +03:00
line := scanner.Text()
sepIndex := strings.Index(line, "=")
if sepIndex < 0 {
continue
2019-01-04 15:02:17 +03:00
}
2019-01-05 19:21:24 +03: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:
cfg.PsqlConnection = parseConnectionString(valueStr)
case CONFIG_PSQL_PLAYER_CONNECTION:
cfg.PsqlPlayerConnection = parseConnectionString(valueStr)
}
2019-01-04 15:02:17 +03:00
}
2019-01-04 13:00:49 +03:00
2019-01-04 15:02:17 +03:00
return cfg
2019-01-04 13:00:49 +03:00
}