Skip to content

Commit e9950b2

Browse files
committed
refactored config, added cli options
* 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 my5G#33
1 parent 78c3216 commit e9950b2

13 files changed

+275
-129
lines changed

cmd/my5g-RANTester/commands.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"github.com/urfave/cli/v2"
5+
"my5G-RANTester/config"
56
)
67

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

27+
// TODO: this code is coupled to the ./config module.
28+
// this is here because config data is used inside of the test functions.
29+
if fixme, err := config.Load(); err != nil {
30+
panic(err)
31+
} else {
32+
cfg = fixme
33+
}
34+
2635
loadTestFlags := []cli.Flag{
2736
&cli.IntFlag{Name: argNumUE, Value: argNumUEDefault, Aliases: []string{"n"}},
2837
}

cmd/my5g-RANTester/main.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ const (
1414
)
1515

1616
func main() {
17-
initLogger()
17+
app := &cli.App{Before: runAllActions}
1818

19+
initLogger()
1920
log.Infof(fmtMsgVersion, version)
20-
21-
app := &cli.App{}
22-
21+
setupOptions(app)
2322
setupCommands(app)
2423

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

35-
// Only log the warning severity or above.
36-
log.SetLevel(log.WarnLevel)
34+
log.SetLevel(0)
3735

3836
spew.Config.Indent = "\t"
3937
}
38+
39+
func runAllActions(c *cli.Context) (err error) {
40+
for _, fn := range []func(c *cli.Context) error{
41+
showUsageString,
42+
setLogLevel,
43+
setConfigFile,
44+
} {
45+
err = fn(c)
46+
if err != nil {
47+
break
48+
}
49+
}
50+
51+
return err
52+
}

cmd/my5g-RANTester/options.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
log "github.com/sirupsen/logrus"
6+
"github.com/urfave/cli/v2"
7+
"my5G-RANTester/config"
8+
)
9+
10+
const (
11+
optLogLevel = "loglevel"
12+
optLogLevelAlias1 = "l"
13+
optLogLevelUsage = "set the log level of the app, (6 is most verbose)"
14+
optLogLevelDefault = log.InfoLevel
15+
16+
optConfig = "config"
17+
optConfigAlias = "c"
18+
optConfigUsage = "set the config file to use"
19+
optConfigDefault = ""
20+
)
21+
22+
// TODO: remove this ugly global variable *hisssss*
23+
var cfg *config.Config
24+
25+
func setupOptions(a *cli.App) {
26+
a.Flags = []cli.Flag{
27+
&cli.IntFlag{
28+
Name: optLogLevel,
29+
Aliases: []string{optLogLevelAlias1},
30+
Usage: optLogLevelUsage,
31+
Value: int(optLogLevelDefault),
32+
},
33+
&cli.StringFlag{
34+
Name: optConfig,
35+
Aliases: []string{optConfigAlias},
36+
Usage: optConfigUsage,
37+
Value: optConfigDefault,
38+
},
39+
}
40+
}
41+
42+
func showUsageString(c *cli.Context) error {
43+
if c.Args().Len() == 0 {
44+
_ = cli.ShowAppHelp(c)
45+
46+
return errors.New("no commands specified, stopping")
47+
}
48+
49+
return nil
50+
}
51+
52+
func setLogLevel(c *cli.Context) error {
53+
ll := c.Int(optLogLevel)
54+
55+
log.SetLevel(log.Level(ll))
56+
57+
if ll != int(optLogLevelDefault) {
58+
log.Debugf("setting log level to %v", log.Level(ll))
59+
}
60+
61+
return nil
62+
}
63+
64+
func setConfigFile(c *cli.Context) error {
65+
path := c.String(optConfig)
66+
67+
loaded, err := config.Load(path)
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
72+
log.Debug("config loaded successfully")
73+
cfg = loaded
74+
75+
return nil
76+
}

cmd/my5g-RANTester/test.go

-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55

66
log "github.com/sirupsen/logrus"
7-
8-
"my5G-RANTester/config"
97
)
108

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

21-
cfg := config.Data
22-
2319
log.Info(logSep)
2420

2521
msgStartTest := fmt.Sprintf(fmtStartTest, name)

cmd/my5g-RANTester/test_gnb.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"github.com/urfave/cli/v2"
5-
65
"my5G-RANTester/internal/templates"
76
)
87

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

1413
testLogCommonInfo(name, argNumUEDefault)
1514

16-
templates.TestAttachGnbWithConfiguration()
17-
18-
return nil
15+
return templates.TestAttachGnbWithConfiguration(*cfg)
1916
}

cmd/my5g-RANTester/test_register_multi_ue.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
log "github.com/sirupsen/logrus"
55
"github.com/urfave/cli/v2"
6-
76
"my5G-RANTester/internal/templates"
87
)
98

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

2322
testLogCommonInfo(name, numUE)
24-
templates.TestMultiUesInQueue(numUE)
2523

26-
return nil
24+
return templates.TestMultiUesInQueue(*cfg, numUE)
2725
}

cmd/my5g-RANTester/test_ue.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"github.com/urfave/cli/v2"
5-
65
"my5G-RANTester/internal/templates"
76
)
87

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

1413
testLogCommonInfo(name, argNumUEDefault)
1514

16-
templates.TestAttachUeWithConfiguration()
17-
18-
return nil
15+
return templates.TestAttachUeWithConfiguration(*cfg)
1916
}

config/config.go

+28-80
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,44 @@
11
package config
22

3-
import (
4-
log "github.com/sirupsen/logrus"
5-
"gopkg.in/yaml.v2"
6-
"io/ioutil"
7-
"path"
8-
"path/filepath"
9-
"runtime"
10-
)
11-
12-
// Conf: Used for access to configuration
13-
var Data = getConfig()
14-
153
type Config struct {
164
GNodeB struct {
175
ControlIF struct {
18-
Ip string `yaml: "ip"`
19-
Port int `yaml: "port"`
20-
} `yaml: "controlif"`
6+
Ip string `yaml:"ip"`
7+
Port int `yaml:"port"`
8+
} `yaml:"controlif"`
219
DataIF struct {
22-
Ip string `yaml: "ip"`
23-
Port int `yaml: "port"`
24-
} `yaml: "dataif"`
10+
Ip string `yaml:"ip"`
11+
Port int `yaml:"port"`
12+
} `yaml:"dataif"`
2513
PlmnList struct {
26-
Mcc string `yaml: "mmc"`
27-
Mnc string `yaml: "mnc"`
28-
Tac string `yaml: "tac"`
29-
GnbId string `yaml: "gnbid"`
30-
} `yaml: "plmnlist"`
14+
Mcc string `yaml:"mmc"`
15+
Mnc string `yaml:"mnc"`
16+
Tac string `yaml:"tac"`
17+
GnbId string `yaml:"gnbid"`
18+
} `yaml:"plmnlist"`
3119
SliceSupportList struct {
32-
Sst string `yaml: "sst"`
33-
Sd string `yaml: "sd"`
34-
} `yaml: "slicesupportlist"`
20+
Sst string `yaml:"sst"`
21+
Sd string `yaml:"sd"`
22+
} `yaml:"slicesupportlist"`
3523
} `yaml:"gnodeb"`
3624
Ue struct {
37-
Msin string `yaml: "msin"`
38-
Key string `yaml: "key"`
39-
Opc string `yaml: "opc"`
40-
Amf string `yaml: "amf"`
41-
Sqn string `yaml: "sqn"`
25+
Msin string `yaml:"msin"`
26+
Key string `yaml:"key"`
27+
Opc string `yaml:"opc"`
28+
Amf string `yaml:"amf"`
29+
Sqn string `yaml:"sqn"`
4230
Hplmn struct {
43-
Mcc string `yaml: "mcc"`
44-
Mnc string `yaml: "mnc"`
45-
} `yaml: "hplmn"`
31+
Mcc string `yaml:"mcc"`
32+
Mnc string `yaml:"mnc"`
33+
} `yaml:"hplmn"`
4634
Snssai struct {
47-
Sst int `yaml: "sst"`
48-
Sd string `yaml: "sd"`
49-
} `yaml: "snssai"`
35+
Sst int `yaml:"sst"`
36+
Sd string `yaml:"sd"`
37+
} `yaml:"snssai"`
5038
} `yaml:"ue"`
5139
AMF struct {
52-
Ip string `yaml: "ip"`
53-
Port int `yaml: "port"`
54-
Name string `yaml: "name"`
40+
Ip string `yaml:"ip"`
41+
Port int `yaml:"port"`
42+
Name string `yaml:"name"`
5543
} `yaml:"amfif"`
5644
}
57-
58-
func RootDir() string {
59-
_, b, _, _ := runtime.Caller(0)
60-
d := path.Join(path.Dir(b))
61-
return filepath.Dir(d)
62-
}
63-
64-
func getConfig() Config {
65-
var cfg = Config{}
66-
Ddir := RootDir()
67-
configPath, err := filepath.Abs(Ddir + "/config/config.yml")
68-
log.Debug(configPath)
69-
if err != nil {
70-
log.Fatal("Could not find config in: ", configPath)
71-
}
72-
file, err := ioutil.ReadFile(configPath)
73-
err = yaml.Unmarshal([]byte(file), &cfg)
74-
if err != nil {
75-
log.Fatal("Could not read file in: ", configPath)
76-
}
77-
78-
return cfg
79-
}
80-
81-
func GetConfig() (Config, error) {
82-
var cfg = Config{}
83-
Ddir := RootDir()
84-
configPath, err := filepath.Abs(Ddir + "/config/config.yml")
85-
log.Debug(configPath)
86-
if err != nil {
87-
return Config{}, nil
88-
}
89-
file, err := ioutil.ReadFile(configPath)
90-
err = yaml.Unmarshal([]byte(file), &cfg)
91-
if err != nil {
92-
return Config{}, nil
93-
}
94-
95-
return cfg, nil
96-
}

0 commit comments

Comments
 (0)