-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection_list.go
49 lines (39 loc) · 881 Bytes
/
section_list.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
package main
import (
"bytes"
"fmt"
"os"
"sort"
"time"
)
type Section struct {
List []SectionEntry
File string
}
type SectionEntry struct {
Link string
Title string
Date time.Time
Summary string
}
func (section *Section) Write(file string) error {
// sort section list
sort.Slice(section.List, func(i, j int) bool {
return section.List[i].Date.After(section.List[j].Date)
})
var buf bytes.Buffer
for _, file := range section.List {
// TODO: this could be a template
entry := "\n"
entry += fmt.Sprintf("=> %s %v: %s\n", file.Link, file.Date.Format("2006-01-02"), file.Title)
entry += fmt.Sprintf("%s\n", file.Summary)
buf.Write([]byte(entry))
}
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("open file: %w", err)
}
_, err = f.Write(buf.Bytes())
f.Close()
return err
}