diff --git a/.gitignore b/.gitignore index 24c8b58..803b0fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -mapserver \ No newline at end of file +mapserver +world.mt diff --git a/main.go b/main.go index 9d6b01e..2c7bfb8 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,16 @@ package main import ( "mapserver/params" + "mapserver/worldconfig" + "fmt" ) func main() { - params.Parse() - p := params.Params() + p := params.Parse() if p.Help { return } + + worldcfg := worldconfig.Parse(p.Worlddir + "world.mt") + fmt.Println("Backend: ", worldcfg.Backend) } diff --git a/params/params.go b/params/params.go index 191d672..46221ee 100644 --- a/params/params.go +++ b/params/params.go @@ -10,13 +10,7 @@ type ParamsType struct { Help bool } -var params ParamsType - -func Params() ParamsType { - return params -} - -func Parse() { +func Parse() ParamsType { params := ParamsType{} flag.StringVar(&(params.Worlddir), "worlddir", "./", "world directory") @@ -27,4 +21,6 @@ func Parse() { if params.Help { flag.PrintDefaults() } + + return params } diff --git a/worldconfig/parse.go b/worldconfig/parse.go index b6f959e..4dcdd92 100644 --- a/worldconfig/parse.go +++ b/worldconfig/parse.go @@ -1,8 +1,53 @@ -package workdconfig +package worldconfig + +import ( + "bufio" + "os" + "strings" +) + +const ( + BACKEND_SQLITE3 string = "sqlite3" + BACKEND_FILES string = "files" + BACKEND_POSTGRES string = "postgresql" +) + +const ( + CONFIG_BACKEND string = "backend" + CONFIG_PLAYER_BACKEND string = "player_backend" +) type WorldConfig struct { + Backend string + PlayerBackend string } -func Parse(data []byte) WorldConfig { +func Parse(filename string) WorldConfig { + file, err := os.Open(filename) + if err != nil { + panic(err) + } + defer file.Close() + cfg := WorldConfig{} + scanner := bufio.NewScanner(file) + for scanner.Scan() { + sc := bufio.NewScanner(strings.NewReader(scanner.Text())) + sc.Split(bufio.ScanWords) + lastPart := "" + for sc.Scan() { + switch (lastPart) { + case CONFIG_BACKEND: + cfg.Backend = sc.Text() + case CONFIG_PLAYER_BACKEND: + cfg.PlayerBackend = sc.Text() + } + + if sc.Text() != "=" { + lastPart = sc.Text() + } + } + } + + return cfg } diff --git a/worldconfig/parse_test.go b/worldconfig/parse_test.go new file mode 100644 index 0000000..d365d96 --- /dev/null +++ b/worldconfig/parse_test.go @@ -0,0 +1,26 @@ +package worldconfig_test + +import ( + "testing" + worldconfig "mapserver/worldconfig" +) + +func TestParseSqlite(t *testing.T) { + cfg := worldconfig.Parse("./testdata/world.mt.sqlite") + if cfg.Backend != worldconfig.BACKEND_SQLITE3 { + t.Fatal("not sqlite3") + } + if cfg.PlayerBackend != worldconfig.BACKEND_FILES { + t.Fatal("not files") + } +} + +func TestParsePostgres(t *testing.T) { + cfg := worldconfig.Parse("./testdata/world.mt.postgres") + if cfg.Backend != worldconfig.BACKEND_POSTGRES { + t.Fatal("not postgres") + } + if cfg.PlayerBackend != worldconfig.BACKEND_POSTGRES { + t.Fatal("not postgres") + } +}