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.
This commit is contained in:
parent
568202efd1
commit
d97f145c89
|
@ -18,5 +18,6 @@
|
||||||
/vendor/
|
/vendor/
|
||||||
/Godeps/
|
/Godeps/
|
||||||
|
|
||||||
# Sitemap file
|
# Sitemap files
|
||||||
|
previous-sitemap.md
|
||||||
sitemap.md
|
sitemap.md
|
||||||
|
|
34
main.go
34
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -18,6 +19,13 @@ type wikiPage struct {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Infof("Tildes Wiki Sitemap\n")
|
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
|
// Create a groups array, which will be used to sort the output
|
||||||
// (couldn't figure out how to sort a map by keys)
|
// (couldn't figure out how to sort a map by keys)
|
||||||
groups := make([]string, 0)
|
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
|
// 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))
|
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)
|
log.Printf("Done! Found %v groups and %v pages, see sitemap.md for the output", len(groups), pagesTotal)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue