Implement static directory support

This commit is contained in:
adnano 2021-04-11 17:50:18 -04:00
parent 5719af8727
commit 4ec169afdc
2 changed files with 41 additions and 0 deletions

View file

@ -27,6 +27,7 @@ type Task struct {
Output string `toml:"output"` // output file extension
Template string `toml:"template"` // template file extension
PostProcess string `toml:"postprocess"` // postprocess directive
Static string `toml:"static"` // static file directory
Destination string `toml:"destination"` // destination directory
}
@ -58,6 +59,7 @@ func DefaultConfig() *Config {
Input: ".gmi",
Output: ".gmi",
Template: ".gmi",
Static: "static",
Destination: "public",
}
return c

39
main.go
View file

@ -3,7 +3,12 @@ package main
import (
"flag"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
@ -71,5 +76,39 @@ func runTask(cfg *Config, task *Task) error {
if err := dir.write(task.Destination, task); err != nil {
return err
}
// Copy static files
if task.Static != "" {
err := copyAll(task.Static, task.Destination)
if err != nil {
return err
}
}
return nil
}
func copyAll(srcDir, dstDir string) error {
return filepath.Walk(srcDir, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
// Do nothing
return nil
}
src, err := os.Open(path)
if err != nil {
return err
}
defer src.Close()
dstPath := filepath.Join(dstDir, strings.TrimPrefix(path, srcDir))
dst, err := os.Create(dstPath)
if err != nil {
return err
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
return err
}
return nil
})
}