]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/main.go
Simplify config read and arguments parsing
[sgblog.git] / cmd / sgblog / main.go
index 7603d125b80d5b45f7ca1b0bc3b0506761aea93b..b373564cbcd6dd31d14efeb17720862af395a392 100644 (file)
@@ -21,9 +21,10 @@ package main
 import (
        "bytes"
        "crypto/sha1"
+       "encoding/json"
+       "flag"
        "fmt"
        "io/ioutil"
-       "os"
        "regexp"
        "sort"
        "strings"
@@ -32,6 +33,7 @@ import (
        "github.com/go-git/go-git/v5"
        "github.com/go-git/go-git/v5/plumbing"
        "github.com/go-git/go-git/v5/plumbing/object"
+       "github.com/hjson/hjson-go"
        "go.cypherpunks.ru/recfile"
 )
 
@@ -195,9 +197,37 @@ func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
        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()
        }