life-report/lifereport/report.go
Matt Baer 38a2c39509 Fix sections order and generate multiple reports
Outputs all reports as HTML in the same given folder.
2016-03-07 15:42:44 -05:00

69 lines
1.3 KiB
Go

package main
import (
"fmt"
"html/template"
"io/ioutil"
"os"
"path"
"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
}
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()
}
}