probo/cmd.bk/backup/session.go

145 lines
3 KiB
Go
Raw Permalink Normal View History

2023-12-21 17:38:05 +01:00
package main
import (
"fmt"
"log"
2024-02-06 09:03:57 +01:00
"git.andreafazzi.eu/andrea/probo/cmd/textinput"
"git.andreafazzi.eu/andrea/probo/pkg/sessionmanager"
"git.andreafazzi.eu/andrea/probo/pkg/store/file"
2023-12-21 17:38:05 +01:00
tea "github.com/charmbracelet/bubbletea"
"github.com/urfave/cli/v2"
)
func push(cCtx *cli.Context) error {
2024-02-06 08:35:07 +01:00
pStore, err := file.NewDefaultParticipantFileStore()
2023-12-21 17:38:05 +01:00
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 {
2023-12-27 15:05:11 +01:00
input := textinput.NewTextInput("My exam session")
p := tea.NewProgram(input)
2023-12-21 17:38:05 +01:00
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
2023-12-27 15:05:11 +01:00
sessionName = input.Value()
2023-12-21 17:38:05 +01:00
}
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)
}
2024-02-06 08:35:07 +01:00
pStore, err := file.NewDefaultParticipantFileStore()
2023-12-21 17:38:05 +01:00
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
}