Generate a report on various aspects of your life.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.7 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "sort"
  9. "strings"
  10. report "github.com/thebaer/life-report"
  11. )
  12. func main() {
  13. if len(os.Args) < 2 {
  14. fmt.Print(`usage: lifereport [directory]
  15. directory: path to a folder of reports
  16. `)
  17. return
  18. }
  19. dir := os.Args[1]
  20. files, err := ioutil.ReadDir(dir)
  21. if err != nil {
  22. fmt.Print("Unable to read directory: %v\n", err)
  23. return
  24. }
  25. t, err := template.ParseFiles("../report.tmpl")
  26. if err != nil {
  27. fmt.Printf("Unable to parse template: %v\n", err)
  28. return
  29. }
  30. var reports []report.Report
  31. for _, file := range files {
  32. if file.Name() == "template.json" || !strings.HasSuffix(file.Name(), ".json") {
  33. continue
  34. }
  35. f, err := os.Open(path.Join(dir, file.Name()))
  36. if err != nil {
  37. fmt.Fprintf(os.Stdout, "Unable to open file: %v\n", err)
  38. continue
  39. }
  40. if strings.HasPrefix(file.Name(), ".") {
  41. f.Close()
  42. continue
  43. }
  44. r, err := report.ParseReport(f)
  45. if err != nil {
  46. f.Close()
  47. fmt.Fprintf(os.Stdout, "Unable to parse report %s: %v\n", file.Name(), err)
  48. continue
  49. }
  50. f.Close()
  51. outName := strings.Replace(file.Name(), ".json", ".html", 1)
  52. o, err := os.Create(path.Join(dir, outName))
  53. if err != nil {
  54. fmt.Fprintf(os.Stdout, "Unable to write report %s: %v\n", outName, err)
  55. continue
  56. }
  57. t.Execute(o, r)
  58. o.Close()
  59. r.File = outName
  60. reports = append(reports, *r)
  61. }
  62. // Create index file
  63. i, err := os.Create(path.Join(dir, "index.html"))
  64. if err != nil {
  65. fmt.Fprintf(os.Stdout, "Unable to write index: %v\n", err)
  66. return
  67. }
  68. it, err := template.ParseFiles("../index.tmpl")
  69. if err != nil {
  70. fmt.Printf("Unable to parse index template: %v\n", err)
  71. return
  72. }
  73. sort.Sort(sort.Reverse(report.ByNum(reports)))
  74. it.Execute(i, reports)
  75. i.Close()
  76. }