package main import ( "fmt" "log" "os" "git.andreafazzi.eu/andrea/probo/sessionmanager" "git.andreafazzi.eu/andrea/probo/store/file" "github.com/urfave/cli/v2" ) func main() { file.DefaultBaseDir = "testdata" 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) } app := &cli.App{ Name: "probo-cli", Usage: "Quiz Management System for Hackers!", Commands: []*cli.Command{ { Name: "init", Aliases: []string{"i"}, Usage: "Initialize the current directory", Action: func(cCtx *cli.Context) error { return nil }, }, { 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.") } session, err := sm.Push(cCtx.Args().First()) 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 }, }, { 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() 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 }, }, { 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'.") } 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 }, }, }, }, }, } if err := app.Run(os.Args); err != nil { log.Fatal(err) } }