]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/main.go
sgblog-topics helper command
[sgblog.git] / cmd / sgblog / main.go
index 68a3914e04c3240591c84e6ee74128f0f048dbfe..ef3ff45d0128cf97c112c994f222e8e76a21553a 100644 (file)
@@ -1,6 +1,6 @@
 /*
 SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
-Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2021 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
@@ -19,18 +19,18 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
-       "bytes"
        "crypto/sha1"
+       "encoding/json"
+       "flag"
        "fmt"
        "io/ioutil"
-       "os"
        "regexp"
        "strings"
 
        "github.com/go-git/go-git/v5"
        "github.com/go-git/go-git/v5/plumbing"
        "github.com/go-git/go-git/v5/plumbing/object"
-       "go.cypherpunks.ru/netstring/v2"
+       "github.com/hjson/hjson-go"
 )
 
 const (
@@ -43,6 +43,8 @@ var (
        notesTree    *object.Tree
        commentsRef  *plumbing.Reference
        commentsTree *object.Tree
+       topicsRef    *plumbing.Reference
+       topicsTree   *object.Tree
 )
 
 type Cfg struct {
@@ -64,6 +66,9 @@ type Cfg struct {
        CommentsNotesRef string
        CommentsEmail    string
 
+       TopicsNotesRef  string
+       TopicsCachePath string
+
        GopherDomain string
 }
 
@@ -76,54 +81,6 @@ func msgSplit(msg string) []string {
        return lines
 }
 
-func getNote(tree *object.Tree, what plumbing.Hash) []byte {
-       if tree == nil {
-               return nil
-       }
-       var entry *object.TreeEntry
-       var err error
-       paths := make([]string, 3)
-       paths[0] = what.String()
-       paths[1] = paths[0][:2] + "/" + paths[0][2:]
-       paths[2] = paths[1][:4+1] + "/" + paths[1][4+1:]
-       for _, p := range paths {
-               entry, err = tree.FindEntry(p)
-               if err == nil {
-                       break
-               }
-       }
-       if entry == nil {
-               return nil
-       }
-       blob, err := repo.BlobObject(entry.Hash)
-       if err != nil {
-               return nil
-       }
-       r, err := blob.Reader()
-       if err != nil {
-               return nil
-       }
-       data, err := ioutil.ReadAll(r)
-       if err != nil {
-               return nil
-       }
-       return bytes.TrimSuffix(data, []byte{'\n'})
-}
-
-func parseComments(data []byte) []string {
-       comments := []string{}
-       nsr := netstring.NewReader(bytes.NewReader(data))
-       for {
-               if _, err := nsr.Next(); err != nil {
-                       break
-               }
-               if comment, err := ioutil.ReadAll(nsr); err == nil {
-                       comments = append(comments, string(comment))
-               }
-       }
-       return comments
-}
-
 func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
        var err error
        repo, err = git.PlainOpen(cfg.GitPath)
@@ -143,6 +100,8 @@ func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
                                notesRef = ref
                        case cfg.CommentsNotesRef:
                                commentsRef = ref
+                       case cfg.TopicsNotesRef:
+                               topicsRef = ref
                        }
                        return nil
                })
@@ -156,13 +115,46 @@ func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
                                commentsTree, _ = commentsCommit.Tree()
                        }
                }
+               if topicsRef != nil {
+                       if topicsCommit, err := repo.CommitObject(topicsRef.Hash()); err == nil {
+                               topicsTree, _ = topicsCommit.Tree()
+                       }
+               }
        }
        return &headHash, nil
 }
 
+func readCfg(cfgPath string) (*Cfg, error) {
+       cfgRaw, err := ioutil.ReadFile(cfgPath)
+       if err != nil {
+               return nil, err
+       }
+       var cfgGeneral map[string]interface{}
+       if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil {
+               return nil, err
+       }
+       cfgRaw, err = json.Marshal(cfgGeneral)
+       if err != nil {
+               return nil, err
+       }
+       var cfg *Cfg
+       if err = json.Unmarshal(cfgRaw, &cfg); err != nil {
+               return nil, err
+       }
+       return cfg, nil
+}
+
 func main() {
-       if len(os.Args) == 3 && os.Args[1] == "-gopher" {
-               serveGopher()
+       gopherCfgPath := flag.String("gopher", "", "Path to gopher-related configuration file")
+       flag.Usage = func() {
+               fmt.Fprintf(flag.CommandLine.Output(), `Usage of sgblog:
+       sgblog -- run CGI HTTP backend
+       sgblog -gopher /path/to/cfg.hjson -- run UCSPI/inetd Gopher backend
+`)
+       }
+       flag.Parse()
+       if *gopherCfgPath != "" {
+               serveGopher(*gopherCfgPath)
        } else {
                serveHTTP()
        }