sync: Expose sync.Exists function

This commit is contained in:
nemunaire 2023-05-03 10:54:02 +02:00
parent 5cf4565573
commit c5a059bd3b
8 changed files with 31 additions and 31 deletions

View file

@ -18,7 +18,7 @@ import (
func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files []string, digests map[string][]byte, errs []error) {
// If no files directory, don't display error
if !i.exists(path.Join(exercice.Path, into)) {
if !i.Exists(path.Join(exercice.Path, into)) {
return
}
@ -80,7 +80,7 @@ func CheckExerciceFilesPresence(i Importer, exercice *fic.Exercice) (files []str
errs = append(errs, berrs...)
for _, fname := range flist {
if !i.exists(path.Join(exercice.Path, "files", fname)) {
if !i.Exists(path.Join(exercice.Path, "files", fname)) {
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("No such file or directory")))
} else if _, ok := digests[fname]; !ok {
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("unable to import file: No digest given")))
@ -90,7 +90,7 @@ func CheckExerciceFilesPresence(i Importer, exercice *fic.Exercice) (files []str
}
for fname := range digests {
if !i.exists(path.Join(exercice.Path, "files", fname)) {
if !i.Exists(path.Join(exercice.Path, "files", fname)) {
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("unable to read file: No such file or directory. Check your DIGESTS.txt for legacy entries.")))
}
}

View file

@ -47,7 +47,7 @@ func buildExerciceHints(i Importer, exercice *fic.Exercice, exceptions *CheckExc
if hint.Content != "" {
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("content and filename can't be filled at the same time")))
continue
} else if !i.exists(path.Join(exercice.Path, "hints", hint.Filename)) {
} else if !i.Exists(path.Join(exercice.Path, "hints", hint.Filename)) {
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("%q: File not found", hint.Filename)))
continue
} else {

View file

@ -139,9 +139,9 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
e.Title = fixnbsp(e.Title)
// Texts to format using Markdown
if i.exists(path.Join(epath, "overview.txt")) {
if i.Exists(path.Join(epath, "overview.txt")) {
e.Overview, err = GetFileContent(i, path.Join(epath, "overview.txt"))
} else if i.exists(path.Join(epath, "overview.md")) {
} else if i.Exists(path.Join(epath, "overview.md")) {
e.Overview, err = GetFileContent(i, path.Join(epath, "overview.md"))
} else {
err = fmt.Errorf("Unable to find overview.txt nor overview.md")
@ -171,9 +171,9 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
}
}
if i.exists(path.Join(epath, "statement.txt")) {
if i.Exists(path.Join(epath, "statement.txt")) {
e.Statement, err = GetFileContent(i, path.Join(epath, "statement.txt"))
} else if i.exists(path.Join(epath, "statement.md")) {
} else if i.Exists(path.Join(epath, "statement.md")) {
e.Statement, err = GetFileContent(i, path.Join(epath, "statement.md"))
} else {
err = fmt.Errorf("Unable to find statement.txt nor statement.md")
@ -193,9 +193,9 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
}
}
if i.exists(path.Join(epath, "finished.txt")) {
if i.Exists(path.Join(epath, "finished.txt")) {
e.Finished, err = GetFileContent(i, path.Join(epath, "finished.txt"))
} else if i.exists(path.Join(epath, "finished.md")) {
} else if i.Exists(path.Join(epath, "finished.md")) {
e.Finished, err = GetFileContent(i, path.Join(epath, "finished.md"))
}
if err != nil {
@ -274,7 +274,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
resolutionFound := false
e.VideoURI = path.Join(epath, "resolution.mp4")
if !i.exists(e.VideoURI) {
if !i.Exists(e.VideoURI) {
e.VideoURI = ""
} else if size, err := GetFileSize(i, e.VideoURI); err != nil {
errs = append(errs, NewExerciceError(e, fmt.Errorf("resolution.mp4: %w", err), theme))
@ -288,11 +288,11 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
}
writeup := path.Join(epath, "resolution.md")
if !i.exists(writeup) {
if !i.Exists(writeup) {
writeup = path.Join(epath, "resolution.txt")
}
if i.exists(writeup) {
if i.Exists(writeup) {
if size, err := GetFileSize(i, writeup); err != nil {
errs = append(errs, NewExerciceError(e, fmt.Errorf("resolution.md: %w", err), theme))
} else if size == 0 {

View file

@ -26,8 +26,8 @@ type Importer interface {
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
// 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).
toURL(filename string) string
// importFile imports the file at the given URI, inside the global FILES/ directory.
@ -64,7 +64,7 @@ var GlobalImporter Importer
// GetFileSize returns the size.
func GetFileSize(i Importer, URI string) (size int64, err error) {
if i.exists(URI) {
if i.Exists(URI) {
if fi, err := i.stat(URI); err != nil {
return 0, err
} else {
@ -73,7 +73,7 @@ func GetFileSize(i Importer, URI string) (size int64, err error) {
}
dirname := path.Dir(URI)
if i.exists(dirname) {
if i.Exists(dirname) {
filename := path.Base(URI)
if files, err := i.listDir(dirname); err != nil {
return size, err
@ -104,7 +104,7 @@ func GetFileSize(i Importer, URI string) (size int64, err error) {
// GetFile helps to manage huge file transfert by concatenating splitted (with split(1)) files.
func GetFile(i Importer, URI string) (io.Reader, func(), error) {
// Import file if it exists
if i.exists(URI) {
if i.Exists(URI) {
fd, err := i.getFile(URI)
return fd, func() {
if fdc, ok := fd.(io.ReadCloser); ok {
@ -115,7 +115,7 @@ func GetFile(i Importer, URI string) (io.Reader, func(), error) {
// Try to find file parts
dirname := path.Dir(URI)
if i.exists(dirname) {
if i.Exists(dirname) {
filename := path.Base(URI)
if files, err := i.listDir(dirname); err != nil {
return nil, nil, err

View file

@ -50,7 +50,7 @@ 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.Path = path.Join(fullURL.Path, filename)

View file

@ -18,8 +18,8 @@ func countFileInDir(dirname string) (int, error) {
return len(files), nil
}
func (i GitImporter) exists(filename string) bool {
return i.li.exists(filename)
func (i GitImporter) Exists(filename string) bool {
return i.li.Exists(filename)
}
func (i GitImporter) toURL(filename string) string {

View file

@ -47,7 +47,7 @@ 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))
return !os.IsNotExist(err)
}
@ -68,7 +68,7 @@ func (i LocalImporter) importFile(URI string, next func(string, string) (interfa
return nil, err
}
if i.exists(URI) {
if i.Exists(URI) {
os.Symlink(i.toURL(URI), dest)
return next(dest, URI)
} else {

View file

@ -142,9 +142,9 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
var intro string
var err error
if i.exists(path.Join(tdir, "overview.txt")) {
if i.Exists(path.Join(tdir, "overview.txt")) {
intro, err = GetFileContent(i, path.Join(tdir, "overview.txt"))
} else if i.exists(path.Join(tdir, "overview.md")) {
} else if i.Exists(path.Join(tdir, "overview.md")) {
intro, err = GetFileContent(i, path.Join(tdir, "overview.md"))
} else {
err = fmt.Errorf("unable to find overview.txt nor overview.md")
@ -180,21 +180,21 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
}
}
if i.exists(path.Join(tdir, "heading.jpg")) {
if i.Exists(path.Join(tdir, "heading.jpg")) {
th.Image = path.Join(tdir, "heading.jpg")
} else if i.exists(path.Join(tdir, "heading.png")) {
} else if i.Exists(path.Join(tdir, "heading.png")) {
th.Image = path.Join(tdir, "heading.png")
} else {
errs = append(errs, NewThemeError(th, fmt.Errorf("heading.jpg: No such file")))
}
if i.exists(path.Join(tdir, "partner.jpg")) {
if i.Exists(path.Join(tdir, "partner.jpg")) {
th.PartnerImage = path.Join(tdir, "partner.jpg")
} else if i.exists(path.Join(tdir, "partner.png")) {
} else if i.Exists(path.Join(tdir, "partner.png")) {
th.PartnerImage = path.Join(tdir, "partner.png")
}
if i.exists(path.Join(tdir, "partner.txt")) {
if i.Exists(path.Join(tdir, "partner.txt")) {
if txt, err := GetFileContent(i, path.Join(tdir, "partner.txt")); err != nil {
errs = append(errs, NewThemeError(th, fmt.Errorf("unable to get partner's text: %w", err)))
} else {