From: Ant Zucaro Date: Sun, 29 Oct 2017 20:56:44 +0000 (-0400) Subject: Add command line and config file parsing. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=39118f3fe085fceede9594bbb7b561fd09ed3d25;p=xonotic%2Fxonstat.git Add command line and config file parsing. --- diff --git a/xonstat/util/xs_glicko.go b/xonstat/util/xs_glicko.go index 67821f5..8bdcca4 100644 --- a/xonstat/util/xs_glicko.go +++ b/xonstat/util/xs_glicko.go @@ -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) }