|
- package main
-
- import (
- "fmt"
- "html/template"
- "io/ioutil"
- "os"
- "path"
- "sort"
- "strings"
-
- report "github.com/thebaer/life-report"
- )
-
- func main() {
- if len(os.Args) < 2 {
- fmt.Print(`usage: lifereport [directory]
- directory: path to a folder of reports
- `)
- return
- }
-
- dir := os.Args[1]
- files, err := ioutil.ReadDir(dir)
- if err != nil {
- fmt.Print("Unable to read directory: %v\n", err)
- return
- }
-
- t, err := template.ParseFiles("../report.tmpl")
- if err != nil {
- fmt.Printf("Unable to parse template: %v\n", err)
- return
- }
-
- var reports []report.Report
-
- for _, file := range files {
- if file.Name() == "template.json" || !strings.HasSuffix(file.Name(), ".json") {
- continue
- }
-
- f, err := os.Open(path.Join(dir, file.Name()))
- if err != nil {
- fmt.Fprintf(os.Stdout, "Unable to open file: %v\n", err)
- continue
- }
- if strings.HasPrefix(file.Name(), ".") {
- f.Close()
- continue
- }
-
- r, err := report.ParseReport(f)
- if err != nil {
- f.Close()
- fmt.Fprintf(os.Stdout, "Unable to parse report %s: %v\n", file.Name(), err)
- continue
- }
- f.Close()
-
- outName := strings.Replace(file.Name(), ".json", ".html", 1)
- o, err := os.Create(path.Join(dir, outName))
- if err != nil {
- fmt.Fprintf(os.Stdout, "Unable to write report %s: %v\n", outName, err)
- continue
- }
-
- t.Execute(o, r)
- o.Close()
-
- r.File = outName
- reports = append(reports, *r)
- }
-
- // Create index file
- i, err := os.Create(path.Join(dir, "index.html"))
- if err != nil {
- fmt.Fprintf(os.Stdout, "Unable to write index: %v\n", err)
- return
- }
- it, err := template.ParseFiles("../index.tmpl")
- if err != nil {
- fmt.Printf("Unable to parse index template: %v\n", err)
- return
- }
- sort.Sort(sort.Reverse(report.ByNum(reports)))
- it.Execute(i, reports)
- i.Close()
- }
|