From e486ad0c1b9be174442783324612f1488186363b Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 24 Sep 2021 09:54:25 +0200 Subject: [PATCH] Can pass multiple students files (concatenate them) --- main.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 8df516c..cf8c080 100644 --- a/main.go +++ b/main.go @@ -205,11 +205,22 @@ func ServeJSONStudent(w http.ResponseWriter, r *http.Request) { } } +type arrayFlags []string + +func (i *arrayFlags) String() string { + return fmt.Sprintf("%v", *i) +} + +func (i *arrayFlags) Set(value string) error { + *i = append(*i, value) + return nil +} + func main() { - var studentsFile string + var studentsFiles arrayFlags var bind = flag.String("bind", "0.0.0.0:8081", "Bind port/socket") - flag.StringVar(&studentsFile, "students", "./students.csv", "Path to a CSV file containing in the third column the name of the directory to look for") + flag.Var(&studentsFiles, "students", "Path to a CSV file containing in the third column the name of the directory to look for (can be repeated multiple times)") flag.StringVar(&rendusDir, "path", "./rendu/", "Path to the submissions directory (each subdirectory is a project)") flag.StringVar(&title, "title", "Rendus VIRLI", "Title of the page") flag.Parse() @@ -234,14 +245,21 @@ func main() { // Read and parse students list log.Println("Reading students files...") - if studentsFile, err = filepath.Abs(studentsFile); err != nil { - log.Fatal(err) - } else if fi, err := os.Open(studentsFile); err != nil { - log.Fatal(err) - } else { - r := csv.NewReader(bufio.NewReader(fi)) - if students, err = r.ReadAll(); err != nil { + for _, studentsFile := range studentsFiles { + log.Println("Reading", studentsFile, "...") + if studentsFile, err = filepath.Abs(studentsFile); err != nil { log.Fatal(err) + } else if fi, err := os.Open(studentsFile); err != nil { + log.Fatal(err) + } else { + var nstudents [][]string + + r := csv.NewReader(bufio.NewReader(fi)) + if nstudents, err = r.ReadAll(); err != nil { + log.Fatal(err) + } + + students = append(students, nstudents...) } } log.Println(len(students), "students loaded.")