diff --git a/home/upgrade.go b/home/upgrade.go index 9445e8b1..3a703ebc 100644 --- a/home/upgrade.go +++ b/home/upgrade.go @@ -11,7 +11,7 @@ import ( yaml "gopkg.in/yaml.v2" ) -const currentSchemaVersion = 5 // used for upgrading from old configs to new config +const currentSchemaVersion = 6 // used for upgrading from old configs to new config // Performs necessary upgrade operations if needed func upgradeConfig() error { @@ -82,6 +82,12 @@ func upgradeConfigSchema(oldVersion int, diskConfig *map[string]interface{}) err if err != nil { return err } + fallthrough + case 5: + err := upgradeSchema5to6(diskConfig) + if err != nil { + return err + } default: err := fmt.Errorf("configuration file contains unknown schema_version, abort") log.Println(err) @@ -268,3 +274,72 @@ func upgradeSchema4to5(diskConfig *map[string]interface{}) error { (*diskConfig)["users"] = users return nil } + +// clients: +// ... +// ip: 127.0.0.1 +// mac: ... +// +// -> +// +// clients: +// ... +// ids: +// - 127.0.0.1 +// - ... +func upgradeSchema5to6(diskConfig *map[string]interface{}) error { + log.Printf("%s(): called", _Func()) + + (*diskConfig)["schema_version"] = 6 + + clients, ok := (*diskConfig)["clients"] + if !ok { + return nil + } + + switch arr := clients.(type) { + case []interface{}: + + for i := range arr { + + switch c := arr[i].(type) { + + case map[interface{}]interface{}: + _ip, ok := c["ip"] + ids := []string{} + if ok { + ip, ok := _ip.(string) + if !ok { + log.Fatalf("client.ip is not a string: %v", _ip) + return nil + } + if len(ip) != 0 { + ids = append(ids, ip) + } + } + + _mac, ok := c["mac"] + if ok { + mac, ok := _mac.(string) + if !ok { + log.Fatalf("client.mac is not a string: %v", _mac) + return nil + } + if len(mac) != 0 { + ids = append(ids, mac) + } + } + + c["ids"] = ids + + default: + continue + } + } + + default: + return nil + } + + return nil +}