Skip to content

Commit

Permalink
#704 Use viper to unmarshal config struct directly
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Jan 17, 2019
1 parent f989a91 commit 31504eb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 133 deletions.
2 changes: 1 addition & 1 deletion hoverctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func initConfig() {
if config.GetTarget(config.DefaultTarget) == nil {
fmt.Printf("Default target `%v` not found, changing default target to `local`", config.DefaultTarget)
config.DefaultTarget = "local"

}

// TODO there could be bug here
target = config.GetTarget(targetNameFlag)
if targetNameFlag == "" && target == nil {
target = configuration.NewDefaultTarget()
Expand Down
24 changes: 21 additions & 3 deletions hoverctl/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,28 @@ func GetConfig() *Config {
}
}

return &Config{
DefaultTarget: viper.GetString("default"),
Targets: getTargetsFromConfig(viper.GetStringMap("targets")),
config := &Config{}
err = viper.Unmarshal(config)

if err != nil {
log.Debug("Error parsing config")
log.Debug(err.Error())
}

if config.DefaultTarget == "" {
config.DefaultTarget = viper.GetString("default")
}

if config.Targets == nil {
config.Targets = map[string]Target{}
}

if config.Targets["local"] == (Target{}) {
localTarget := NewDefaultTarget()
config.Targets["local"] = *localTarget
}

return config
}

func (this *Config) GetTarget(targetName string) *Target {
Expand Down
37 changes: 0 additions & 37 deletions hoverctl/configuration/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,43 +67,6 @@ func NewTarget(name, host string, adminPort, proxyPort int) *Target {
return target
}

func getTargetsFromConfig(configTargets map[string]interface{}) map[string]Target {
targets := map[string]Target{}

for key, target := range configTargets {
targetMap := target.(map[interface{}]interface{})

targetHoverfly := Target{}

targetHoverfly.Name = key

if targetMap["host"] != nil {
targetHoverfly.Host = targetMap["host"].(string)
}

if targetMap["admin.port"] != nil {
targetHoverfly.AdminPort = targetMap["admin.port"].(int)
}

if targetMap["proxy.port"] != nil {
targetHoverfly.ProxyPort = targetMap["proxy.port"].(int)
}

if targetMap["auth.token"] != nil {
targetHoverfly.AuthToken = targetMap["auth.token"].(string)
}

targets[key] = targetHoverfly
}

if targets["local"] == (Target{}) {
localTarget := NewDefaultTarget()
targets["local"] = *localTarget
}

return targets
}

func (this Target) BuildFlags() Flags {
flags := Flags{}

Expand Down
92 changes: 0 additions & 92 deletions hoverctl/configuration/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,98 +61,6 @@ func Test_NewTarget_OverridesProxyPortfNotEmpty(t *testing.T) {
}))
}

func Test_getTargetsFromConfig_host(t *testing.T) {
RegisterTestingT(t)

targets := getTargetsFromConfig(map[string]interface{}{
"newtarget": map[interface{}]interface{}{
"host": "test.org",
},
})

Expect(targets).To(HaveKeyWithValue("newtarget", Target{
Name: "newtarget",
Host: "test.org",
}))
}

func Test_getTargetsFromConfig_adminport(t *testing.T) {
RegisterTestingT(t)

targets := getTargetsFromConfig(map[string]interface{}{
"other": map[interface{}]interface{}{
"admin.port": 1234,
},
})

Expect(targets).To(HaveKeyWithValue("other", Target{
Name: "other",
AdminPort: 1234,
}))
}

func Test_getTargetsFromConfig_proxyport(t *testing.T) {
RegisterTestingT(t)

targets := getTargetsFromConfig(map[string]interface{}{
"otherother": map[interface{}]interface{}{
"proxy.port": 8765,
},
})

Expect(targets).To(HaveKeyWithValue("otherother", Target{
Name: "otherother",
ProxyPort: 8765,
}))
}

func Test_getTargetsFromConfig_authtoken(t *testing.T) {
RegisterTestingT(t)

targets := getTargetsFromConfig(map[string]interface{}{
"anotherother": map[interface{}]interface{}{
"auth.token": "token123:456",
},
})

Expect(targets).To(HaveKeyWithValue("anotherother", Target{
Name: "anotherother",
AuthToken: "token123:456",
}))
}

func Test_getTargetsFromConfig_AddsLocal(t *testing.T) {
RegisterTestingT(t)

targets := getTargetsFromConfig(map[string]interface{}{})

Expect(targets).To(HaveKeyWithValue("local", Target{
Name: "local",
Host: "localhost",
AdminPort: 8888,
ProxyPort: 8500,
}))
}

func Test_getTargetsFromConfig_DoesNotOverwriteLocal(t *testing.T) {
RegisterTestingT(t)

targets := getTargetsFromConfig(map[string]interface{}{
"local": map[interface{}]interface{}{
"host": "notlocalhost",
"admin.port": 1234,
"proxy.port": 4321,
},
})

Expect(targets).To(HaveKeyWithValue("local", Target{
Name: "local",
Host: "notlocalhost",
AdminPort: 1234,
ProxyPort: 4321,
}))
}

func Test_Target_BuildFlags_AdminPortSetsTheApFlag(t *testing.T) {
RegisterTestingT(t)

Expand Down

0 comments on commit 31504eb

Please sign in to comment.