about summary refs log tree commit diff stats
path: root/cmd
diff options
context:
space:
mode:
authorAlan Pearce2024-06-28 20:43:14 +0200
committerAlan Pearce2024-06-29 16:50:51 +0200
commit6c4a3268bc4c528ecff45f50ed5ca6aa1d48500c (patch)
tree6663946e586d5c0745b984a0a809fc2330ce70d0 /cmd
parentb4095d108a2646bcf9e7fff64788b10d9bce8da3 (diff)
downloadwebsite-6c4a3268bc4c528ecff45f50ed5ca6aa1d48500c.tar.lz
website-6c4a3268bc4c528ecff45f50ed5ca6aa1d48500c.tar.zst
website-6c4a3268bc4c528ecff45f50ed5ca6aa1d48500c.zip
Diffstat (limited to 'cmd')
-rw-r--r--cmd/postdate/main.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/cmd/postdate/main.go b/cmd/postdate/main.go
new file mode 100644
index 0000000..2f340cf
--- /dev/null
+++ b/cmd/postdate/main.go
@@ -0,0 +1,71 @@
+package main
+
+import (
+	"encoding/json"
+	"os"
+	"path/filepath"
+	"sync"
+	"website/internal/builder"
+	"website/internal/content"
+	"website/internal/log"
+	"website/internal/vcs"
+
+	"github.com/go-git/go-git/v5/plumbing/object"
+)
+
+func getPostVCSDates(iter object.CommitIter) *content.PostVCSDates {
+	p := &content.PostVCSDates{}
+	defer iter.Close()
+	for c, err := iter.Next(); c != nil && err == nil; c, err = iter.Next() {
+		if p.LastUpdated.IsZero() {
+			p.LastUpdated = c.Committer.When
+		}
+
+		p.Created = c.Committer.When
+	}
+
+	return p
+}
+
+func main() {
+	posts, err := content.GetPostFiles(filepath.Join(builder.ContentRoot, "post"))
+	if err != nil {
+		log.Fatal("failed to glob posts", "error", err)
+	}
+
+	var (
+		wg sync.WaitGroup
+		mu sync.Mutex
+	)
+	out := make(map[string]*content.PostVCSDates)
+	for _, f := range posts {
+		wg.Add(1)
+		go func(f string) {
+			repo, err := vcs.Open(".")
+			if err != nil {
+				log.Error("failed to open repository", "error", err)
+			}
+
+			defer wg.Done()
+			iter, err := repo.FileLog(f)
+			if err != nil {
+				log.Error("failed to get commit log for file", "filename", f)
+			}
+			dates := getPostVCSDates(iter)
+			mu.Lock()
+			out[f] = dates
+			mu.Unlock()
+		}(f)
+	}
+
+	wg.Wait()
+	j, err := json.MarshalIndent(out, "", "  ")
+	if err != nil {
+		log.Fatal("failed to convert to JSON", "error", err)
+	}
+
+	err = os.WriteFile(filepath.Join(builder.ContentRoot, content.DatesFilename), j, 0644)
+	if err != nil {
+		log.Fatal("could not write output file", "error", err)
+	}
+}