Fixed codeloc option behaviour

also fixed config file paths to have proper tree structure
This commit is contained in:
David Vennik
2023-01-09 00:02:29 +00:00
parent aecd1c2778
commit 066482d8c9
19 changed files with 81 additions and 127 deletions

View File

@@ -2,6 +2,8 @@ package main
import (
"fmt"
"os"
"github.com/indra-labs/indra"
"github.com/indra-labs/indra/pkg/app"
"github.com/indra-labs/indra/pkg/cfg"
@@ -14,7 +16,6 @@ import (
"github.com/indra-labs/indra/pkg/server"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/multiformats/go-multiaddr"
"os"
)
var (
@@ -22,10 +23,6 @@ var (
check = log.E.Chk
)
func init() {
log2.App = "indra"
}
var commands = &cmds.Command{
Name: "indra",
Description: "Network Freedom.",
@@ -137,7 +134,7 @@ var commands = &cmds.Command{
func multiAddrSanitizer(opt *list.Opt) error {
//log.I.Ln("adding p2p listener", opt.String())
// log.I.Ln("adding p2p listener", opt.String())
return nil
}

View File

@@ -2,7 +2,7 @@ package app
import (
"github.com/indra-labs/indra"
cmds2 "github.com/indra-labs/indra/pkg/cmds"
"github.com/indra-labs/indra/pkg/cmds"
log2 "github.com/indra-labs/indra/pkg/log"
)
@@ -12,30 +12,31 @@ var (
)
type App struct {
*cmds2.Command
launch *cmds2.Command
*cmds.Command
launch *cmds.Command
runArgs []string
cmds2.Envs
cmds.Envs
}
func New(cmd *cmds2.Command, args []string) (a *App, e error) {
func New(c *cmds.Command, args []string) (a *App, e error) {
log2.App = c.Name
// Add the default configuration items for datadir/configfile
cmds2.GetConfigBase(cmd.Configs, cmd.Name, false)
cmds.GetConfigBase(c.Configs, c.Name, false)
// Add the help function
cmd.AddCommand(cmds2.Help())
a = &App{Command: cmd}
c.AddCommand(cmds.Help())
a = &App{Command: c}
if a.Command, e = cmds.Init(c, nil); check(e) {
return
}
// We first parse the CLI args, in case config file location has been
// specified
if a.launch, _, e = a.Command.ParseCLIArgs(args); check(e) {
return
}
if e = cmd.LoadConfig(); log.E.Chk(e) {
if e = c.LoadConfig(); log.E.Chk(e) {
return
}
if a.Command, e = cmds2.Init(cmd, nil); check(e) {
return
}
a.Envs = cmd.GetEnvs()
a.Envs = c.GetEnvs()
if e = a.Envs.LoadFromEnvironment(); check(e) {
return
}

View File

@@ -11,7 +11,7 @@ import (
func TestNew(t *testing.T) {
log2.SetLogLevel(log2.Trace)
log2.CodeLoc = true
args1 := "/random/path/to/server_binary --cafile ~/some/cafile --LC=cn node -addrindex --BD 48h30s"
args1s := strings.Split(args1, " ")
var a *App

View File

@@ -187,7 +187,6 @@ func lookAhead(cmd *Command, cfgName, arg string, iArgs []string,
break
}
}
// Otherwise set the boolean value to the opposite of default.
cur := cmd.Configs[cfgName].Meta().Default()
cmd.Configs[cfgName].FromString(cur)
v := !cmd.Configs[cfgName].Value().Bool()

View File

@@ -9,7 +9,7 @@ import (
func TestCommand_ParseCLIArgs(t *testing.T) {
log2.SetLogLevel(log2.Trace)
log2.CodeLoc = true
ec := GetExampleCommands()
o, _ := Init(ec, nil)
args6 := "/random/path/to/server_binary --cafile ~/some/cafile --LC=cn " +

View File

@@ -19,9 +19,7 @@ import (
type Op func(c *Command, args []string) error
var NoOp = func(c *Command, args []string) error { return nil }
var Tags = func(s ...string) []string {
return s
}
var Tags = func(s ...string) []string { return s }
// Command is a specification for a command and can include any number of
// subcommands, and for each Command a list of options
@@ -96,12 +94,9 @@ loaded from at application startup, and where it will be written if changed.
Documentation: strings.TrimSpace(strings.TrimSpace(`
Toggles on and off the printing of code locations in logs.
`)),
Default: "true",
Default: "false",
}, func(o *toggle.Opt) (err error) {
// fmt.Println(log2.CodeLoc)
// fmt.Println(runtime.Caller(1))
// fmt.Println(spew.Sdump(o.Value().Bool()))
// log2.CodeLoc = o.Value().Bool()
log2.LogCodeLocations(o.Value().Bool())
return
}),
@@ -227,7 +222,8 @@ func (c *Command) GetOpt(path path.Path) (o config.Option) {
// search subcommands
for i := range c.Commands {
if util.Norm(c.Commands[i].Name) == util.Norm(p[1]) {
return c.Commands[i].GetOpt(p[1:])
cc := c.Commands[i]
return cc.GetOpt(p[1:])
}
}
case len(p) == 2:
@@ -262,7 +258,6 @@ func (c *Command) GetCommand(p string) (o *Command) {
}
func (c *Command) GetValue(key string) config.Concrete {
return c.Configs[key].Value()
}

View File

@@ -6,7 +6,6 @@ import (
"os/exec"
"os/signal"
"runtime"
"strings"
"syscall"
"go.uber.org/atomic"
@@ -43,12 +42,6 @@ var interruptCallbackSources []string
// responds to custom shutdown signals as required
func Listener() {
invokeCallbacks := func() {
log.I.Ln(
"running interrupt callbacks",
len(interruptCallbacks),
strings.Repeat(" ", 48),
interruptCallbackSources,
)
// run handlers in LIFO order.
for i := range interruptCallbacks {
idx := len(interruptCallbacks) - 1 - i
@@ -103,6 +96,7 @@ out:
case sig := <-ch:
// if !requested {
// L.Printf("\r>>> received signal (%s)\n", sig)
fmt.Print("\r")
log.I.Ln("received interrupt signal", sig)
requested.Store(true)
invokeCallbacks()

View File

@@ -144,7 +144,7 @@ var (
// allSubsystems stores all package subsystem names found in the current
// application.
allSubsystems []string
CodeLoc = true
codeLoc = false
)
func GetAllSubsystems() (o []string) {
@@ -202,6 +202,12 @@ func SetLogLevel(l LogLevel) {
logLevel = l
}
func LogCodeLocations(on bool) {
writerMx.Lock()
defer writerMx.Unlock()
codeLoc = on
}
// GetLoc calls runtime.Caller and formats as expected by source code editors
// for terminal hyperlinks
//
@@ -246,8 +252,9 @@ func GetLoc(skip int, subsystem string) (output string) {
}
// LocTimeStampFormat is a custom time format that provides millisecond precision.
var LocTimeStampFormat = "2006-01-02T15:04:05.000000Z07:00"
var timeStampFormat = time.Stamp
var LocTimeStampFormat = "2006-01-02T15:04:05.000000"
var timeStampFormat = "15:04:05.000"
// SetTimeStampFormat sets a custom timeStampFormat for the logger
func SetTimeStampFormat(format string) {
@@ -256,9 +263,7 @@ func SetTimeStampFormat(format string) {
// getTimeText is a helper that returns the current time with the
// timeStampFormat that is configured.
func getTimeText(tsf string) string {
return time.Now().Format(tsf)
}
func getTimeText(tsf string) string { return time.Now().Format(tsf) }
// joinStrings constructs a string from a slice of interface same as Println but
// without the terminal newline
@@ -286,7 +291,7 @@ func logPrint(
formatString := "%v%s%s%-6v %s\n"
loc := ""
tsf := timeStampFormat
if CodeLoc {
if codeLoc {
formatString = "%-58v%s%s%-6v %s\n"
loc = GetLoc(3, subsystem)
tsf = LocTimeStampFormat

View File

@@ -17,13 +17,9 @@ type Opt struct {
h []Hook
}
func (o *Opt) Path() (p path.Path) {
return o.p
}
func (o *Opt) Path() (p path.Path) { return o.p }
func (o *Opt) SetPath(p path.Path) {
o.p = p
}
func (o *Opt) SetPath(p path.Path) { o.p = p }
var _ config.Option = &Opt{}
@@ -70,9 +66,7 @@ func (o *Opt) String() (s string) {
return strconv.FormatInt(o.v.Load(), 10)
}
func (o *Opt) Expanded() (s string) {
return o.String()
}
func (o *Opt) Expanded() (s string) { return o.String() }
func (o *Opt) SetExpanded(s string) {
err := o.FromString(s)

View File

@@ -18,13 +18,9 @@ type Opt struct {
h []Hook
}
func (o *Opt) Path() (p path.Path) {
return o.p
}
func (o *Opt) Path() (p path.Path) { return o.p }
func (o *Opt) SetPath(p path.Path) {
o.p = p
}
func (o *Opt) SetPath(p path.Path) { o.p = p }
var _ config.Option = &Opt{}
@@ -68,13 +64,11 @@ func (o *Opt) String() (s string) {
return fmt.Sprint(o.v.Load())
}
func (o *Opt) Expanded() (s string) {
return o.String()
}
func (o *Opt) Expanded() (s string) { return o.String() }
func (o *Opt) SetExpanded(s string) {
err := o.FromString(s)
log.E.Chk(err)
if err := o.FromString(s); log.E.Chk(err) {
}
}
func (o *Opt) Value() (c config.Concrete) {

View File

@@ -17,13 +17,9 @@ type Opt struct {
h []Hook
}
func (o *Opt) Path() (p path.Path) {
return o.p
}
func (o *Opt) Path() (p path.Path) { return o.p }
func (o *Opt) SetPath(p path.Path) {
o.p = p
}
func (o *Opt) SetPath(p path.Path) { o.p = p }
var _ config.Option = &Opt{}
@@ -70,9 +66,7 @@ func (o *Opt) String() (s string) {
return strconv.FormatFloat(o.v.Load(), 'f', -1, 64)
}
func (o *Opt) Expanded() (s string) {
return o.String()
}
func (o *Opt) Expanded() (s string) { return o.String() }
func (o *Opt) SetExpanded(s string) {
err := o.FromString(s)

View File

@@ -20,13 +20,9 @@ type Opt struct {
var _ config.Option = &Opt{}
func (o *Opt) Path() (p path.Path) {
return o.p
}
func (o *Opt) Path() (p path.Path) { return o.p }
func (o *Opt) SetPath(p path.Path) {
o.p = p
}
func (o *Opt) SetPath(p path.Path) { o.p = p }
type Hook func(*Opt) error

View File

@@ -18,13 +18,9 @@ type Opt struct {
h []Hook
}
func (o *Opt) Path() (p path.Path) {
return o.p
}
func (o *Opt) Path() (p path.Path) { return o.p }
func (o *Opt) SetPath(p path.Path) {
o.p = p
}
func (o *Opt) SetPath(p path.Path) { o.p = p }
var _ config.Option = &Opt{}
@@ -62,17 +58,11 @@ func (o *Opt) FromString(s string) (e error) {
return
}
func (o *Opt) String() (s string) {
return o.v.Load()
}
func (o *Opt) String() (s string) { return o.v.Load() }
func (o *Opt) Expanded() (s string) {
return o.x.Load()
}
func (o *Opt) Expanded() (s string) { return o.x.Load() }
func (o *Opt) SetExpanded(s string) {
o.x.Store(s)
}
func (o *Opt) SetExpanded(s string) { o.x.Store(s) }
func (o *Opt) Value() (c config.Concrete) {
c = config.NewConcrete()

View File

@@ -18,13 +18,9 @@ type Opt struct {
h []Hook
}
func (o *Opt) Path() (p path.Path) {
return o.p
}
func (o *Opt) Path() (p path.Path) { return o.p }
func (o *Opt) SetPath(p path.Path) {
o.p = p
}
func (o *Opt) SetPath(p path.Path) { o.p = p }
var _ config.Option = &Opt{}
@@ -78,9 +74,7 @@ func (o *Opt) String() (s string) {
return strconv.FormatBool(o.v.Load())
}
func (o *Opt) Expanded() (s string) {
return o.String()
}
func (o *Opt) Expanded() (s string) { return o.String() }
func (o *Opt) SetExpanded(s string) {
err := o.FromString(s)

View File

@@ -2,10 +2,11 @@ package metrics
import (
"context"
"time"
"github.com/indra-labs/indra"
log2 "github.com/indra-labs/indra/pkg/log"
"github.com/libp2p/go-libp2p/core/host"
"time"
)
var (

View File

@@ -3,13 +3,14 @@ package seed
import (
"context"
"errors"
"sync"
"time"
"github.com/indra-labs/indra"
log2 "github.com/indra-labs/indra/pkg/log"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"sync"
"time"
)
var (

View File

@@ -10,7 +10,6 @@ import (
"github.com/indra-labs/indra/pkg/key/prv"
"github.com/indra-labs/indra/pkg/key/pub"
log2 "github.com/indra-labs/indra/pkg/log"
"github.com/indra-labs/indra/pkg/nonce"
"github.com/indra-labs/indra/pkg/sha256"
"github.com/indra-labs/indra/pkg/slice"
@@ -30,7 +29,7 @@ import (
)
func TestOnionSkins_Cipher(t *testing.T) {
log2.CodeLoc = true
var e error
hdrP, pldP := GetTwoPrvKeys(t)
// hdr, pld := pub.Derive(hdrP), pub.Derive(pldP)
@@ -60,7 +59,7 @@ func TestOnionSkins_Cipher(t *testing.T) {
}
func TestOnionSkins_Confirmation(t *testing.T) {
log2.CodeLoc = true
var e error
n := nonce.NewID()
on := OnionSkins{}.
@@ -85,7 +84,7 @@ func TestOnionSkins_Confirmation(t *testing.T) {
}
func TestOnionSkins_Delay(t *testing.T) {
log2.CodeLoc = true
var e error
del := time.Duration(rand.Uint64())
on := OnionSkins{}.
@@ -110,7 +109,7 @@ func TestOnionSkins_Delay(t *testing.T) {
}
func TestOnionSkins_Exit(t *testing.T) {
log2.CodeLoc = true
var e error
prvs, pubs := GetCipherSet(t)
ciphers := GenCiphers(prvs, pubs)
@@ -161,7 +160,7 @@ func TestOnionSkins_Exit(t *testing.T) {
}
func TestOnionSkins_Forward(t *testing.T) {
log2.CodeLoc = true
var e error
ipSizes := []int{net.IPv4len, net.IPv6len}
for i := range ipSizes {
@@ -204,7 +203,7 @@ func TestOnionSkins_Forward(t *testing.T) {
}
func TestOnionSkins_Layer(t *testing.T) {
log2.CodeLoc = true
var e error
n := nonce.NewID()
n1 := nonce.New()
@@ -245,7 +244,7 @@ func TestOnionSkins_Layer(t *testing.T) {
}
func TestOnionSkins_Purchase(t *testing.T) {
log2.CodeLoc = true
var e error
prvs, pubs := GetCipherSet(t)
ciphers := GenCiphers(prvs, pubs)
@@ -285,7 +284,7 @@ func TestOnionSkins_Purchase(t *testing.T) {
}
func TestOnionSkins_Reply(t *testing.T) {
log2.CodeLoc = true
var e error
ipSizes := []int{net.IPv4len, net.IPv6len}
for i := range ipSizes {
@@ -328,7 +327,7 @@ func TestOnionSkins_Reply(t *testing.T) {
}
func TestOnionSkins_Response(t *testing.T) {
log2.CodeLoc = true
var e error
var msg slice.Bytes
var hash sha256.Hash
@@ -360,7 +359,7 @@ func TestOnionSkins_Response(t *testing.T) {
}
func TestOnionSkins_Session(t *testing.T) {
log2.CodeLoc = true
var e error
hdrP, pldP := GetTwoPrvKeys(t)
hdr, pld := pub.Derive(hdrP), pub.Derive(pldP)
@@ -391,7 +390,7 @@ func TestOnionSkins_Session(t *testing.T) {
}
func TestOnionSkins_Token(t *testing.T) {
log2.CodeLoc = true
var e error
ni := nonce.NewID()
n := sha256.Single(ni[:])

View File

@@ -118,7 +118,7 @@ func PeelExit(t *testing.T, b slice.Bytes,
}
func TestPing(t *testing.T) {
log2.CodeLoc = true
_, ks, e := signer.New()
if check(e) {
t.Error(e)
@@ -198,7 +198,7 @@ func TestPing(t *testing.T) {
}
func TestSendKeys(t *testing.T) {
log2.CodeLoc = true
_, ks, e := signer.New()
if check(e) {
t.Error(e)
@@ -322,7 +322,7 @@ func TestSendKeys(t *testing.T) {
}
func TestSendPurchase(t *testing.T) {
log2.CodeLoc = true
log2.SetLogLevel(log2.Trace)
_, ks, e := signer.New()
if check(e) {
@@ -424,7 +424,7 @@ func TestSendPurchase(t *testing.T) {
}
func TestSendExit(t *testing.T) {
log2.CodeLoc = true
_, ks, e := signer.New()
if check(e) {
t.Error(e)

View File

@@ -8,11 +8,11 @@ var (
// URL is the git URL for the repository.
URL = "github.com/indra-labs/indra"
// GitRef is the gitref, as in refs/heads/branchname.
GitRef = "refs/heads/protocol"
GitRef = "refs/heads/proc-debug"
// ParentGitCommit is the commit hash of the parent HEAD.
ParentGitCommit = "0a37f65155ad746edf54a4f653d65bfc889e078a"
ParentGitCommit = "14449a6c621cb0478387c84053d31e1233af2115"
// BuildTime stores the time when the current binary was built.
BuildTime = "2023-01-08T13:56:39Z"
BuildTime = "2023-01-09T00:02:29Z"
// SemVer lists the (latest) git tag on the release.
SemVer = "v0.1.5"
// PathBase is the path base returned from runtime caller.