oef/scripts/export_schools/main.go

47 lines
999 B
Go

package main
import (
"flag"
"os"
"git.andreafazzi.eu/andrea/oef/client"
"git.andreafazzi.eu/andrea/oef/orm"
"github.com/gocarina/gocsv"
)
func main() {
username := flag.String("username", "admin", "Username")
password := flag.String("password", "admin", "Password")
noParticipants := flag.Bool("no-participants", false, "Filter schools with no participants")
output := flag.String("output", "schools.csv", "Output filename")
flag.Parse()
client, err := client.Dial(flag.Arg(0), *username, *password)
if err != nil {
panic(err)
}
schools := make([]*orm.School, 0)
err = client.ReadAll(&schools)
if err != nil {
panic(err)
}
f, err := os.Create(*output)
if err != nil {
panic(err)
}
defer f.Close()
if *noParticipants {
filteredSchools := make([]*orm.School, 0)
for _, school := range schools {
if len(school.Participants) == 0 {
filteredSchools = append(filteredSchools, school)
}
}
schools = filteredSchools
}
gocsv.MarshalFile(schools, f)
}