From a7e8568b1ebc904ad9505409e9433e8ceebb9c42 Mon Sep 17 00:00:00 2001 From: adnano Date: Sun, 11 Apr 2021 16:00:29 -0400 Subject: [PATCH] Allow specifying which task to run --- main.go | 81 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index bed9bc3..4cbeb1a 100644 --- a/main.go +++ b/main.go @@ -1,44 +1,75 @@ package main import ( + "flag" + "fmt" "log" ) 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) } } -func run() error { - // Load config - cfg, err := LoadConfig("config.toml") - if err != nil { - return err - } - if err := cfg.LoadTemplates("templates"); err != nil { - return 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) + } + return runTask(cfg, task) } +} +func runAll(cfg *Config) error { for _, task := range cfg.Tasks { - // Load content - dir := NewDir("") - dir.inputExt = task.Input - dir.outputExt = task.Output - dir.templateExt = task.Template - if err := dir.read("content", ""); err != nil { - return err - } - dir.sort() - // Manipulate content - if err := dir.manipulate(cfg); err != nil { - return err - } - // Write content - if err := dir.write(task.Destination, task); err != nil { + err := runTask(cfg, task) + if err != nil { return err } } - + return nil +} + +func runTask(cfg *Config, task *Task) error { + // Load content + dir := NewDir("") + dir.inputExt = task.Input + dir.outputExt = task.Output + dir.templateExt = task.Template + if err := dir.read("content", ""); err != nil { + return err + } + dir.sort() + // Manipulate content + if err := dir.manipulate(cfg); err != nil { + return err + } + // Write content + if err := dir.write(task.Destination, task); err != nil { + return err + } return nil }