-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.go
100 lines (88 loc) · 1.84 KB
/
sitemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"os/exec"
"sync"
"time"
. "github.com/oetherington/smetana"
)
func newSitemapLocation(
baseUrl string,
pageUrl string,
srcFile string,
) (SitemapLocation, error) {
args := []string{
"log",
"-1",
"--pretty=format:%aI",
"--follow",
"--",
srcFile,
}
result := exec.Command("git", args...)
stdout, err := result.Output()
if err != nil {
return SitemapLocation{}, err
}
url := fmt.Sprintf("%s%s", baseUrl, pageUrl)
modified, err := time.Parse(time.RFC3339, string(stdout))
if err != nil {
return SitemapLocation{}, err
}
return SitemapLocationMod(url, modified), nil
}
type StaticRoute struct {
url string
srcFile string
}
func countLocations(staticRoutes []StaticRoute, articles []ArticleInfo) int {
count := len(staticRoutes)
for _, article := range articles {
if article.Published {
count++
}
}
return count
}
func getSitemap(
baseUrl string,
staticRoutes []StaticRoute,
articles []ArticleInfo,
) (Sitemap, error) {
count := countLocations(staticRoutes, articles)
locations := make([]SitemapLocation, count)
errors := make([]error, count)
var wg sync.WaitGroup
for i, route := range staticRoutes {
wg.Add(1)
go func(i int, route StaticRoute) {
loc, err := newSitemapLocation(baseUrl, route.url, route.srcFile)
locations[i] = loc
errors[i] = err
wg.Done()
}(i, route)
}
var i = 0
for _, article := range articles {
if !article.Published {
continue
}
wg.Add(1)
go func(i int, article ArticleInfo) {
filePath := fmt.Sprintf("./articles/%s.md", article.Path)
loc, err := newSitemapLocation(baseUrl, article.Path, filePath)
index := len(staticRoutes) + i
locations[index] = loc
errors[index] = err
wg.Done()
}(i, article)
i += 1
}
wg.Wait()
for _, err := range errors {
if err != nil {
return nil, err
}
}
return locations, nil
}