diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/postdate/main.go | 71 |
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) + } +} |