admin: Implement theme synchronization
This commit is contained in:
parent
38606f28c7
commit
4d1dde4528
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"srs.epita.fr/fic-server/admin/sync"
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
@ -37,6 +38,14 @@ func init() {
|
|||||||
|
|
||||||
router.GET("/api/themes/:thid/exercices/:eid/keys", apiHandler(exerciceHandler(listExerciceKeys)))
|
router.GET("/api/themes/:thid/exercices/:eid/keys", apiHandler(exerciceHandler(listExerciceKeys)))
|
||||||
router.POST("/api/themes/:thid/exercices/:eid/keys", apiHandler(exerciceHandler(createExerciceKey)))
|
router.POST("/api/themes/:thid/exercices/:eid/keys", apiHandler(exerciceHandler(createExerciceKey)))
|
||||||
|
|
||||||
|
// Remote
|
||||||
|
router.GET("/api/remote/themes", apiHandler(sync.ApiListRemoteThemes))
|
||||||
|
router.GET("/api/remote/themes/:thname", apiHandler(sync.ApiGetRemoteTheme))
|
||||||
|
|
||||||
|
// Synchronize
|
||||||
|
router.GET("/api/sync/themes", apiHandler(
|
||||||
|
func(_ httprouter.Params, _ []byte) (interface{}, error) { return sync.SyncThemes(sync.GlobalImporter), nil }))
|
||||||
}
|
}
|
||||||
|
|
||||||
func bindingFiles(_ httprouter.Params, body []byte) (interface{}, error) {
|
func bindingFiles(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||||
|
75
admin/sync/themes.go
Normal file
75
admin/sync/themes.go
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package sync
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"srs.epita.fr/fic-server/libfic"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getThemes(i Importer) ([]string, error) {
|
||||||
|
var themes []string
|
||||||
|
|
||||||
|
if dirs, err := i.listDir("/"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
for _, dir := range dirs {
|
||||||
|
if _, err := i.listDir(dir); err == nil {
|
||||||
|
themes = append(themes, dir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return themes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAuthors(i Importer, tname string) ([]string, error) {
|
||||||
|
if authors, err := getFileContent(i, path.Join(tname, "AUTHORS.txt")); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return strings.Split(strings.TrimSpace(authors), "\n"), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SyncThemes(i Importer) []string {
|
||||||
|
var errs []string
|
||||||
|
|
||||||
|
if themes, err := getThemes(i); err != nil {
|
||||||
|
errs = append(errs, err.Error())
|
||||||
|
} else {
|
||||||
|
for _, tname := range themes {
|
||||||
|
if authors, err := getAuthors(i, tname); err != nil {
|
||||||
|
errs = append(errs, fmt.Sprintf("%q: unable to get AUTHORS: %s", tname, err))
|
||||||
|
continue
|
||||||
|
} else if theme, err := fic.GetThemeByName(tname); err != nil {
|
||||||
|
if _, err := fic.CreateTheme(tname, strings.Join(authors, ", ")); err != nil {
|
||||||
|
errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tname, err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
authors_str := strings.Join(authors, ", ")
|
||||||
|
|
||||||
|
if theme.Name != tname || theme.Authors != authors_str {
|
||||||
|
theme.Name = tname
|
||||||
|
theme.Authors = authors_str
|
||||||
|
if _, err := theme.Update(); err != nil {
|
||||||
|
errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tname, err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errs
|
||||||
|
}
|
||||||
|
|
||||||
|
func ApiListRemoteThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||||
|
return getThemes(GlobalImporter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ApiGetRemoteTheme(ps httprouter.Params, _ []byte) (interface{}, error) {
|
||||||
|
return getAuthors(GlobalImporter, ps.ByName("thid"))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user