server/admin/sync/exercice_keys.go

47 lines
1.1 KiB
Go

package sync
import (
"fmt"
"path"
"strings"
"srs.epita.fr/fic-server/libfic"
)
func SyncExerciceKeys(i Importer, exercice fic.Exercice) []string {
var errs []string
if _, err := exercice.WipeKeys(); err != nil {
errs = append(errs, err.Error())
} else if flags, err := getFileContent(i, path.Join(exercice.Path, "flags.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to read flags: %s", path.Base(exercice.Path), err))
} else {
for nline, flag := range strings.Split(flags, "\n") {
flag_splt := strings.SplitN(string(flag), "\t", 2)
if len(flag_splt) > 2 {
errs = append(errs, fmt.Sprintf("%q: error in flags file at line %d: invalid format", path.Base(exercice.Path), nline + 1))
continue
}
var label, rawkey string
if len(flag_splt) == 1 {
rawkey = flag_splt[0]
} else {
label = flag_splt[0]
rawkey = flag_splt[1]
}
if len(label) == 0 {
label = "Flag"
}
if _, err := exercice.AddRawKey(label, rawkey); err != nil {
errs = append(errs, fmt.Sprintf("%q: error in flags file at line %d: %s", path.Base(exercice.Path), nline + 1, err))
continue
}
}
}
return errs
}