Skip to content

Commit

Permalink
econfig working well on ra25 but some issues remain..
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Jul 4, 2023
1 parent 4206391 commit b1bd493
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 69 deletions.
2 changes: 1 addition & 1 deletion econfig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Docs: [GoDoc](https://pkg.go.dev/github.com/emer/emergent/econfig)
TODO:
* slice fields
* OpenFS
* std stuff from ecmd
* flag args are conflicting with config args -- just support all flags directly.


`econfig` provides methods to set values on a `Config` struct through a (TOML) config file or command-line args (`flags` in Go terminology), with support for setting Network params and values on any other struct as well (e.g., an Env to be constructed later in a ConfigEnv method).
Expand Down
7 changes: 5 additions & 2 deletions econfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package econfig
import (
"flag"
"fmt"
"os"
)

var (
Expand Down Expand Up @@ -43,14 +44,16 @@ func Config(cfg any, defaultFile string) ([]string, error) {

if *helpArg || *hArg {
flag.PrintDefaults()
fmt.Println("")
fmt.Println(Usage(cfg))
os.Exit(0)
}

file := defaultFile
if *configArg != "" {
defaultFile = *configArg
file = *configArg
} else if *cfgArg != "" {
defaultFile = *cfgArg
file = *cfgArg
}

err = OpenWithIncludes(cfg, file)
Expand Down
38 changes: 2 additions & 36 deletions econfig/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,10 @@

package econfig

import (
"fmt"
"log"
"reflect"

"github.com/goki/ki/kit"
)
import "github.com/goki/ki/kit"

// SetFromDefaults sets Config values from field tag `def:` values.
// Parsing errors are automatically logged.
func SetFromDefaults(cfg any) error {
return SetFromDefaultsStruct(cfg)
}

// todo: move this to kit:

// SetFromDefaultsStruct sets values of fields in given struct based on
// `def:` default value field tags.
func SetFromDefaultsStruct(obj any) error {
typ := kit.NonPtrType(reflect.TypeOf(obj))
val := kit.NonPtrValue(reflect.ValueOf(obj))
var err error
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
fv := val.Field(i)
if kit.NonPtrType(f.Type).Kind() == reflect.Struct {
SetFromDefaultsStruct(kit.PtrValue(fv).Interface())
continue
}
def, ok := f.Tag.Lookup("def")
if !ok || def == "" {
continue
}
ok = kit.SetRobust(kit.PtrValue(fv).Interface(), def) // overkill but whatever
if !ok {
err = fmt.Errorf("SetFromDefaultsStruct: was not able to set field: %s in object of type: %s from val: %s", f.Name, typ.Name(), def)
log.Println(err)
}
}
return err
return kit.SetFromDefaultTags(cfg)
}
32 changes: 2 additions & 30 deletions econfig/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@ package econfig

import (
"bufio"
"errors"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/BurntSushi/toml" // either one of these works fine

Check failure on line 14 in econfig/io.go

View workflow job for this annotation

GitHub Actions / Build (1.18.x, ubuntu-latest)

missing go.sum entry for module providing package github.com/BurntSushi/toml (imported by github.com/emer/emergent/econfig); to add:
// "github.com/pelletier/go-toml/v2"
"github.com/goki/ki/dirs"
)

// Open reads config from given config file,
// looking on IncludePaths for the file.
func Open(cfg any, file string) error {
filename, err := FindFileOnPaths(IncludePaths, file)
filename, err := dirs.FindFileOnPaths(IncludePaths, file)
if err != nil {
log.Println(err)
return err
Expand Down Expand Up @@ -92,30 +88,6 @@ func OpenWithIncludes(cfg any, file string) error {
return err
}

// FindFileOnPaths attempts to locate given file on given list of paths,
// returning the full Abs path to file if found, else error
func FindFileOnPaths(paths []string, file string) (string, error) {
for _, path := range paths {
filePath := filepath.Join(path, file)
ok, _ := FileExists(filePath)
if ok {
return filePath, nil
}
}
return "", fmt.Errorf("FindFileOnPaths: unable to find file: %s on paths: %v\n", file, paths)
}

func FileExists(filePath string) (bool, error) {
fileInfo, err := os.Stat(filePath)
if err == nil {
return !fileInfo.IsDir(), nil
}
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}

/////////////////////////////////////////////////////////
// Saving

Expand Down
26 changes: 26 additions & 0 deletions econfig/std.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2023, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package econfig

import (
"github.com/emer/emergent/elog"
"github.com/emer/emergent/etime"
)

// LogFileName returns a standard log file name as netName_runName_logName.tsv
func LogFileName(logName, netName, runName string) string {
return netName + "_" + runName + "_" + logName + ".tsv"
}

// SetLogFile sets the log file for given mode and time,
// using given logName (extension), netName and runName,
// if the Config flag is set.
func SetLogFile(logs *elog.Logs, configOn bool, mode etime.Modes, time etime.Times, logName, netName, runName string) {
if !configOn {
return
}
fnm := LogFileName(logName, netName, runName)
logs.SetLogFile(mode, time, fnm)
}

0 comments on commit b1bd493

Please sign in to comment.