]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Add command line and config file parsing.
authorAnt Zucaro <azucaro@gmail.com>
Sun, 29 Oct 2017 20:56:44 +0000 (16:56 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Sun, 29 Oct 2017 20:56:44 +0000 (16:56 -0400)
xonstat/util/xs_glicko.go

index 67821f523f988e7795e7a7a8d741772b1e31dbbb..8bdcca4ee23f097e935b224dfd9d66b7828d9955 100644 (file)
@@ -1,9 +1,81 @@
 package main
 
 import (
+       "encoding/json"
+       "flag"
        "fmt"
+       "log"
+       "os"
 )
 
+const DefaultStartGameID = 0
+const DefaultEndGameID = -1
+const DefaultRankingWindowDays = 7
+
+type Config struct {
+       // database connection string
+       ConnStr string
+
+       // the starting game_id in the games table
+       StartGameID int
+
+       // the ending game_id in the games table
+       EndGameID int
+
+       // the number of days constituting the ranking window
+       RankingWindowDays int
+}
+
+func loadConfig(path string) (*Config, error) {
+       config := new(Config)
+
+       // defaults
+       config.ConnStr = "user=xonstat host=localhost dbname=xonstatdb sslmode=disable"
+       config.StartGameID = DefaultStartGameID
+       config.EndGameID = DefaultEndGameID
+       config.RankingWindowDays = DefaultRankingWindowDays
+
+       file, err := os.Open(path)
+       if err != nil {
+               fmt.Println("Failed opening the file.")
+               return config, err
+       }
+
+       decoder := json.NewDecoder(file)
+
+       // overwrite in-mem config with new values
+       err = decoder.Decode(config)
+       if err != nil {
+               fmt.Println("Failed to decode the JSON.")
+               return config, err
+       }
+
+       return config, nil
+}
+
 func main() {
-       fmt.Println("Hello, world")
+       path := flag.String("config", "xs_glicko.json", "configuration file path")
+       start := flag.Int("start", DefaultStartGameID, "starting game_id")
+       end := flag.Int("end", DefaultEndGameID, "ending game_id")
+       days := flag.Int("days", DefaultRankingWindowDays, "number of days in the ranking window")
+       flag.Parse()
+
+       config, err := loadConfig(*path)
+       if err != nil {
+               log.Fatalf("Unable to load config file: %s.\n", err)
+       }
+
+       if *start != DefaultStartGameID {
+               config.StartGameID = *start
+       }
+
+       if *end != DefaultEndGameID {
+               config.EndGameID = *end
+       }
+
+       if *days != DefaultRankingWindowDays {
+               config.RankingWindowDays = *days
+       }
+
+       fmt.Printf("%+v\n", config)
 }