Fix empty values serialization

This commit is contained in:
George
2020-05-15 09:54:57 -04:00
parent f627480007
commit da259bb4cb
2 changed files with 31 additions and 5 deletions
+2 -2
View File
@@ -107,13 +107,13 @@ func run_yggdrasil() {
// their configuration file with newly mapped names (like above) or to
// convert from plain JSON to commented HJSON.
if *normaliseconf {
fmt.Println(popura.SaveConfig(yggConfig, popConfig, *confjson))
fmt.Println(popura.SaveConfig(*yggConfig, *popConfig, *confjson))
return
}
case *genconf:
// Generate a new configuration and print it to stdout.
yggConfig, popConfig = popura.GenerateConfig()
fmt.Println(popura.SaveConfig(yggConfig, popConfig, *confjson))
fmt.Println(popura.SaveConfig(*yggConfig, *popConfig, *confjson))
return
default:
// No flags were provided, therefore print the list of flags to stdout.
+29 -3
View File
@@ -26,13 +26,39 @@ func GenerateConfig() (*config.NodeConfig, *PopuraConfig) {
return config.GenerateConfig(), &popConfig
}
func SaveConfig(yggConfig *config.NodeConfig, popConfig *PopuraConfig, isjson bool) string {
// initialize empty values for correct JSON serialization
func correctEmptyValues(yggConfig *config.NodeConfig) {
if len(yggConfig.TunnelRouting.IPv4LocalSubnets) == 0 {
yggConfig.TunnelRouting.IPv4LocalSubnets = []string{}
}
if len(yggConfig.TunnelRouting.IPv6LocalSubnets) == 0 {
yggConfig.TunnelRouting.IPv6LocalSubnets = []string{}
}
if len(yggConfig.TunnelRouting.IPv4RemoteSubnets) == 0 {
yggConfig.TunnelRouting.IPv4RemoteSubnets = make(map[string]string)
}
if len(yggConfig.TunnelRouting.IPv6RemoteSubnets) == 0 {
yggConfig.TunnelRouting.IPv6RemoteSubnets = make(map[string]string)
}
if len(yggConfig.SessionFirewall.WhitelistEncryptionPublicKeys) == 0 {
yggConfig.SessionFirewall.WhitelistEncryptionPublicKeys = []string{}
}
if len(yggConfig.SessionFirewall.BlacklistEncryptionPublicKeys) == 0 {
yggConfig.SessionFirewall.BlacklistEncryptionPublicKeys = []string{}
}
if len(yggConfig.NodeInfo) == 0 {
yggConfig.NodeInfo = make(map[string]interface{})
}
}
func SaveConfig(yggConfig config.NodeConfig, popConfig PopuraConfig, isjson bool) string {
// combine config structs into one and marshal it
// FIXME hjson comments are lost
var combo map[string]interface{}
ybs, _ := json.Marshal(yggConfig)
pbs, _ := json.Marshal(popConfig)
correctEmptyValues(&yggConfig)
ybs, _ := json.Marshal(&yggConfig)
pbs, _ := json.Marshal(&popConfig)
json.Unmarshal(ybs, &combo)
json.Unmarshal(pbs, &combo)