199 lines
3.8 KiB
Go
199 lines
3.8 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
|
||
|
"git.andreafazzi.eu/andrea/probo/lib/sessionmanager"
|
||
|
"git.andreafazzi.eu/andrea/probo/lib/store/file"
|
||
|
"github.com/charmbracelet/bubbles/textinput"
|
||
|
tea "github.com/charmbracelet/bubbletea"
|
||
|
"github.com/urfave/cli/v2"
|
||
|
)
|
||
|
|
||
|
type model struct {
|
||
|
textInput textinput.Model
|
||
|
err error
|
||
|
}
|
||
|
|
||
|
type (
|
||
|
errMsg error
|
||
|
)
|
||
|
|
||
|
func initialModel() *model {
|
||
|
ti := textinput.New()
|
||
|
ti.Placeholder = "My exam session"
|
||
|
ti.Focus()
|
||
|
ti.CharLimit = 156
|
||
|
ti.Width = 20
|
||
|
|
||
|
return &model{
|
||
|
textInput: ti,
|
||
|
err: nil,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *model) Init() tea.Cmd {
|
||
|
return textinput.Blink
|
||
|
}
|
||
|
|
||
|
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||
|
var cmd tea.Cmd
|
||
|
|
||
|
switch msg := msg.(type) {
|
||
|
case tea.KeyMsg:
|
||
|
switch msg.Type {
|
||
|
case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:
|
||
|
return m, tea.Quit
|
||
|
}
|
||
|
|
||
|
// We handle errors just like any other message
|
||
|
case errMsg:
|
||
|
m.err = msg
|
||
|
return m, nil
|
||
|
}
|
||
|
|
||
|
m.textInput, cmd = m.textInput.Update(msg)
|
||
|
return m, cmd
|
||
|
}
|
||
|
|
||
|
func (m *model) View() string {
|
||
|
return fmt.Sprintf(
|
||
|
"\nPlease insert the name of the session\n\n%s\n\n%s",
|
||
|
m.textInput.View(),
|
||
|
"(esc to quit)",
|
||
|
) + "\n"
|
||
|
}
|
||
|
|
||
|
func push(cCtx *cli.Context) error {
|
||
|
pStore, err := file.NewParticipantDefaultFileStore()
|
||
|
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 {
|
||
|
m := initialModel()
|
||
|
p := tea.NewProgram(m)
|
||
|
if _, err := p.Run(); err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
sessionName = m.textInput.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.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)
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|