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:
parent
281056a723
commit
aebfb7bf96
@ -128,6 +128,9 @@ func main() {
|
|||||||
sync.GlobalImporter, _ = sync.NewCloudImporter(cloudDAVBase, cloudUsername, cloudPassword)
|
sync.GlobalImporter, _ = sync.NewCloudImporter(cloudDAVBase, cloudUsername, cloudPassword)
|
||||||
}
|
}
|
||||||
if sync.GlobalImporter != nil {
|
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())
|
log.Println("Using", sync.GlobalImporter.Kind())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.
|
// SyncExercices imports new or updates existing exercices, in a given theme.
|
||||||
func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
|
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 {
|
if exercices, err := GetExercices(i, theme); err != nil {
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
} else {
|
} else {
|
||||||
|
@ -19,6 +19,10 @@ import (
|
|||||||
type Importer interface {
|
type Importer interface {
|
||||||
// Kind returns information about the Importer, for human interrest.
|
// Kind returns information about the Importer, for human interrest.
|
||||||
Kind() string
|
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 checks if the given location exists from the Importer point of view.
|
||||||
exists(filename string) bool
|
exists(filename string) bool
|
||||||
// toURL gets the full path/URL to the given file, the Importer will look internaly (used for debuging purpose).
|
// toURL gets the full path/URL to the given file, the Importer will look internaly (used for debuging purpose).
|
||||||
|
@ -22,6 +22,11 @@ var oneThemeDeepSync sync.Mutex
|
|||||||
// DeepSyncProgress expose the progression of the depp synchronization (0 = 0%, 255 = 100%).
|
// DeepSyncProgress expose the progression of the depp synchronization (0 = 0%, 255 = 100%).
|
||||||
var DeepSyncProgress uint8
|
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.
|
// SpeedySyncDeep performs a recursive synchronisation without importing files.
|
||||||
func SpeedySyncDeep(i Importer) (errs map[string][]string) {
|
func SpeedySyncDeep(i Importer) (errs map[string][]string) {
|
||||||
oneDeepSync.Lock()
|
oneDeepSync.Lock()
|
||||||
@ -36,6 +41,11 @@ func SpeedySyncDeep(i Importer) (errs map[string][]string) {
|
|||||||
errs = map[string][]string{}
|
errs = map[string][]string{}
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
|
if err := i.Sync(); err != nil {
|
||||||
|
errs["_sync"] = []string{err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
|
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
|
||||||
errs["_themes"] = SyncThemes(i)
|
errs["_themes"] = SyncThemes(i)
|
||||||
|
|
||||||
@ -89,6 +99,11 @@ func SyncDeep(i Importer) (errs map[string][]string) {
|
|||||||
errs = map[string][]string{}
|
errs = map[string][]string{}
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
|
if err := i.Sync(); err != nil {
|
||||||
|
errs["_sync"] = []string{err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
|
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
|
||||||
errs["_themes"] = SyncThemes(i)
|
errs["_themes"] = SyncThemes(i)
|
||||||
|
|
||||||
@ -171,6 +186,10 @@ func SyncThemeDeep(i Importer, theme fic.Theme, tid int, themeStep uint8) (errs
|
|||||||
oneThemeDeepSync.Lock()
|
oneThemeDeepSync.Lock()
|
||||||
defer oneThemeDeepSync.Unlock()
|
defer oneThemeDeepSync.Unlock()
|
||||||
|
|
||||||
|
if err := i.Sync(); err != nil {
|
||||||
|
errs = append(errs, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
DeepSyncProgress = 3 + uint8(tid)*themeStep
|
DeepSyncProgress = 3 + uint8(tid)*themeStep
|
||||||
errs = SyncExercices(i, theme)
|
errs = SyncExercices(i, theme)
|
||||||
|
|
||||||
|
@ -38,6 +38,14 @@ func (i CloudImporter) Kind() string {
|
|||||||
return "cloud file importer: " + i.baseDAV.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 {
|
func (i CloudImporter) exists(filename string) bool {
|
||||||
fullURL := i.baseDAV
|
fullURL := i.baseDAV
|
||||||
fullURL.Path = path.Join(fullURL.Path, filename)
|
fullURL.Path = path.Join(fullURL.Path, filename)
|
||||||
|
@ -2,6 +2,7 @@ package sync
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -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 {
|
func (i LocalImporter) exists(filename string) bool {
|
||||||
_, err := os.Stat(i.toURL(filename))
|
_, err := os.Stat(i.toURL(filename))
|
||||||
return !os.IsNotExist(err)
|
return !os.IsNotExist(err)
|
||||||
|
@ -173,9 +173,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SyncThemes imports new or updates existing themes.
|
// SyncThemes imports new or updates existing themes.
|
||||||
func SyncThemes(i Importer) []string {
|
func SyncThemes(i Importer) (errs []string) {
|
||||||
var errs []string
|
|
||||||
|
|
||||||
if themes, err := GetThemes(i); err != nil {
|
if themes, err := GetThemes(i); err != nil {
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
} else {
|
} 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.
|
// ApiListRemoteThemes is an accessor letting foreign packages to access remote themes list.
|
||||||
|
Loading…
Reference in New Issue
Block a user