-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[simulator][machine][config] Init reading config
- Add machine simulator config struct, which supports: num, start, end, step - add util function for read config file in test (TODO: don't know if it will have sideffect if is called multiple times)
- Loading branch information
Showing
6 changed files
with
73 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package config | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
// MachineSimulatorConfig defines the configurable property for the machine simulator | ||
type MachineSimulatorConfig struct { | ||
Num int | ||
Start time.Time | ||
End time.Time | ||
Step int | ||
} | ||
|
||
// ReadMachineSimulatorConfigFromViper return a config struct using configuration in yml | ||
func ReadMachineSimulatorConfigFromViper() *MachineSimulatorConfig { | ||
c := &MachineSimulatorConfig{} | ||
c.Num = viper.GetInt("simulator.machine.num") | ||
c.Start = viper.GetTime("simulator.machine.start") | ||
c.End = viper.GetTime("simulator.machine.end") | ||
c.Step = viper.GetInt("simulator.machine.step") | ||
return c | ||
} |
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,12 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
"github.com/xephonhq/xephon-b/pkg/util" | ||
) | ||
|
||
func TestReadMachineSimulatorConfigFromViper(t *testing.T) { | ||
util.ViperReadTestConfig() | ||
c := ReadMachineSimulatorConfigFromViper() | ||
t.Log(c) | ||
} |
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,24 @@ | ||
package util | ||
|
||
// Test util | ||
|
||
import ( | ||
"path" | ||
"runtime" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
// ViperReadTestConfig | ||
func ViperReadTestConfig() { | ||
// Get current file path https://coderwall.com/p/_fmbug/go-get-path-to-current-file | ||
_, filename, _, ok := runtime.Caller(1) | ||
if !ok { | ||
log.Fatal("can't get current file path") | ||
} | ||
|
||
filePath := path.Join(path.Dir(filename), "../../xephon-b.yml") | ||
log.Debug(filePath) | ||
viper.SetConfigFile(filePath) | ||
viper.ReadInConfig() | ||
} |
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,6 +1,11 @@ | ||
ww# The example xephon-b configuration file | ||
# The example xephon-b configuration file | ||
# TODO: might be split to several config files like ansible | ||
simulator: | ||
machine: | ||
num: 100 | ||
start: | ||
# start time in ISO 8601 | ||
start: 1997-07-16T19:20:30.45+01:00 | ||
# end time in ISO 8601 | ||
end: 1997-07-16T19:20:31.45+01:00 | ||
# in second | ||
step: 10 |