sync: Expose sync.Exists function
This commit is contained in:
parent
5cf4565573
commit
c5a059bd3b
8 changed files with 31 additions and 31 deletions
|
@ -18,7 +18,7 @@ import (
|
||||||
|
|
||||||
func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files []string, digests map[string][]byte, errs []error) {
|
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 no files directory, don't display error
|
||||||
if !i.exists(path.Join(exercice.Path, into)) {
|
if !i.Exists(path.Join(exercice.Path, into)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ func CheckExerciceFilesPresence(i Importer, exercice *fic.Exercice) (files []str
|
||||||
errs = append(errs, berrs...)
|
errs = append(errs, berrs...)
|
||||||
|
|
||||||
for _, fname := range flist {
|
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")))
|
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("No such file or directory")))
|
||||||
} else if _, ok := digests[fname]; !ok {
|
} else if _, ok := digests[fname]; !ok {
|
||||||
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("unable to import file: No digest given")))
|
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 {
|
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.")))
|
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("unable to read file: No such file or directory. Check your DIGESTS.txt for legacy entries.")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ func buildExerciceHints(i Importer, exercice *fic.Exercice, exceptions *CheckExc
|
||||||
if hint.Content != "" {
|
if hint.Content != "" {
|
||||||
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("content and filename can't be filled at the same time")))
|
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("content and filename can't be filled at the same time")))
|
||||||
continue
|
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)))
|
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("%q: File not found", hint.Filename)))
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -139,9 +139,9 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
||||||
e.Title = fixnbsp(e.Title)
|
e.Title = fixnbsp(e.Title)
|
||||||
|
|
||||||
// Texts to format using Markdown
|
// 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"))
|
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"))
|
e.Overview, err = GetFileContent(i, path.Join(epath, "overview.md"))
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("Unable to find overview.txt nor overview.md")
|
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"))
|
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"))
|
e.Statement, err = GetFileContent(i, path.Join(epath, "statement.md"))
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("Unable to find statement.txt nor statement.md")
|
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"))
|
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"))
|
e.Finished, err = GetFileContent(i, path.Join(epath, "finished.md"))
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -274,7 +274,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
||||||
resolutionFound := false
|
resolutionFound := false
|
||||||
|
|
||||||
e.VideoURI = path.Join(epath, "resolution.mp4")
|
e.VideoURI = path.Join(epath, "resolution.mp4")
|
||||||
if !i.exists(e.VideoURI) {
|
if !i.Exists(e.VideoURI) {
|
||||||
e.VideoURI = ""
|
e.VideoURI = ""
|
||||||
} else if size, err := GetFileSize(i, e.VideoURI); err != nil {
|
} else if size, err := GetFileSize(i, e.VideoURI); err != nil {
|
||||||
errs = append(errs, NewExerciceError(e, fmt.Errorf("resolution.mp4: %w", err), theme))
|
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")
|
writeup := path.Join(epath, "resolution.md")
|
||||||
if !i.exists(writeup) {
|
if !i.Exists(writeup) {
|
||||||
writeup = path.Join(epath, "resolution.txt")
|
writeup = path.Join(epath, "resolution.txt")
|
||||||
}
|
}
|
||||||
|
|
||||||
if i.exists(writeup) {
|
if i.Exists(writeup) {
|
||||||
if size, err := GetFileSize(i, writeup); err != nil {
|
if size, err := GetFileSize(i, writeup); err != nil {
|
||||||
errs = append(errs, NewExerciceError(e, fmt.Errorf("resolution.md: %w", err), theme))
|
errs = append(errs, NewExerciceError(e, fmt.Errorf("resolution.md: %w", err), theme))
|
||||||
} else if size == 0 {
|
} else if size == 0 {
|
||||||
|
|
|
@ -26,8 +26,8 @@ type Importer interface {
|
||||||
Init() error
|
Init() error
|
||||||
// sync tries to pull the latest modification of the underlying storage.
|
// sync tries to pull the latest modification of the underlying storage.
|
||||||
Sync() error
|
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).
|
||||||
toURL(filename string) string
|
toURL(filename string) string
|
||||||
// importFile imports the file at the given URI, inside the global FILES/ directory.
|
// importFile imports the file at the given URI, inside the global FILES/ directory.
|
||||||
|
@ -64,7 +64,7 @@ var GlobalImporter Importer
|
||||||
|
|
||||||
// GetFileSize returns the size.
|
// GetFileSize returns the size.
|
||||||
func GetFileSize(i Importer, URI string) (size int64, err error) {
|
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 {
|
if fi, err := i.stat(URI); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
} else {
|
} else {
|
||||||
|
@ -73,7 +73,7 @@ func GetFileSize(i Importer, URI string) (size int64, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
dirname := path.Dir(URI)
|
dirname := path.Dir(URI)
|
||||||
if i.exists(dirname) {
|
if i.Exists(dirname) {
|
||||||
filename := path.Base(URI)
|
filename := path.Base(URI)
|
||||||
if files, err := i.listDir(dirname); err != nil {
|
if files, err := i.listDir(dirname); err != nil {
|
||||||
return size, err
|
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.
|
// GetFile helps to manage huge file transfert by concatenating splitted (with split(1)) files.
|
||||||
func GetFile(i Importer, URI string) (io.Reader, func(), error) {
|
func GetFile(i Importer, URI string) (io.Reader, func(), error) {
|
||||||
// Import file if it exists
|
// Import file if it exists
|
||||||
if i.exists(URI) {
|
if i.Exists(URI) {
|
||||||
fd, err := i.getFile(URI)
|
fd, err := i.getFile(URI)
|
||||||
return fd, func() {
|
return fd, func() {
|
||||||
if fdc, ok := fd.(io.ReadCloser); ok {
|
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
|
// Try to find file parts
|
||||||
dirname := path.Dir(URI)
|
dirname := path.Dir(URI)
|
||||||
if i.exists(dirname) {
|
if i.Exists(dirname) {
|
||||||
filename := path.Base(URI)
|
filename := path.Base(URI)
|
||||||
if files, err := i.listDir(dirname); err != nil {
|
if files, err := i.listDir(dirname); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|
|
@ -50,7 +50,7 @@ func (i CloudImporter) Sync() error {
|
||||||
return nil
|
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)
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@ func countFileInDir(dirname string) (int, error) {
|
||||||
return len(files), nil
|
return len(files), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i GitImporter) exists(filename string) bool {
|
func (i GitImporter) Exists(filename string) bool {
|
||||||
return i.li.exists(filename)
|
return i.li.Exists(filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i GitImporter) toURL(filename string) string {
|
func (i GitImporter) toURL(filename string) string {
|
||||||
|
|
|
@ -47,7 +47,7 @@ func (i LocalImporter) Sync() error {
|
||||||
return nil
|
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)
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ func (i LocalImporter) importFile(URI string, next func(string, string) (interfa
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if i.exists(URI) {
|
if i.Exists(URI) {
|
||||||
os.Symlink(i.toURL(URI), dest)
|
os.Symlink(i.toURL(URI), dest)
|
||||||
return next(dest, URI)
|
return next(dest, URI)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -142,9 +142,9 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
|
||||||
|
|
||||||
var intro string
|
var intro string
|
||||||
var err error
|
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"))
|
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"))
|
intro, err = GetFileContent(i, path.Join(tdir, "overview.md"))
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("unable to find overview.txt nor overview.md")
|
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")
|
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")
|
th.Image = path.Join(tdir, "heading.png")
|
||||||
} else {
|
} else {
|
||||||
errs = append(errs, NewThemeError(th, fmt.Errorf("heading.jpg: No such file")))
|
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")
|
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")
|
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 {
|
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)))
|
errs = append(errs, NewThemeError(th, fmt.Errorf("unable to get partner's text: %w", err)))
|
||||||
} else {
|
} else {
|
||||||
|
|
Reference in a new issue