-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
195 lines (171 loc) · 4.87 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
"time"
)
var repoPath = os.Getenv("REPO_PATH")
var templatePath = os.Getenv("TEMPLATE_PATH")
var inputDescription = os.Getenv("INPUT_DESCRIPTION")
var inputFooter = os.Getenv("INPUT_FOOTER")
var inputListMostRecent = os.Getenv("INPUT_LIST_MOST_RECENT")
var inputDateFormat = os.Getenv("INPUT_DATE_FORMAT")
var re = regexp.MustCompile(`^Date:\s*`)
var re2 = regexp.MustCompile(`^#\s*`)
type Til struct {
Title string
Filename string
Category string
DateAdded time.Time
}
// sort TILs by DateAdded (DESC) and return n most recent
func cmdTrimMostRecentTils(tils *[]Til, n int) {
if n <= 0 {
n = 0
}
if n > len(*tils) {
n = len(*tils)
}
sort.Slice(*tils, func(i, j int) bool {
first := (*tils)[i].DateAdded
second := (*tils)[j].DateAdded
return first.After(second)
})
*tils = (*tils)[0:n]
}
// run a git cli command, the capture and parse the output to extract the date
// a file was added to the repository
func cmdGetDate(file string) time.Time {
c1 := exec.Command("git", "log", "--diff-filter=A", "--date=rfc", "--", file)
c1.Dir = repoPath
var commandOutput bytes.Buffer
var commandErrorOutput bytes.Buffer
c1.Stdout = &commandOutput
c1.Stderr = &commandErrorOutput
err := c1.Start()
if err != nil {
fmt.Println("start error")
fmt.Println(commandErrorOutput.String())
fmt.Println(file)
fmt.Println(err)
return time.Time{}
}
err = c1.Wait()
if err != nil {
fmt.Println("finish error")
fmt.Println(commandErrorOutput.String())
fmt.Println(file)
fmt.Println(err)
return time.Time{}
}
date := time.Time{}
for _, outputLine := range strings.Split(commandOutput.String(), "\n") {
if re.MatchString(outputLine) {
// strip "Date: " substring from matching line
var strippedDate = re.ReplaceAllString(outputLine, "")
date, _ = time.Parse(time.RFC1123Z, strippedDate)
break
}
}
return date
}
func main() {
// map of all categories and respective TILs
tilsMap := make(map[string][]Til)
// list of all (non-grouped by category) TILs for use with `list_most_recent` feature
var tilsSlice []Til
// tils = TIL markdown files
// note tils sorted lexicographically which may differ from how
// they're presented in an IDE
tils, _ := filepath.Glob(repoPath + "/**/*.md")
for _, til := range tils {
// grab the "category" and the "file"
// ex: html/div-tags.md -- category "html" file "div-tags.md"
splitResult := strings.Split(til, "/")
length := len(splitResult)
// category := strings.ToLower(splitResult[length-2])
category := splitResult[length-2]
file := splitResult[length-1]
if strings.ToLower(file) == "readme.md" {
continue
}
// Read the first line of each file, use the string as a title
f, err := os.Open(til)
if err != nil {
log.Panic(err)
}
reader := bufio.NewReader(f)
linkTitle, err := reader.ReadString('\n')
if err != nil {
log.Println(fmt.Sprintf("ERROR: file \"%s\" does not have > 1 line of text (no title)", file))
log.Panic(err)
}
// strip "# " from beginning of line
linkTitle = re2.ReplaceAllString(linkTitle, "")
linkTitle = strings.TrimSpace(linkTitle)
// if category first encountered in loop so far, append new map key, otherwise
// add to existing
tilStruct := Til{
Title: linkTitle,
Filename: file,
Category: category,
DateAdded: cmdGetDate(category + "/" + file),
}
if _, exists := tilsMap[category]; exists {
tilsMap[category] = append(tilsMap[category], tilStruct)
} else {
tilsMap[category] = []Til{tilStruct}
}
tilsSlice = append(tilsSlice, tilStruct)
}
n, err := strconv.Atoi(inputListMostRecent)
if err != nil {
n = 0
}
cmdTrimMostRecentTils(&tilsSlice, n)
// load and execute template, write results to README.md
t, err := template.New(path.Base(templatePath)).Funcs(template.FuncMap{
"toLower": strings.ToLower,
}).ParseFiles(templatePath)
if err != nil {
log.Panic(err)
}
var output bytes.Buffer
err = t.Execute(&output, struct {
Tils map[string][]Til
AllTils []string
InputDescription string
InputFooter string
MostRecentTils []Til
InputDateFormat string
}{
Tils: tilsMap,
AllTils: tils,
InputDescription: inputDescription,
InputFooter: inputFooter,
MostRecentTils: tilsSlice,
InputDateFormat: inputDateFormat,
})
if err != nil {
log.Panic(err)
}
fmt.Println("------------------------------------------------------------")
fmt.Println("------------------------------------------------------------")
fmt.Print(output.String())
fmt.Println("------------------------------------------------------------")
fmt.Println("------------------------------------------------------------")
// truncates before writing
ioutil.WriteFile(repoPath+"/README.md", []byte(output.String()), 0644)
}