forked from tildetown/tilde.town
84 lines
1.2 KiB
Go
84 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
_ "embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"text/template"
|
|
)
|
|
|
|
const statsPath = "/usr/local/bin/stats"
|
|
|
|
//go:embed feed.tmpl.xml
|
|
var feedTmpl string
|
|
|
|
type newsEntry struct {
|
|
Title string // Title of entry
|
|
Pubdate string // Human readable date
|
|
Content string // HTML of entry
|
|
}
|
|
|
|
type tildeData struct {
|
|
News []newsEntry
|
|
}
|
|
|
|
func _main() error {
|
|
data, err := stats()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
type tmplData struct {
|
|
News []newsEntry
|
|
}
|
|
|
|
td := &tmplData{
|
|
News: data.News,
|
|
}
|
|
|
|
t, err := template.New("feed").Parse(feedTmpl)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse the feed template: %w", err)
|
|
}
|
|
|
|
out := bytes.Buffer{}
|
|
if err = t.Execute(&out, td); err != nil {
|
|
return fmt.Errorf("failed to render feed template: %w", err)
|
|
}
|
|
|
|
fmt.Println(out.String())
|
|
|
|
return nil
|
|
}
|
|
|
|
func stats() (*tildeData, error) {
|
|
sout := bytes.Buffer{}
|
|
cmd := exec.Command(statsPath)
|
|
cmd.Stdout = &sout
|
|
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var 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)
|
|
}
|
|
}
|