use YAML for group scneario map

This commit is contained in:
Steffen Vogel 2021-04-29 09:24:12 +02:00
parent 174ed2dd2e
commit c9a14bb767
3 changed files with 8 additions and 6 deletions

View file

@ -22,7 +22,6 @@
package configuration package configuration
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -30,6 +29,8 @@ import (
"os" "os"
"strings" "strings"
"gopkg.in/yaml.v3"
"github.com/zpatrick/go-config" "github.com/zpatrick/go-config"
) )
@ -74,7 +75,7 @@ func InitConfig() error {
contactName = flag.String("contact-name", "Steffen Vogel", "Name of the administrative contact") 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") 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)") 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") groupsPath = flag.String("groups-path", "configuration/groups.json", "The path to a YAML file that maps user groups to scenario IDs")
) )
flag.Parse() flag.Parse()
@ -184,9 +185,9 @@ func ReadGroupsFile(path string) error {
byteValue, _ := ioutil.ReadAll(jsonFile) byteValue, _ := ioutil.ReadAll(jsonFile)
err = json.Unmarshal(byteValue, &ScenarioGroupMap) err = yaml.Unmarshal(byteValue, &ScenarioGroupMap)
if err != nil { if err != nil {
return fmt.Errorf("error unmarshalling json into ScenarioGroupMap: %v", err) return fmt.Errorf("error unmarshalling yaml into ScenarioGroupMap: %v", err)
} }
log.Println("ScenarioGroupMap", ScenarioGroupMap) log.Println("ScenarioGroupMap", ScenarioGroupMap)

1
go.mod
View file

@ -24,6 +24,7 @@ require (
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
gopkg.in/go-playground/validator.v9 v9.30.0 gopkg.in/go-playground/validator.v9 v9.30.0
gopkg.in/ini.v1 v1.51.0 // indirect gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
) )
go 1.15 go 1.15

View file

@ -99,13 +99,13 @@ func main() {
gPath, err := configuration.GlobalConfig.String("groups.path") gPath, err := configuration.GlobalConfig.String("groups.path")
if err != nil { if err != nil {
log.Fatalf("Error reading path to groups JSON file: %s, aborting.", err) log.Fatalf("Error reading path to groups YAML file: %s, aborting.", err)
} }
if gPath != "" { if gPath != "" {
err = configuration.ReadGroupsFile(gPath) err = configuration.ReadGroupsFile(gPath)
if err != nil { if err != nil {
log.Fatalf("Error reading groups JSON file: %s, aborting.", err) log.Fatalf("Error reading groups YAML file: %s, aborting.", err)
} }
} }