2020-09-22 20:42:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-11 20:00:29 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-04-11 21:50:18 +00:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2020-09-22 20:42:14 +00:00
|
|
|
"log"
|
2021-04-11 21:50:18 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-09-28 23:54:48 +00:00
|
|
|
)
|
|
|
|
|
2020-09-22 20:42:14 +00:00
|
|
|
func main() {
|
2021-04-11 20:00:29 +00:00
|
|
|
var (
|
|
|
|
task string
|
|
|
|
config string
|
|
|
|
)
|
|
|
|
|
|
|
|
flag.StringVar(&task, "t", "all", "the task to run")
|
|
|
|
flag.StringVar(&config, "c", "config.toml", "the configuration file to use")
|
|
|
|
flag.Parse()
|
2020-09-22 20:42:14 +00:00
|
|
|
|
2020-11-20 17:07:38 +00:00
|
|
|
// Load config
|
2021-04-11 20:00:29 +00:00
|
|
|
cfg, err := LoadConfig(config)
|
2021-03-20 06:02:36 +00:00
|
|
|
if err != nil {
|
2021-04-11 20:00:29 +00:00
|
|
|
log.Fatal(err)
|
2020-11-20 17:07:38 +00:00
|
|
|
}
|
2021-03-20 06:18:15 +00:00
|
|
|
if err := cfg.LoadTemplates("templates"); err != nil {
|
2021-04-11 20:00:29 +00:00
|
|
|
log.Fatal(err)
|
2020-10-01 02:14:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 20:00:29 +00:00
|
|
|
if err := run(cfg, task); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(cfg *Config, taskName string) error {
|
|
|
|
switch taskName {
|
|
|
|
case "all":
|
|
|
|
return runAll(cfg)
|
|
|
|
default:
|
|
|
|
task, ok := cfg.Tasks[taskName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("run task %q: no such task", taskName)
|
2021-03-21 03:17:58 +00:00
|
|
|
}
|
2021-04-11 20:00:29 +00:00
|
|
|
return runTask(cfg, task)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAll(cfg *Config) error {
|
|
|
|
for _, task := range cfg.Tasks {
|
|
|
|
err := runTask(cfg, task)
|
|
|
|
if err != nil {
|
2021-03-21 03:17:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-28 02:53:16 +00:00
|
|
|
}
|
2021-04-11 20:00:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-28 02:53:16 +00:00
|
|
|
|
2021-04-11 20:00:29 +00:00
|
|
|
func runTask(cfg *Config, task *Task) error {
|
|
|
|
// Load content
|
|
|
|
dir := NewDir("")
|
2021-04-11 22:42:55 +00:00
|
|
|
dir.inputExt = task.InputExt
|
|
|
|
dir.outputExt = task.OutputExt
|
|
|
|
dir.templateExt = task.TemplateExt
|
2021-04-11 20:00:29 +00:00
|
|
|
if err := dir.read("content", ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dir.sort()
|
|
|
|
// Manipulate content
|
|
|
|
if err := dir.manipulate(cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Write content
|
2021-04-11 22:42:55 +00:00
|
|
|
if err := dir.write(task.OutputDir, task); err != nil {
|
2021-04-11 20:00:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-11 21:50:18 +00:00
|
|
|
// Copy static files
|
2021-04-11 22:42:55 +00:00
|
|
|
if task.StaticDir != "" {
|
|
|
|
err := copyAll(task.StaticDir, task.OutputDir)
|
2021-04-11 21:50:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 20:42:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-11 21:50:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|