package main import ( "bytes" "encoding/json" "fmt" "os" "os/exec" "text/template" ) //const statsPath = "/home/vilmibm/bin/townstats" const statsPath = "/town/bin/stats" type newsEntry struct { Title string `json:"title"` // Title of entry Pubdate string `json:"pubdate"` // Human readable date Content string `json:"content"` // HTML of entry } type tildeData struct { News []newsEntry `json:"news"` // Collection of town news entries } func _main() error { data, err := stats() if err != nil { return err } hypertextDocument := getHeader() for _, entry := range data.News { entryHTML, err := renderEntry(entry) if err != nil { return err } hypertextDocument += entryHTML } hypertextDocument += getFooter() fmt.Println(hypertextDocument) return nil } func renderEntry(entry newsEntry) (string, error) { t, err := template.New("news").Parse(`

{{.Title}}

{{.Pubdate}} {{.Content}} `) if err != nil { return "", err } out := bytes.Buffer{} err = t.Execute(&out, entry) if err != nil { return "", err } return out.String(), nil } func getHeader() string { return ` web log of tilde town

a web log for tilde town or at least what passes for one

` } func getFooter() string { return ` ` } func stats() (*tildeData, error) { sout := bytes.Buffer{} cmd := exec.Command(statsPath) cmd.Stdout = &sout err := cmd.Run() if err != nil { return nil, err } data := tildeData{} err = json.Unmarshal(sout.Bytes(), &data) if err != nil { return nil, err } return &data, nil } func main() { err := _main() if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(1) } }