probo/cmd.bk/backup/session.go

144 lines
3 KiB
Go

package main
import (
"fmt"
"log"
"git.andreafazzi.eu/andrea/probo/cmd/textinput"
"git.andreafazzi.eu/andrea/probo/pkg/sessionmanager"
"git.andreafazzi.eu/andrea/probo/pkg/store/file"
tea "github.com/charmbracelet/bubbletea"
"github.com/urfave/cli/v2"
)
func push(cCtx *cli.Context) error {
pStore, err := file.NewDefaultParticipantFileStore()
if err != nil {
return cli.Exit(fmt.Sprintf("An error occurred: %v", err), 1)
}
participants := pStore.ReadAll()
if len(participants) == 0 {
return cli.Exit("No participants found!", 1)
}
sessionName := cCtx.Args().First()
if cCtx.Args().Len() < 1 {
input := textinput.NewTextInput("My exam session")
p := tea.NewProgram(input)
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
sessionName = input.Value()
}
sStore, err := file.NewDefaultSessionFileStore()
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)
}
session, err := sm.Push(sessionName)
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
_, err = sStore.Create(session)
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
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
}
func pull(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()
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
pStore, err := file.NewDefaultParticipantFileStore()
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)
}
err = sm.Pull(rStore, cCtx.Args().First())
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
log.Println("Responses download completed with success!")
return nil
}
func scores(cCtx *cli.Context) error {
if cCtx.Args().Len() < 1 {
log.Fatalf("Please provide a session name as first argument of 'score'.")
}
sStore, err := file.NewDefaultSessionFileStore()
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
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)
return nil
}