probo/cli/main.go

145 lines
3.3 KiB
Go
Raw Normal View History

2023-12-05 22:11:08 +01:00
package main
import (
2023-12-17 18:56:20 +01:00
"fmt"
2023-12-05 22:11:08 +01:00
"log"
"os"
2023-12-17 18:56:20 +01:00
"git.andreafazzi.eu/andrea/probo/sessionmanager"
2023-12-05 22:11:08 +01:00
"git.andreafazzi.eu/andrea/probo/store/file"
"github.com/urfave/cli/v2"
)
func main() {
file.DefaultBaseDir = "testdata"
2023-12-17 18:56:20 +01:00
sStore, err := file.NewDefaultSessionFileStore()
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
pStore, err := file.NewParticipantDefaultFileStore()
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
qStore, err := file.NewDefaultQuizFileStore()
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
sm, err := sessionmanager.NewSessionManager(
"http://localhost:8080/",
pStore.Storer,
qStore.Storer,
nil,
nil,
)
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
2023-12-05 22:11:08 +01:00
app := &cli.App{
Name: "probo-cli",
2023-12-17 18:56:20 +01:00
Usage: "Quiz Management System for Hackers!",
2023-12-05 22:11:08 +01:00
Commands: []*cli.Command{
2023-12-17 18:56:20 +01:00
{
Name: "init",
Aliases: []string{"i"},
Usage: "Initialize the current directory",
Action: func(cCtx *cli.Context) error {
return nil
},
},
2023-12-05 22:11:08 +01:00
{
Name: "session",
Aliases: []string{"s"},
Usage: "options for command 'session'",
Subcommands: []*cli.Command{
{
Name: "create",
Usage: "Create a new exam session",
Action: func(cCtx *cli.Context) error {
if cCtx.Args().Len() < 1 {
log.Fatalf("Please provide a session name as first argument of create.")
}
2023-12-17 18:56:20 +01:00
session, err := sm.Push(cCtx.Args().First())
2023-12-05 22:11:08 +01:00
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
2023-12-17 18:56:20 +01:00
_, err = sStore.Create(session)
2023-12-05 22:11:08 +01:00
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
2023-12-11 09:32:50 +01:00
2023-12-17 18:56:20 +01:00
log.Println("Session upload completed with success!")
for _, p := range pStore.ReadAll() {
log.Printf("http://localhost:8080/%v/%v", session.ID, p.Token)
}
return nil
},
},
{
Name: "pull",
Usage: "Download responses from a session",
Action: func(cCtx *cli.Context) error {
if cCtx.Args().Len() < 1 {
log.Fatalf("Please provide a session ID as first argument of pull.")
}
rStore, err := file.NewDefaultResponseFileStore()
2023-12-11 09:32:50 +01:00
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
2023-12-17 18:56:20 +01:00
err = sm.Pull(rStore, cCtx.Args().First())
2023-12-05 22:11:08 +01:00
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
2023-12-17 18:56:20 +01:00
log.Println("Responses download completed with success!")
2023-12-11 09:32:50 +01:00
2023-12-17 18:56:20 +01:00
return nil
},
},
{
Name: "scores",
Usage: "Show the scores for the given session",
Action: func(cCtx *cli.Context) error {
if cCtx.Args().Len() < 1 {
log.Fatalf("Please provide a session name as first argument of 'score'.")
2023-12-05 22:11:08 +01:00
}
2023-12-17 18:56:20 +01:00
session, err := sStore.Read(cCtx.Args().First())
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
rStore, err := file.NewDefaultResponseFileStore()
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
scores, err := sessionmanager.NewScores(rStore, session)
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
fmt.Println(scores)
2023-12-05 22:11:08 +01:00
return nil
},
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}