]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/ctx.go
Raise copyright years
[nncp.git] / src / cypherpunks.ru / nncp / ctx.go
index 340791139b491b2ac36c446e90c16e0f92597db7..5d705968d680d2885494cefe31333a54ff622e6a 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -20,17 +20,20 @@ package nncp
 
 import (
        "errors"
+       "io/ioutil"
        "os"
        "path/filepath"
 )
 
 type Ctx struct {
-       Self  *NodeOur
-       Neigh map[NodeId]*Node
-       Alias map[string]*NodeId
+       Self   *NodeOur
+       SelfId *NodeId
+       Neigh  map[NodeId]*Node
+       Alias  map[string]*NodeId
 
        Spool      string
        LogPath    string
+       Quiet      bool
        Debug      bool
        NotifyFile *FromToYAML
        NotifyFreq *FromToYAML
@@ -66,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
+}