sync: Expose GetFile and GetFileContent functions
This commit is contained in:
parent
d24b1c5d4d
commit
560110ba5e
@ -75,7 +75,7 @@ type ExerciceParams struct {
|
|||||||
// parseExerciceParams reads challenge definitions from defines.txt and extract usefull data to set up the challenge.
|
// 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) {
|
func parseExerciceParams(i Importer, exPath string) (p ExerciceParams, md toml.MetaData, err error) {
|
||||||
var defs string
|
var defs string
|
||||||
defs, err = getFileContent(i, path.Join(exPath, "challenge.txt"))
|
defs, err = GetFileContent(i, path.Join(exPath, "challenge.txt"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse DIGESTS.txt
|
// 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))
|
errs = append(errs, fmt.Sprintf("%q: unable to read DIGESTS.txt: %s", path.Base(exercice.Path), err))
|
||||||
} else {
|
} else {
|
||||||
digests = map[string][]byte{}
|
digests = map[string][]byte{}
|
||||||
@ -104,7 +104,7 @@ func CheckExerciceFiles(i Importer, exercice *fic.Exercice) (files []string, err
|
|||||||
for _, fname := range flist {
|
for _, fname := range flist {
|
||||||
w, hash160, hash512 := fic.CreateHashBuffers()
|
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))
|
errs = append(errs, fmt.Sprintf("%q: unable to read file %q: %s", path.Base(exercice.Path), fname, err))
|
||||||
continue
|
continue
|
||||||
} else if _, err := fic.CheckBufferHash(hash160, hash512, digests[fname]); err != nil {
|
} else if _, err := fic.CheckBufferHash(hash160, hash512, digests[fname]); err != nil {
|
||||||
|
@ -107,7 +107,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Overwrite title if title.txt exists
|
// 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)
|
myTitle = strings.TrimSpace(myTitle)
|
||||||
if strings.Contains(myTitle, "\n") {
|
if strings.Contains(myTitle, "\n") {
|
||||||
errs = append(errs, fmt.Sprintf("%q: title.txt: Title can't contain new lines", edir))
|
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
|
// 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")
|
||||||
}
|
}
|
||||||
@ -146,9 +146,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")
|
||||||
}
|
}
|
||||||
@ -161,7 +161,7 @@ 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"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, fmt.Sprintf("%q: finished.txt: %s", edir, err))
|
errs = append(errs, fmt.Sprintf("%q: finished.txt: %s", edir, err))
|
||||||
} else {
|
} 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()))
|
errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error()))
|
||||||
} else if size == 0 {
|
} else if size == 0 {
|
||||||
errs = append(errs, fmt.Sprintf("%q: resolution.md: The file is empty!", edir))
|
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()))
|
errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error()))
|
||||||
} else if e.Resolution, err = ProcessMarkdown(i, e.Resolution, epath); err != nil {
|
} 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()))
|
errs = append(errs, fmt.Sprintf("%q: resolution.md: error during markdown processing: %s", edir, err.Error()))
|
||||||
|
@ -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)
|
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.
|
// GetFile helps to manage huge file transfert by concatenating splitted (with split(1)) files.
|
||||||
func getFile(i Importer, URI string, writer *bufio.Writer) error {
|
func GetFile(i Importer, URI string, writer *bufio.Writer) error {
|
||||||
// Import file if it exists
|
// Import file if it exists
|
||||||
if i.exists(URI) {
|
if i.exists(URI) {
|
||||||
return i.getFile(URI, writer)
|
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)
|
return fmt.Errorf("%q: no such file or directory", URI)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getFileContent retrieves the content of the given text file.
|
// GetFileContent retrieves the content of the given text file.
|
||||||
func getFileContent(i Importer, URI string) (string, error) {
|
func GetFileContent(i Importer, URI string) (string, error) {
|
||||||
cnt := bytes.Buffer{}
|
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
|
return "", err
|
||||||
} else {
|
} else {
|
||||||
// Ensure we read UTF-8 content.
|
// Ensure we read UTF-8 content.
|
||||||
@ -181,7 +181,7 @@ func ImportFile(i Importer, URI string, next func(string, string) (interface{},
|
|||||||
} else {
|
} else {
|
||||||
defer fdto.Close()
|
defer fdto.Close()
|
||||||
writer := bufio.NewWriter(fdto)
|
writer := bufio.NewWriter(fdto)
|
||||||
if err := getFile(i, URI, writer); err != nil {
|
if err := GetFile(i, URI, writer); err != nil {
|
||||||
os.Remove(dest)
|
os.Remove(dest)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ func (t *imageImporterTransformer) Transform(doc *ast.Document, reader text.Read
|
|||||||
} else {
|
} else {
|
||||||
defer fdto.Close()
|
defer fdto.Close()
|
||||||
writer := bufio.NewWriter(fdto)
|
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)
|
os.Remove(dPath)
|
||||||
return ast.WalkStop, err
|
return ast.WalkStop, err
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ func resizePicture(importedPath string, rect image.Rectangle) error {
|
|||||||
|
|
||||||
// getAuthors parses the AUTHORS file.
|
// getAuthors parses the AUTHORS file.
|
||||||
func getAuthors(i Importer, tname string) ([]string, error) {
|
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
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
var ret []string
|
var ret []string
|
||||||
@ -112,7 +112,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
|||||||
th.Path = tdir
|
th.Path = tdir
|
||||||
|
|
||||||
// Extract theme's label
|
// 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)
|
th.Name = fixnbsp(tname)
|
||||||
} else if f := strings.Index(tdir, "-"); f >= 0 {
|
} else if f := strings.Index(tdir, "-"); f >= 0 {
|
||||||
th.Name = fixnbsp(tdir[f+1:])
|
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, ", ")
|
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))
|
errs = append(errs, fmt.Sprintf("%q: unable to get theme's overview: %s", th.Name, err))
|
||||||
} else {
|
} else {
|
||||||
// Split headline from intro
|
// 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 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))
|
errs = append(errs, fmt.Sprintf("%q: unable to get partner's text: %s", th.Name, err))
|
||||||
} else {
|
} else {
|
||||||
th.PartnerText, err = ProcessMarkdown(i, txt, tdir)
|
th.PartnerText, err = ProcessMarkdown(i, txt, tdir)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user