Skip to content

Commit

Permalink
refactored config, added cli options
Browse files Browse the repository at this point in the history
* config global state removed from the config module
* config global state now resides in main, TODO to remove it
* config now creates a default if none are found
* added command line option for specifying the config file
* added command line option to specify the log level
* config directory now looks in user dir, in a platform agnostic way.
ex: on linux, will typically look in `~/.config/my5g/RANTester` for file
`config.yml`
* updated go.mod to use go v1.16
* embedded default config file data
* errors are now bubbled up appropriately
* depends on #33
  • Loading branch information
gravestench committed Mar 2, 2022
1 parent 78c3216 commit 0108ab1
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 129 deletions.
9 changes: 9 additions & 0 deletions cmd/my5g-RANTester/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/urfave/cli/v2"
"my5G-RANTester/config"
)

const (
Expand All @@ -23,6 +24,14 @@ const (
func setupCommands(a *cli.App) {
var commands []*cli.Command

// TODO: this code is coupled to the ./config module.
// this is here because config data is used inside of the test functions.
if fixme, err := config.Load(); err != nil {
panic(err)
} else {
cfg = fixme
}

loadTestFlags := []cli.Flag{
&cli.IntFlag{Name: argNumUE, Value: argNumUEDefault, Aliases: []string{"n"}},
}
Expand Down
25 changes: 19 additions & 6 deletions cmd/my5g-RANTester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ const (
)

func main() {
initLogger()
app := &cli.App{Before: runAllActions}

initLogger()
log.Infof(fmtMsgVersion, version)

app := &cli.App{}

setupOptions(app)
setupCommands(app)

if err := app.Run(os.Args); err != nil {
Expand All @@ -32,8 +31,22 @@ func initLogger() {
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)

// Only log the warning severity or above.
log.SetLevel(log.WarnLevel)
log.SetLevel(0)

spew.Config.Indent = "\t"
}

func runAllActions(c *cli.Context) (err error) {
for _, fn := range []func(c *cli.Context) error{
showUsageString,
setLogLevel,
setConfigFile,
} {
err = fn(c)
if err != nil {
break
}
}

return err
}
76 changes: 76 additions & 0 deletions cmd/my5g-RANTester/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"errors"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"my5G-RANTester/config"
)

const (
optLogLevel = "loglevel"
optLogLevelAlias1 = "l"
optLogLevelUsage = "set the log level of the app, (6 is most verbose)"
optLogLevelDefault = log.InfoLevel

optConfig = "config"
optConfigAlias = "c"
optConfigUsage = "set the config file to use"
optConfigDefault = ""
)

// TODO: remove this ugly global variable *hisssss*
var cfg *config.Config

func setupOptions(a *cli.App) {
a.Flags = []cli.Flag{
&cli.IntFlag{
Name: optLogLevel,
Aliases: []string{optLogLevelAlias1},
Usage: optLogLevelUsage,
Value: int(optLogLevelDefault),
},
&cli.StringFlag{
Name: optConfig,
Aliases: []string{optConfigAlias},
Usage: optConfigUsage,
Value: optConfigDefault,
},
}
}

func showUsageString(c *cli.Context) error {
if c.Args().Len() == 0 {
_ = cli.ShowAppHelp(c)

return errors.New("no commands specified, stopping")
}

return nil
}

func setLogLevel(c *cli.Context) error {
ll := c.Int(optLogLevel)

log.SetLevel(log.Level(ll))

if ll != int(optLogLevelDefault) {
log.Debugf("setting log level to %v", log.Level(ll))
}

return nil
}

func setConfigFile(c *cli.Context) error {
path := c.String(optConfig)

loaded, err := config.Load(path)
if err != nil {
log.Fatal(err)
}

log.Debug("config loaded successfully")
cfg = loaded

return nil
}
4 changes: 0 additions & 4 deletions cmd/my5g-RANTester/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"

log "github.com/sirupsen/logrus"

"my5G-RANTester/config"
)

func testLogCommonInfo(name string, numUE int) {
Expand All @@ -18,8 +16,6 @@ func testLogCommonInfo(name string, numUE int) {
fmtAMFInfo = "AMF IP/Port: " + fmtIPPort
)

cfg := config.Data

log.Info(logSep)

msgStartTest := fmt.Sprintf(fmtStartTest, name)
Expand Down
5 changes: 1 addition & 4 deletions cmd/my5g-RANTester/test_gnb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"github.com/urfave/cli/v2"

"my5G-RANTester/internal/templates"
)

Expand All @@ -13,7 +12,5 @@ func testGNB(_ *cli.Context) error {

testLogCommonInfo(name, argNumUEDefault)

templates.TestAttachGnbWithConfiguration()

return nil
return templates.TestAttachGnbWithConfiguration(*cfg)
}
4 changes: 1 addition & 3 deletions cmd/my5g-RANTester/test_register_multi_ue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"my5G-RANTester/internal/templates"
)

Expand All @@ -21,7 +20,6 @@ func testRegisterMultiUE(c *cli.Context) error {
numUE := c.Int(argNumUE)

testLogCommonInfo(name, numUE)
templates.TestMultiUesInQueue(numUE)

return nil
return templates.TestMultiUesInQueue(*cfg, numUE)
}
5 changes: 1 addition & 4 deletions cmd/my5g-RANTester/test_ue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"github.com/urfave/cli/v2"

"my5G-RANTester/internal/templates"
)

Expand All @@ -13,7 +12,5 @@ func testUE(_ *cli.Context) error {

testLogCommonInfo(name, argNumUEDefault)

templates.TestAttachUeWithConfiguration()

return nil
return templates.TestAttachUeWithConfiguration(*cfg)
}
108 changes: 28 additions & 80 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,44 @@
package config

import (
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
"path"
"path/filepath"
"runtime"
)

// Conf: Used for access to configuration
var Data = getConfig()

type Config struct {
GNodeB struct {
ControlIF struct {
Ip string `yaml: "ip"`
Port int `yaml: "port"`
} `yaml: "controlif"`
Ip string `yaml:"ip"`
Port int `yaml:"port"`
} `yaml:"controlif"`
DataIF struct {
Ip string `yaml: "ip"`
Port int `yaml: "port"`
} `yaml: "dataif"`
Ip string `yaml:"ip"`
Port int `yaml:"port"`
} `yaml:"dataif"`
PlmnList struct {
Mcc string `yaml: "mmc"`
Mnc string `yaml: "mnc"`
Tac string `yaml: "tac"`
GnbId string `yaml: "gnbid"`
} `yaml: "plmnlist"`
Mcc string `yaml:"mmc"`
Mnc string `yaml:"mnc"`
Tac string `yaml:"tac"`
GnbId string `yaml:"gnbid"`
} `yaml:"plmnlist"`
SliceSupportList struct {
Sst string `yaml: "sst"`
Sd string `yaml: "sd"`
} `yaml: "slicesupportlist"`
Sst string `yaml:"sst"`
Sd string `yaml:"sd"`
} `yaml:"slicesupportlist"`
} `yaml:"gnodeb"`
Ue struct {
Msin string `yaml: "msin"`
Key string `yaml: "key"`
Opc string `yaml: "opc"`
Amf string `yaml: "amf"`
Sqn string `yaml: "sqn"`
Msin string `yaml:"msin"`
Key string `yaml:"key"`
Opc string `yaml:"opc"`
Amf string `yaml:"amf"`
Sqn string `yaml:"sqn"`
Hplmn struct {
Mcc string `yaml: "mcc"`
Mnc string `yaml: "mnc"`
} `yaml: "hplmn"`
Mcc string `yaml:"mcc"`
Mnc string `yaml:"mnc"`
} `yaml:"hplmn"`
Snssai struct {
Sst int `yaml: "sst"`
Sd string `yaml: "sd"`
} `yaml: "snssai"`
Sst int `yaml:"sst"`
Sd string `yaml:"sd"`
} `yaml:"snssai"`
} `yaml:"ue"`
AMF struct {
Ip string `yaml: "ip"`
Port int `yaml: "port"`
Name string `yaml: "name"`
Ip string `yaml:"ip"`
Port int `yaml:"port"`
Name string `yaml:"name"`
} `yaml:"amfif"`
}

func RootDir() string {
_, b, _, _ := runtime.Caller(0)
d := path.Join(path.Dir(b))
return filepath.Dir(d)
}

func getConfig() Config {
var cfg = Config{}
Ddir := RootDir()
configPath, err := filepath.Abs(Ddir + "/config/config.yml")
log.Debug(configPath)
if err != nil {
log.Fatal("Could not find config in: ", configPath)
}
file, err := ioutil.ReadFile(configPath)
err = yaml.Unmarshal([]byte(file), &cfg)
if err != nil {
log.Fatal("Could not read file in: ", configPath)
}

return cfg
}

func GetConfig() (Config, error) {
var cfg = Config{}
Ddir := RootDir()
configPath, err := filepath.Abs(Ddir + "/config/config.yml")
log.Debug(configPath)
if err != nil {
return Config{}, nil
}
file, err := ioutil.ReadFile(configPath)
err = yaml.Unmarshal([]byte(file), &cfg)
if err != nil {
return Config{}, nil
}

return cfg, nil
}
Loading

0 comments on commit 0108ab1

Please sign in to comment.