From ad44f8d1eb7aa5cde5d3baced15c0bcbba799384 Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Wed, 21 Apr 2021 14:16:23 +0200 Subject: [PATCH] read static scenario/group mapping from JSON file #61 --- configuration/config.go | 43 ++++++++++++++++++++++++++++++++++++--- configuration/groups.json | 3 +++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 configuration/groups.json diff --git a/configuration/config.go b/configuration/config.go index aaaa4e0..f3a537b 100644 --- a/configuration/config.go +++ b/configuration/config.go @@ -22,7 +22,10 @@ package configuration import ( + "encoding/json" "flag" + "fmt" + "io/ioutil" "log" "os" "strings" @@ -33,9 +36,7 @@ import ( // Global configuration var GlobalConfig *config.Config = nil -var ScenarioGroupMap = map[string][]int{ - "moodle-l2pmanager@13306": {1, 2, 3}, -} +var ScenarioGroupMap = map[string][]int{} func InitConfig() error { if GlobalConfig != nil { @@ -73,6 +74,7 @@ func InitConfig() error { contactName = flag.String("contact-name", "Steffen Vogel", "Name of the administrative contact") contactMail = flag.String("contact-mail", "svogel2@eonerc.rwth-aachen.de", "EMail of the administrative contact") testDataPath = flag.String("test-data-path", "database/testdata.json", "The path to the test data json file (used in test mode)") + groupsPath = flag.String("groups-path", "configuration/groups.json", "The path to the JSON file that maps user groups to scenario IDs") ) flag.Parse() @@ -103,6 +105,7 @@ func InitConfig() error { "contact.name": *contactName, "contact.mail": *contactMail, "test.datapath": *testDataPath, + "groups.path": *groupsPath, } if *s3NoSSL == true { @@ -162,5 +165,39 @@ func InitConfig() error { } } + gPath, err := GlobalConfig.String("groups.path") + if err != nil { + return err + } + + if gPath != "" { + err = readGroupsFile(gPath) + if err != nil { + return err + } + } + + return nil +} + +func readGroupsFile(path string) error { + + jsonFile, err := os.Open(path) + if err != nil { + return fmt.Errorf("error opening json file for groups: %v", err) + } + log.Println("Successfully opened json groups file", path) + + defer jsonFile.Close() + + byteValue, _ := ioutil.ReadAll(jsonFile) + + err = json.Unmarshal(byteValue, &ScenarioGroupMap) + if err != nil { + return fmt.Errorf("error unmarshalling json into ScenarioGroupMap: %v", err) + } + + log.Println("ScenarioGroupMap", ScenarioGroupMap) + return nil } diff --git a/configuration/groups.json b/configuration/groups.json new file mode 100644 index 0000000..e3f3c80 --- /dev/null +++ b/configuration/groups.json @@ -0,0 +1,3 @@ +{ + "moodle-l2pmanager@13306": [1, 2, 3] +} \ No newline at end of file