sync: Expose GetFile and GetFileContent functions

This commit is contained in:
nemunaire 2022-05-24 21:52:58 +02:00
parent d24b1c5d4d
commit 560110ba5e
6 changed files with 21 additions and 21 deletions

View File

@ -75,7 +75,7 @@ type ExerciceParams struct {
// parseExerciceParams reads challenge definitions from defines.txt and extract usefull data to set up the challenge.
func parseExerciceParams(i Importer, exPath string) (p ExerciceParams, md toml.MetaData, err error) {
var defs string
defs, err = getFileContent(i, path.Join(exPath, "challenge.txt"))
defs, err = GetFileContent(i, path.Join(exPath, "challenge.txt"))
if err != nil {
return
}

View File

@ -21,7 +21,7 @@ func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files
}
// Parse DIGESTS.txt
if digs, err := getFileContent(i, path.Join(exercice.Path, into, "DIGESTS.txt")); err != nil {
if digs, err := GetFileContent(i, path.Join(exercice.Path, into, "DIGESTS.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to read DIGESTS.txt: %s", path.Base(exercice.Path), err))
} else {
digests = map[string][]byte{}
@ -104,7 +104,7 @@ func CheckExerciceFiles(i Importer, exercice *fic.Exercice) (files []string, err
for _, fname := range flist {
w, hash160, hash512 := fic.CreateHashBuffers()
if err := getFile(i, path.Join(exercice.Path, "files", fname), bufio.NewWriter(w)); err != nil {
if err := GetFile(i, path.Join(exercice.Path, "files", fname), bufio.NewWriter(w)); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to read file %q: %s", path.Base(exercice.Path), fname, err))
continue
} else if _, err := fic.CheckBufferHash(hash160, hash512, digests[fname]); err != nil {

View File

@ -107,7 +107,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
}
// Overwrite title if title.txt exists
if myTitle, err := getFileContent(i, path.Join(epath, "title.txt")); err == nil {
if myTitle, err := GetFileContent(i, path.Join(epath, "title.txt")); err == nil {
myTitle = strings.TrimSpace(myTitle)
if strings.Contains(myTitle, "\n") {
errs = append(errs, fmt.Sprintf("%q: title.txt: Title can't contain new lines", edir))
@ -121,9 +121,9 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
// Texts to format using Markdown
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")) {
e.Overview, err = getFileContent(i, 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")
}
@ -146,9 +146,9 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
}
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")) {
e.Statement, err = getFileContent(i, 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")
}
@ -161,7 +161,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
}
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"))
if err != nil {
errs = append(errs, fmt.Sprintf("%q: finished.txt: %s", edir, err))
} else {
@ -250,7 +250,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error()))
} else if size == 0 {
errs = append(errs, fmt.Sprintf("%q: resolution.md: The file is empty!", edir))
} else if e.Resolution, err = getFileContent(i, writeup); err != nil {
} else if e.Resolution, err = GetFileContent(i, writeup); err != nil {
errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error()))
} else if e.Resolution, err = ProcessMarkdown(i, e.Resolution, epath); err != nil {
errs = append(errs, fmt.Sprintf("%q: resolution.md: error during markdown processing: %s", edir, err.Error()))

View File

@ -95,8 +95,8 @@ func getFileSize(i Importer, URI string) (size int64, err error) {
return size, fmt.Errorf("%q: no such file or directory", URI)
}
// getFile helps to manage huge file transfert by concatenating splitted (with split(1)) files.
func getFile(i Importer, URI string, writer *bufio.Writer) error {
// GetFile helps to manage huge file transfert by concatenating splitted (with split(1)) files.
func GetFile(i Importer, URI string, writer *bufio.Writer) error {
// Import file if it exists
if i.exists(URI) {
return i.getFile(URI, writer)
@ -130,11 +130,11 @@ func getFile(i Importer, URI string, writer *bufio.Writer) error {
return fmt.Errorf("%q: no such file or directory", URI)
}
// getFileContent retrieves the content of the given text file.
func getFileContent(i Importer, URI string) (string, error) {
// GetFileContent retrieves the content of the given text file.
func GetFileContent(i Importer, URI string) (string, error) {
cnt := bytes.Buffer{}
if err := getFile(i, URI, bufio.NewWriter(io.Writer(&cnt))); err != nil {
if err := GetFile(i, URI, bufio.NewWriter(io.Writer(&cnt))); err != nil {
return "", err
} else {
// Ensure we read UTF-8 content.
@ -181,7 +181,7 @@ func ImportFile(i Importer, URI string, next func(string, string) (interface{},
} else {
defer fdto.Close()
writer := bufio.NewWriter(fdto)
if err := getFile(i, URI, writer); err != nil {
if err := GetFile(i, URI, writer); err != nil {
os.Remove(dest)
return nil, err
}

View File

@ -88,7 +88,7 @@ func (t *imageImporterTransformer) Transform(doc *ast.Document, reader text.Read
} else {
defer fdto.Close()
writer := bufio.NewWriter(fdto)
if err := getFile(t.importer, path.Join(t.rootDir, iPath), writer); err != nil {
if err := GetFile(t.importer, path.Join(t.rootDir, iPath), writer); err != nil {
os.Remove(dPath)
return ast.WalkStop, err
}

View File

@ -87,7 +87,7 @@ func resizePicture(importedPath string, rect image.Rectangle) error {
// getAuthors parses the AUTHORS file.
func getAuthors(i Importer, tname string) ([]string, error) {
if authors, err := getFileContent(i, path.Join(tname, "AUTHORS.txt")); err != nil {
if authors, err := GetFileContent(i, path.Join(tname, "AUTHORS.txt")); err != nil {
return nil, err
} else {
var ret []string
@ -112,7 +112,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
th.Path = tdir
// Extract theme's label
if tname, err := getFileContent(i, path.Join(tdir, "title.txt")); err == nil {
if tname, err := GetFileContent(i, path.Join(tdir, "title.txt")); err == nil {
th.Name = fixnbsp(tname)
} else if f := strings.Index(tdir, "-"); f >= 0 {
th.Name = fixnbsp(tdir[f+1:])
@ -129,7 +129,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
th.Authors = strings.Join(authors, ", ")
}
if intro, err := getFileContent(i, path.Join(tdir, "overview.txt")); err != nil {
if intro, err := GetFileContent(i, path.Join(tdir, "overview.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to get theme's overview: %s", th.Name, err))
} else {
// Split headline from intro
@ -168,7 +168,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
}
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, fmt.Sprintf("%q: unable to get partner's text: %s", th.Name, err))
} else {
th.PartnerText, err = ProcessMarkdown(i, txt, tdir)