From 4ec169afdcde366bd7e7126ca987220e19e5bea7 Mon Sep 17 00:00:00 2001 From: adnano Date: Sun, 11 Apr 2021 17:50:18 -0400 Subject: [PATCH] Implement static directory support --- config.go | 2 ++ main.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/config.go b/config.go index 71ecd3f..e6a168b 100644 --- a/config.go +++ b/config.go @@ -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 diff --git a/main.go b/main.go index 4cbeb1a..24fdda0 100644 --- a/main.go +++ b/main.go @@ -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 + }) +}