]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog-comment-add/main.go
io/ioutil is deprecated since Go 1.16
[sgblog.git] / cmd / sgblog-comment-add / main.go
index 837666c3b8dd12a1f71b713dbefbc36c29666da4..186e56f36ad1635e60c05b1dde4ee871fe26b78b 100644 (file)
@@ -1,6 +1,6 @@
 /*
-SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
-Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
+SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
+Copyright (C) 2020-2022 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
@@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-// Git-backed CGI/inetd blogging/phlogging engine
+// Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
 package main
 
 import (
@@ -24,24 +24,27 @@ import (
        "encoding/hex"
        "flag"
        "fmt"
-       "io/ioutil"
+       "io"
        "log"
        "mime"
        "net/mail"
        "os"
        "os/exec"
+       "regexp"
        "strconv"
        "strings"
        "syscall"
        "time"
 
-       "go.cypherpunks.ru/netstring/v2"
+       "go.cypherpunks.ru/recfile"
        "go.stargrave.org/sgblog"
 )
 
+var hashFinder = regexp.MustCompile("([0-9a-f]{40})")
+
 // Remove various whitespaces and excess lines, because git-notes-add
 // will remove and we have to know exact bytes count
-func cleanupBody(body string) string {
+func cleanupBody(body string) []string {
        lines := strings.Split(string(body), "\n")
        for i, line := range lines {
                line = strings.ReplaceAll(line, "       ", "    ")
@@ -67,7 +70,7 @@ func cleanupBody(body string) string {
                }
                withoutDups = append(withoutDups, line)
        }
-       return strings.Join(withoutDups, "\n")
+       return withoutDups
 }
 
 func main() {
@@ -100,7 +103,7 @@ func main() {
        if err != nil {
                log.Fatal(err)
        }
-       body, err := ioutil.ReadAll(r)
+       body, err := io.ReadAll(r)
        if err != nil {
                log.Fatal(err)
        }
@@ -116,7 +119,10 @@ func main() {
                log.Fatal(err)
        }
 
-       subj = strings.TrimPrefix(subj, "Re: ")
+       subj = hashFinder.FindString(subj)
+       if subj == "" {
+               log.Fatal("no commit hash found in subject")
+       }
        if h, err := hex.DecodeString(subj); err != nil || len(h) != sha1.Size {
                os.Exit(0)
        }
@@ -137,13 +143,25 @@ func main() {
        note = bytes.TrimRight(note, "\r\n")
 
        buf := bytes.NewBuffer(note)
-       w := netstring.NewWriter(buf)
-       w.WriteChunk([]byte(fmt.Sprintf(
-               "From: %s\nDate: %s\n\n%s",
-               from,
-               time.Now().UTC().Format(sgblog.WhenFmt),
-               cleanupBody(string(body)),
-       )))
+       recfileW := recfile.NewWriter(buf)
+       if _, err = recfileW.RecordStart(); err != nil {
+               log.Fatal(err)
+       }
+       // We trimmed newline, so have to start record twice
+       if _, err = recfileW.RecordStart(); err != nil {
+               log.Fatal(err)
+       }
+       if _, err = recfileW.WriteFields(
+               recfile.Field{Name: "From", Value: from},
+               recfile.Field{Name: "Date", Value: time.Now().UTC().Format(sgblog.WhenFmt)},
+       ); err != nil {
+               log.Fatal(err)
+       }
+       if _, err = recfileW.WriteFieldMultiline(
+               "Body", append([]string{""}, cleanupBody(string(body))...),
+       ); err != nil {
+               log.Fatal(err)
+       }
 
        if *dryRun {
                fmt.Print(buf.String())