From d28829ba288452dabf46cc42f45548dd54df64e9 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sat, 1 May 2021 16:11:26 +0300 Subject: [PATCH] Simplify config read and arguments parsing --- cmd/sgblog/gopher.go | 20 ++------------------ cmd/sgblog/http.go | 20 +++----------------- cmd/sgblog/main.go | 36 +++++++++++++++++++++++++++++++++--- common.go | 2 +- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/cmd/sgblog/gopher.go b/cmd/sgblog/gopher.go index 183914c..b12a2b8 100644 --- a/cmd/sgblog/gopher.go +++ b/cmd/sgblog/gopher.go @@ -19,11 +19,9 @@ package main import ( "bufio" - "encoding/json" "errors" "fmt" "io" - "io/ioutil" "log" "net/url" "os" @@ -34,7 +32,6 @@ 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.stargrave.org/sgblog" ) @@ -86,24 +83,11 @@ type TableMenuEntry struct { Topics []string } -func serveGopher() { - cfgPath := os.Args[2] - cfgRaw, err := ioutil.ReadFile(cfgPath) +func serveGopher(cfgPath string) { + cfg, err := readCfg(cfgPath) if err != nil { log.Fatalln(err) } - var cfgGeneral map[string]interface{} - if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil { - log.Fatalln(err) - } - cfgRaw, err = json.Marshal(cfgGeneral) - if err != nil { - log.Fatalln(err) - } - var cfg *Cfg - if err = json.Unmarshal(cfgRaw, &cfg); err != nil { - log.Fatalln(err) - } if cfg.GopherDomain == "" { log.Fatalln("GopherDomain is not configured") } diff --git a/cmd/sgblog/http.go b/cmd/sgblog/http.go index bad1cb8..d039ed3 100644 --- a/cmd/sgblog/http.go +++ b/cmd/sgblog/http.go @@ -22,14 +22,12 @@ import ( "compress/gzip" "crypto/sha1" "encoding/hex" - "encoding/json" "encoding/xml" "errors" "fmt" "hash" "html" "io" - "io/ioutil" "log" "net/url" "os" @@ -40,7 +38,6 @@ 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.stargrave.org/sgblog" "go.stargrave.org/sgblog/cmd/sgblog/atom" "golang.org/x/crypto/blake2b" @@ -260,22 +257,11 @@ func serveHTTP() { if cfgPath == "" { log.Fatalln("SGBLOG_CFG is not set") } - cfgRaw, err := ioutil.ReadFile(cfgPath) + cfg, err := readCfg(cfgPath) if err != nil { - makeErr(err) - } - var cfgGeneral map[string]interface{} - if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil { - makeErr(err) - } - cfgRaw, err = json.Marshal(cfgGeneral) - if err != nil { - makeErr(err) - } - var cfg *Cfg - if err = json.Unmarshal(cfgRaw, &cfg); err != nil { - makeErr(err) + log.Fatalln(err) } + pathInfo, exists := os.LookupEnv("PATH_INFO") if !exists { pathInfo = "/" diff --git a/cmd/sgblog/main.go b/cmd/sgblog/main.go index 7603d12..b373564 100644 --- a/cmd/sgblog/main.go +++ b/cmd/sgblog/main.go @@ -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() } diff --git a/common.go b/common.go index ea1e7db..ebb5a9d 100644 --- a/common.go +++ b/common.go @@ -2,6 +2,6 @@ package sgblog const ( - Version = "0.16.0" + Version = "0.17.0" WhenFmt = "2006-01-02 15:04:05Z07:00" ) -- 2.44.0