oef/scripts/export_credentials/main.go

73 lines
1.5 KiB
Go

package main
import (
"flag"
"log"
"os"
"git.andreafazzi.eu/andrea/oef/client"
"git.andreafazzi.eu/andrea/oef/orm"
"github.com/gocarina/gocsv"
)
type Credential struct {
ID uint
Username string
Password string
}
func main() {
username := flag.String("username", "admin", "Username")
password := flag.String("password", "admin", "Password")
output := flag.String("output", "credentials.csv", "Output filename")
cid := flag.Uint("cid", 0, "Export credentials of participants subscribed to the given contest")
flag.Parse()
client, err := client.Dial(flag.Arg(0), *username, *password)
if err != nil {
panic(err)
}
users := make([]*orm.User, 0)
credentials := make([]*Credential, 0)
log.Println("Get all users...")
err = client.ReadAll(&users)
if err != nil {
panic(err)
}
if *cid != 0 {
log.Printf("Get participants subscribed to contest ID %v", *cid)
contest := new(orm.Contest)
contest.ID = *cid
_, err := client.Read(contest)
if err != nil {
panic(err)
}
for _, participant := range contest.Participants {
for _, user := range users {
if user.ID == participant.UserID {
credentials = append(credentials, &Credential{user.ID, user.Username, user.Password})
}
}
}
} else {
for _, user := range users {
if user.Role == "participant" {
credentials = append(credentials, &Credential{user.ID, user.Username, user.Password})
}
}
}
f, err := os.Create(*output)
if err != nil {
panic(err)
}
defer f.Close()
gocsv.MarshalFile(credentials, f)
}