]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/ctx.go
Ability to override spool directory and logfile
[nncp.git] / src / cypherpunks.ru / nncp / ctx.go
index 21b3d6fd8e8efe76e146b02e1d47af38d0df675a..8e550fb94e8c1bb3d18d166e9bf1da1c37e0cbb4 100644 (file)
@@ -20,6 +20,7 @@ package nncp
 
 import (
        "errors"
+       "io/ioutil"
        "os"
        "path/filepath"
 )
@@ -68,3 +69,37 @@ func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
        fd.Close()
        return nil
 }
+
+func CtxFromCmdline(cfgPath, spoolPath, logPath string, quiet, debug bool) (*Ctx, error) {
+       env := os.Getenv(CfgPathEnv)
+       if env != "" {
+               cfgPath = env
+       }
+       cfgRaw, err := ioutil.ReadFile(cfgPath)
+       if err != nil {
+               return nil, err
+       }
+       ctx, err := CfgParse(cfgRaw)
+       if err != nil {
+               return nil, err
+       }
+       if spoolPath == "" {
+               env = os.Getenv(CfgSpoolEnv)
+               if env != "" {
+                       ctx.Spool = env
+               }
+       } else {
+               ctx.Spool = spoolPath
+       }
+       if logPath == "" {
+               env = os.Getenv(CfgLogEnv)
+               if env != "" {
+                       ctx.LogPath = env
+               }
+       } else {
+               ctx.LogPath = logPath
+       }
+       ctx.Quiet = quiet
+       ctx.Debug = debug
+       return ctx, nil
+}