Allow specifying which task to run

This commit is contained in:
adnano 2021-04-11 16:00:29 -04:00
parent 70795af4c8
commit a7e8568b1e

49
main.go
View file

@ -1,26 +1,59 @@
package main package main
import ( import (
"flag"
"fmt"
"log" "log"
) )
func main() { func main() {
if err := run(); err != nil { 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()
// Load config
cfg, err := LoadConfig(config)
if err != nil {
log.Fatal(err)
}
if err := cfg.LoadTemplates("templates"); err != nil {
log.Fatal(err)
}
if err := run(cfg, task); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
func run() error { func run(cfg *Config, taskName string) error {
// Load config switch taskName {
cfg, err := LoadConfig("config.toml") case "all":
return runAll(cfg)
default:
task, ok := cfg.Tasks[taskName]
if !ok {
return fmt.Errorf("run task %q: no such task", taskName)
}
return runTask(cfg, task)
}
}
func runAll(cfg *Config) error {
for _, task := range cfg.Tasks {
err := runTask(cfg, task)
if err != nil { if err != nil {
return err return err
} }
if err := cfg.LoadTemplates("templates"); err != nil {
return err
} }
return nil
}
for _, task := range cfg.Tasks { func runTask(cfg *Config, task *Task) error {
// Load content // Load content
dir := NewDir("") dir := NewDir("")
dir.inputExt = task.Input dir.inputExt = task.Input
@ -38,7 +71,5 @@ func run() error {
if err := dir.write(task.Destination, task); err != nil { if err := dir.write(task.Destination, task); err != nil {
return err return err
} }
}
return nil return nil
} }