oef/scripts/export_credentials/main.go

74 lines
1.5 KiB
Go
Raw Permalink Normal View History

2021-11-10 05:45:53 +01:00
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")
2023-05-03 16:19:53 +02:00
cid := flag.Uint("cid", 0, "Export credentials of participants subscribed to the given contest")
2021-11-10 05:45:53 +01:00
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)
}
2023-05-03 16:19:53 +02:00
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})
}
2021-11-10 05:45:53 +01:00
}
}
f, err := os.Create(*output)
if err != nil {
panic(err)
}
defer f.Close()
gocsv.MarshalFile(credentials, f)
}