mirror of https://github.com/zschaffer/jenga.git
refactor: seperate input data processing into own functino
This commit is contained in:
parent
8b425d4aca
commit
d96099e3fb
31
builder.go
31
builder.go
|
@ -34,10 +34,25 @@ func readFile(filePath string) (template.HTML, error) {
|
|||
|
||||
// build reads all the files in the inputFilePaths slice, then passes them to writeOutputFile to build an index.html
|
||||
func (b *builder) build() error {
|
||||
fmt.Println()
|
||||
inputData, err := getInputData(b.inputFilePaths)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get input data: %w", err)
|
||||
}
|
||||
|
||||
if err := writeOutputFile(inputData, b.outputDirPath, b.template); err != nil {
|
||||
return fmt.Errorf("failed to write to output file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getInputData(inputFilePaths []string) ([]template.HTML, error) {
|
||||
fmt.Println("\033[0;34m[1/2]\033[0m converting source files to HTML")
|
||||
var inputData []template.HTML
|
||||
temporaryMap := make(map[string]template.HTML)
|
||||
g := new(errgroup.Group)
|
||||
for _, inputFilePath := range b.inputFilePaths {
|
||||
|
||||
for _, inputFilePath := range inputFilePaths {
|
||||
path := inputFilePath
|
||||
g.Go(func() error {
|
||||
data, err := readFile(path)
|
||||
|
@ -49,18 +64,14 @@ func (b *builder) build() error {
|
|||
}
|
||||
|
||||
if err := g.Wait(); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
} else {
|
||||
for _, inputFilePath := range b.inputFilePaths {
|
||||
for _, inputFilePath := range inputFilePaths {
|
||||
inputData = append([]template.HTML{temporaryMap[inputFilePath]}, inputData...)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if err := writeOutputFile(inputData, b.outputDirPath, b.template); err != nil {
|
||||
return fmt.Errorf("failed to write to output file: %w", err)
|
||||
}
|
||||
return nil
|
||||
return inputData, nil
|
||||
}
|
||||
|
||||
// writeOutputFile creates an index.html file at outputDirPath using a template filled with inputData
|
||||
|
@ -71,13 +82,9 @@ func writeOutputFile(inputData []template.HTML, outputDirPath string, t *templat
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to create file %+s: %v", filePath, err)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
writer := bufio.NewWriter(file)
|
||||
|
||||
defer writer.Flush()
|
||||
|
||||
if err := t.Execute(writer, inputData); err != nil {
|
||||
return fmt.Errorf("error executing template: %w", err)
|
||||
}
|
||||
|
|
7
go.mod
7
go.mod
|
@ -4,12 +4,9 @@ go 1.18
|
|||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.2.0
|
||||
github.com/fsnotify/fsnotify v1.5.4
|
||||
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53
|
||||
github.com/rjeczalik/notify v0.9.2
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/rjeczalik/notify v0.9.2
|
||||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
|
||||
)
|
||||
require golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect
|
||||
|
|
6
go.sum
6
go.sum
|
@ -1,7 +1,5 @@
|
|||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
|
||||
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
|
||||
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53 h1:JguE3sS3yLzLiCTCnsmzVFuTvTMDJALbzCgurwY5G/0=
|
||||
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
|
||||
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=
|
||||
|
@ -9,5 +7,5 @@ github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa
|
|||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
|
||||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 h1:v1W7bwXHsnLLloWYTVEdvGvA7BHMeBYsPcF0GLDxIRs=
|
||||
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
|
|
Loading…
Reference in New Issue