130 lines
2.6 KiB
Go
130 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"git.andreafazzi.eu/andrea/oef/client"
|
|
"git.andreafazzi.eu/andrea/oef/config"
|
|
"git.andreafazzi.eu/andrea/oef/orm"
|
|
"github.com/BurntSushi/toml"
|
|
"github.com/gocarina/gocsv"
|
|
)
|
|
|
|
type importer struct {
|
|
client *client.Client
|
|
importFuncs map[string]func(*client.Client, string) error
|
|
}
|
|
|
|
func newImporter(c *client.Client) *importer {
|
|
return &importer{
|
|
c,
|
|
map[string]func(*client.Client, string) error{
|
|
"contests": importContest,
|
|
"schools": importSchools,
|
|
"participants": importParticipants,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (i *importer) runImport(importer, filename string) error {
|
|
f, ok := i.importFuncs[importer]
|
|
if !ok {
|
|
return fmt.Errorf("Importer %s not defined!", importer)
|
|
}
|
|
return f(i.client, filename)
|
|
}
|
|
|
|
func importSchools(client *client.Client, filename string) error {
|
|
var schools []*orm.School
|
|
data, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := gocsv.Unmarshal(strings.NewReader(string(data)), &schools); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for _, s := range schools {
|
|
id, err := client.Create(s)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Println("Created school with ID", id)
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func importParticipants(client *client.Client, filename string) error {
|
|
var participants []*orm.Participant
|
|
data, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := gocsv.Unmarshal(strings.NewReader(string(data)), &participants); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for _, p := range participants {
|
|
id, err := client.Create(p)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Println("Created participant with ID", id)
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func importContest(client *client.Client, filename string) error {
|
|
log.Println("Reading contest data from toml file...")
|
|
var contest *orm.Contest
|
|
if _, err := toml.DecodeFile(filename, &contest); err != nil {
|
|
return err
|
|
}
|
|
|
|
contestID, err := client.Create(contest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Println("Created contest with ID", contestID)
|
|
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
var importer string
|
|
|
|
flag.StringVar(&importer, "importer", "contest", "Name of importer to use")
|
|
flag.Parse()
|
|
|
|
if flag.NArg() == 0 {
|
|
panic("A toml filename is needed as first argument of this script.")
|
|
}
|
|
conf := new(config.ConfigT)
|
|
if _, err := os.Stat("./config.yaml"); err != nil {
|
|
panic(err)
|
|
}
|
|
err := config.ReadFile("./config.yaml", conf)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
client, err := client.Dial(conf.Url, conf.Admin.Username, conf.Admin.Password)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
i := newImporter(client)
|
|
err = i.runImport(importer, flag.Arg(0))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|