2021-07-11 12:03:59 +03:00
|
|
|
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.Before(section.List[j].Date)
|
|
|
|
})
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2021-07-11 12:34:52 +03:00
|
|
|
|
2021-07-11 12:03:59 +03:00
|
|
|
for _, file := range section.List {
|
|
|
|
// TODO: this could be a template
|
|
|
|
entry := "\n"
|
|
|
|
entry += fmt.Sprintf("=> %s %s\n", file.Link, file.Title)
|
|
|
|
entry += fmt.Sprintf("%v - %s\n", file.Date.Format("2006-01-02"), file.Summary)
|
|
|
|
|
|
|
|
buf.Write([]byte(entry))
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
2021-07-11 12:34:52 +03:00
|
|
|
return fmt.Errorf("open file: %w", err)
|
2021-07-11 12:03:59 +03:00
|
|
|
}
|
2021-07-11 12:34:52 +03:00
|
|
|
|
2021-07-11 12:03:59 +03:00
|
|
|
_, err = f.Write(buf.Bytes())
|
|
|
|
f.Close()
|
2021-07-11 12:34:52 +03:00
|
|
|
|
2021-07-11 12:03:59 +03:00
|
|
|
return err
|
|
|
|
}
|