main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/cdelorme/go-log"
  6. "github.com/cdelorme/go-maps"
  7. "github.com/cdelorme/go-option"
  8. "github.com/cdelorme/staticmd"
  9. )
  10. var exit = os.Exit
  11. var getwd = os.Getwd
  12. type generator interface {
  13. Generate() error
  14. }
  15. type logger interface {
  16. Error(string, ...interface{})
  17. }
  18. func configure() (generator, logger) {
  19. // get current directory
  20. cwd, _ := getwd()
  21. // prepare staticmd with dependencies & defaults
  22. smd := &staticmd.Generator{
  23. Logger: &log.Logger{},
  24. Input: cwd,
  25. Output: filepath.Join(cwd, "public/"),
  26. }
  27. // prepare cli options
  28. appOptions := option.App{Description: "command line tool for generating deliverable static content"}
  29. appOptions.Flag("template", "path to the template file", "--template", "-t")
  30. appOptions.Flag("input", "path to the markdown files", "--input", "-i")
  31. appOptions.Flag("output", "path to place generated content", "--output", "-o")
  32. appOptions.Flag("book", "combine all content into a single file", "--book", "-b")
  33. appOptions.Flag("relative", "use relative paths instead of absolute paths", "--relative", "-r")
  34. appOptions.Example("-t template.tmpl -i . -b")
  35. appOptions.Example("-t template.tmpl -i src/ -o out/ -r")
  36. flags := appOptions.Parse()
  37. // apply flags
  38. smd.TemplateFile, _ = maps.String(flags, smd.TemplateFile, "template")
  39. smd.Input, _ = maps.String(flags, smd.Input, "input")
  40. smd.Output, _ = maps.String(flags, smd.Output, "output")
  41. smd.Book, _ = maps.Bool(flags, smd.Book, "book")
  42. smd.Relative, _ = maps.Bool(flags, smd.Relative, "relative")
  43. return smd, smd.Logger
  44. }
  45. func main() {
  46. smd, l := configure()
  47. if err := smd.Generate(); err != nil {
  48. l.Error("generator failed (%s)", err)
  49. exit(1)
  50. }
  51. }