forked from my5G/my5G-RANTester
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
78c3216
commit e9950b2
Showing
13 changed files
with
275 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.