aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornytpu <alex@nytpu.com>2021-02-26 12:55:34 -0700
committernytpu <alex@nytpu.com>2021-02-26 12:55:34 -0700
commitafadfe82af738db57b1452da3c307a9c3a6ea7bf (patch)
tree7e15c13f387e5caa609e8436fced9d890827fa5b
parentfix gemini file writing (diff)
downloadcheetah-bot-afadfe82af738db57b1452da3c307a9c3a6ea7bf.tar.bz2
cheetah-bot-afadfe82af738db57b1452da3c307a9c3a6ea7bf.zip
improve line skipping when generating index
-rw-r--r--cheetah-bot.go26
1 files changed, 16 insertions, 10 deletions
diff --git a/cheetah-bot.go b/cheetah-bot.go
index d11d6a2..136e160 100644
--- a/cheetah-bot.go
+++ b/cheetah-bot.go
@@ -106,6 +106,7 @@ func main() {
linkGemini := fmt.Sprintf("=> D%03d.jpg %04d-%02d-%02d - Day %[1]d: %[5]s", day, curYear, curMonth, curDay, description)
// do the final culminating posts
+ // open old and new indexes
geminiOrig, err := os.Open("/var/gemini/nytpu.com/cheetahs/index.gmi")
if err != nil {
fmt.Println("Failed to open file: ", err)
@@ -118,29 +119,34 @@ func main() {
os.Exit(1)
}
defer geminiNew.Close()
+
+ // write header and new link to new index
io.WriteString(geminiNew, titleGemini)
io.WriteString(geminiNew, linkGemini)
+
+ // skip i lines in old file
reader := bufio.NewReader(geminiOrig)
- row1, err := reader.ReadSlice('\n')
- if err != nil {
- fmt.Println("Failed to get 1st line in file: ", err)
- os.Exit(1)
- }
- row2, err := reader.ReadSlice('\n')
- if err != nil {
- fmt.Println("Failed to get 2nd line in file: ", err)
- os.Exit(1)
+ total := 0
+ for i := 0; i < 9; i++ {
+ row, err := reader.ReadSlice('\n')
+ if err != nil {
+ fmt.Println("Failed to get skip line in file: ", err)
+ os.Exit(1)
+ }
+ total += len(row)
}
- _, err = geminiOrig.Seek(int64(len(row1)+len(row2)), io.SeekStart)
+ _, err = geminiOrig.Seek(int64(total), io.SeekStart)
if err != nil {
fmt.Println("Failed to seek in file: ", err)
os.Exit(1)
}
+ // copy the rest of old file
_, err = io.Copy(geminiNew, geminiOrig)
if err != nil {
fmt.Println("Failed to update gemini file: ", err)
os.Exit(1)
}
+ // move new to old once writing is confirmed to have been successful
err = os.Rename("/var/gemini/nytpu.com/cheetahs/index.gmi.new", "/var/gemini/nytpu.com/cheetahs/index.gmi")
if err != nil {
fmt.Println("Failed to move file: ", err)