From 7db4b81ce3a0932897c10e8d17744bf282454a74 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Tue, 18 Jul 2023 21:21:16 +0200 Subject: [PATCH] Upgrade codebase to go 1.20 fix some outdated stuff * New go version 1.20 * Reformat code * Clean up regex * Remove deprecated function calls (ioutils) * Handle all errors * Fix typos Signed-off-by: Aaron Fischer --- go.mod | 4 +--- go.sum | 2 -- src/bdm.go | 43 ++++++++++++++++++++++++++++--------------- templates/book.html | 2 +- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 66f8a4b..cdc379f 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module okoyono.de/buchdesmonats -go 1.16 - -require github.com/a-h/gemini v0.0.61 +go 1.20 diff --git a/go.sum b/go.sum index 8d42340..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +0,0 @@ -github.com/a-h/gemini v0.0.61 h1:WSV9f5T6Vut9gIATUhqAh9KfsA4/kqcUIbTiDbsn1JY= -github.com/a-h/gemini v0.0.61/go.mod h1:p9wvIRDc2s3Lnbkw7CgNzDNgJHPuXDwh3dOF7w0NT8s= diff --git a/src/bdm.go b/src/bdm.go index 78eb4ce..f1c3c7e 100644 --- a/src/bdm.go +++ b/src/bdm.go @@ -2,15 +2,13 @@ package main import ( "fmt" - //"html/template" - xmltpl "text/template" "io" - "io/ioutil" "log" "net/http" "os" "regexp" "strings" + xmltpl "text/template" ) type Item struct { @@ -18,7 +16,7 @@ type Item struct { Author string Title string Filename string - Date string + Date string } func (i Item) ImageURL() string { @@ -41,14 +39,24 @@ func (i Item) downloadCover() error { return err } - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Fatal("Can not close the response body") + } + }(resp.Body) out, err := os.Create(i.targetFilename()) if err != nil { return err } - defer out.Close() + defer func(out *os.File) { + err := out.Close() + if err != nil { + log.Fatal("Can not close the file") + } + }(out) _, err = io.Copy(out, resp.Body) @@ -65,16 +73,21 @@ func getItems(filename string) []Item { log.Fatal(filename + " is missing") } - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Fatal("Can not close the response body") + } + }(resp.Body) - content, err := ioutil.ReadAll(resp.Body) + content, err := io.ReadAll(resp.Body) if err != nil { log.Fatal("Can not download the file. Network problem?") } currentYear := "" currentMonth := 0 - re := regexp.MustCompile(`^[^[]+ \[(?P[^"]+)"(?P[^"]+)"\]\([^=]+=(?P<isbn>[0-9]+).*$`) + re := regexp.MustCompile(`^[^[]+ \[(?P<author>[^"]+)"(?P<title>[^"]+)"]\([^=]+=(?P<isbn>[0-9]+).*$`) yearRe := regexp.MustCompile(`^## (?P<year>20[0-9]{2})$`) var yearBucket []Item @@ -86,7 +99,7 @@ func getItems(filename string) []Item { currentMonth = 0 // Add the bucket in reverse order - for i := len(yearBucket)-1; i >= 0; i-- { + for i := len(yearBucket) - 1; i >= 0; i-- { items = append(items, yearBucket[i]) } yearBucket = nil @@ -97,10 +110,10 @@ func getItems(filename string) []Item { currentMonth++ yearBucket = append(yearBucket, Item{ - Author: strings.Trim(matches[1], " "), - Title: strings.Trim(matches[2], " "), - ISBN: matches[3], - Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear), + Author: strings.Trim(matches[1], " "), + Title: strings.Trim(matches[2], " "), + ISBN: matches[3], + Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear), Filename: filename, }) } @@ -118,7 +131,7 @@ func getTemplate(sourceFile string, templateFilename string, w http.ResponseWrit // Get all items from the git repo items := getItems(sourceFile) - // Generate the restulting HTML + // Generate the resulting HTML t, err := xmltpl.ParseFiles("templates/" + templateFilename) if err != nil { panic(err) diff --git a/templates/book.html b/templates/book.html index f1481ec..48ae518 100644 --- a/templates/book.html +++ b/templates/book.html @@ -1,5 +1,5 @@ <!DOCTYPE html> -<html> +<html lang="de"> <head> <meta charset="utf-8" /> <title>okoyono.de -- Buch des Monats