From d97f145c89f3c3eee5394fbe71239fd582151f97 Mon Sep 17 00:00:00 2001 From: Bauke Date: Thu, 10 Oct 2019 22:41:09 +0200 Subject: [PATCH] feat: add a simple way of detecting if the sitemap needs updating because there's no easy way to get the markdown source of anything without logging in and pressing the edit button this just adds a check if the saved sitemap from a previous run is the same as the new one that was generated, and logs an appropriate message. --- .gitignore | 3 ++- main.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4f2489d..e97481a 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,6 @@ /vendor/ /Godeps/ -# Sitemap file +# Sitemap files +previous-sitemap.md sitemap.md diff --git a/main.go b/main.go index 5e84837..de14219 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io/ioutil" "os" "sort" "strings" @@ -18,6 +19,13 @@ type wikiPage struct { func main() { log.Infof("Tildes Wiki Sitemap\n") + // Create a variable we'll use to check if a current sitemap already exists + _, sitemapExists := os.Stat("sitemap.md") + if sitemapExists == nil { + // If it does exist, rename it to "previous" + os.Rename("sitemap.md", "previous-sitemap.md") + } + // Create a groups array, which will be used to sort the output // (couldn't figure out how to sort a map by keys) groups := make([]string, 0) @@ -106,5 +114,31 @@ func main() { // And finally write how many groups and pages there are, like `tree` writes directories and files file.WriteString(fmt.Sprintf("\n%v groups, %v pages\n", len(groups), pagesTotal)) + + // If the sitemap exists we want to read both sitemaps and check if they're the same + if sitemapExists == nil { + file, err := os.Open("sitemap.md") + if err != nil { + log.Fatal(err) + } + + current, _ := ioutil.ReadAll(file) + file.Close() + file, err = os.Open("previous-sitemap.md") + if err != nil { + log.Fatal(err) + } + + previous, _ := ioutil.ReadAll(file) + file.Close() + + // If they're the same just log that all is good, if not warn that we need to update + if string(current) == string(previous) { + log.Infof("Current and previous sitemaps are the same, no need to update.\n") + } else { + log.Warnf("Current and previous sitemaps are not the same, you should update it.\n") + } + } + log.Printf("Done! Found %v groups and %v pages, see sitemap.md for the output", len(groups), pagesTotal) }