csb-json.go
Raw
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"github.com/docopt/docopt-go"
)
func buildHtml(templatefile string, additionaldirs []string) error {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
var d interface{}
err = json.Unmarshal(data, &d)
if err != nil {
return err
}
templatename := filepath.Base(templatefile)
t, err := template.ParseFiles(templatefile)
if err != nil {
return err
}
for _, d := range additionaldirs {
_, err = t.ParseGlob(d + "/*")
if err != nil {
return err
}
}
return t.ExecuteTemplate(os.Stdout, templatename, d)
}
func main() {
usage := fmt.Sprintf(`Builds html from json data
Usage:
%[1]s --template=<template> [--include=<dir>]...
`, filepath.Base(os.Args[0]))
arguments, err := docopt.Parse(usage, nil, true, "0.1.0", false)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
err = buildHtml(arguments["--template"].(string),
arguments["--include"].([]string))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
os.Exit(0)
}