sync: Add Init and Sync functions

Init initializes the directory/repository before the first use.
Sync is called to unsure the directory is up-to-date.
This commit is contained in:
nemunaire 2021-10-26 19:56:40 +02:00
parent 281056a723
commit aebfb7bf96
7 changed files with 62 additions and 6 deletions

View File

@ -128,6 +128,9 @@ func main() {
sync.GlobalImporter, _ = sync.NewCloudImporter(cloudDAVBase, cloudUsername, cloudPassword)
}
if sync.GlobalImporter != nil {
if err := sync.GlobalImporter.Init(); err != nil {
log.Fatal("Unable to initialize the importer:", err)
}
log.Println("Using", sync.GlobalImporter.Kind())
}

View File

@ -243,6 +243,12 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic
// SyncExercices imports new or updates existing exercices, in a given theme.
func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
if !avoidImporterSync() {
if err := i.Sync(); err != nil {
errs = append(errs, err.Error())
}
}
if exercices, err := GetExercices(i, theme); err != nil {
errs = append(errs, err.Error())
} else {

View File

@ -19,6 +19,10 @@ import (
type Importer interface {
// Kind returns information about the Importer, for human interrest.
Kind() string
// init performs the importer initialization.
Init() error
// sync tries to pull the latest modification of the underlying storage.
Sync() error
// exists checks if the given location exists from the Importer point of view.
exists(filename string) bool
// toURL gets the full path/URL to the given file, the Importer will look internaly (used for debuging purpose).

View File

@ -22,6 +22,11 @@ var oneThemeDeepSync sync.Mutex
// DeepSyncProgress expose the progression of the depp synchronization (0 = 0%, 255 = 100%).
var DeepSyncProgress uint8
// avoidImporterSync checks if Sync should be called or not.
func avoidImporterSync() bool {
return DeepSyncProgress > 1 && DeepSyncProgress < 255
}
// SpeedySyncDeep performs a recursive synchronisation without importing files.
func SpeedySyncDeep(i Importer) (errs map[string][]string) {
oneDeepSync.Lock()
@ -36,6 +41,11 @@ func SpeedySyncDeep(i Importer) (errs map[string][]string) {
errs = map[string][]string{}
startTime := time.Now()
if err := i.Sync(); err != nil {
errs["_sync"] = []string{err.Error()}
}
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
errs["_themes"] = SyncThemes(i)
@ -89,6 +99,11 @@ func SyncDeep(i Importer) (errs map[string][]string) {
errs = map[string][]string{}
startTime := time.Now()
if err := i.Sync(); err != nil {
errs["_sync"] = []string{err.Error()}
}
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
errs["_themes"] = SyncThemes(i)
@ -171,6 +186,10 @@ func SyncThemeDeep(i Importer, theme fic.Theme, tid int, themeStep uint8) (errs
oneThemeDeepSync.Lock()
defer oneThemeDeepSync.Unlock()
if err := i.Sync(); err != nil {
errs = append(errs, err.Error())
}
DeepSyncProgress = 3 + uint8(tid)*themeStep
errs = SyncExercices(i, theme)

View File

@ -38,6 +38,14 @@ func (i CloudImporter) Kind() string {
return "cloud file importer: " + i.baseDAV.String()
}
func (i CloudImporter) Init() error {
return nil
}
func (i CloudImporter) Sync() error {
return nil
}
func (i CloudImporter) exists(filename string) bool {
fullURL := i.baseDAV
fullURL.Path = path.Join(fullURL.Path, filename)

View File

@ -2,6 +2,7 @@ package sync
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path"
@ -11,7 +12,7 @@ import (
// inside a local directory from your filesystem.
type LocalImporter struct {
// Base is the root directory used by the LocalImporter. It should contains all themes.
Base string
Base string
// Symlink changes the normal file copy/concatenate behaviour to symlink/concatenate.
// If enable, your base directory must be accessible by the frontend processus as it will follow the symlink.
Symlink bool
@ -25,6 +26,23 @@ func (i LocalImporter) Kind() string {
}
}
func (i LocalImporter) Init() error {
if f, err := os.Stat(i.Base); os.IsNotExist(err) {
if err = os.Mkdir(i.Base, 0751); err != nil {
return err
}
} else if err != nil {
return err
} else if !f.IsDir() {
return fmt.Errorf("%q exists and is not a directory", i.Base)
}
return nil
}
func (i LocalImporter) Sync() error {
return nil
}
func (i LocalImporter) exists(filename string) bool {
_, err := os.Stat(i.toURL(filename))
return !os.IsNotExist(err)
@ -46,7 +64,7 @@ func (i LocalImporter) importFile(URI string, next func(string, string) (interfa
os.Symlink(i.toURL(URI), dest)
return next(dest, URI)
} else {
os.Symlink(i.toURL(URI) + "_MERGED", dest)
os.Symlink(i.toURL(URI)+"_MERGED", dest)
return ImportFile(i, URI, next)
}
} else {

View File

@ -173,9 +173,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
}
// SyncThemes imports new or updates existing themes.
func SyncThemes(i Importer) []string {
var errs []string
func SyncThemes(i Importer) (errs []string) {
if themes, err := GetThemes(i); err != nil {
errs = append(errs, err.Error())
} else {
@ -233,7 +231,7 @@ func SyncThemes(i Importer) []string {
}
}
return errs
return
}
// ApiListRemoteThemes is an accessor letting foreign packages to access remote themes list.