pve-go/ipconfig.go
2021-04-17 19:56:27 +02:00

81 lines
1.3 KiB
Go

// Copyright (C) 2020 Marius Schellenberger
package pve
import (
"strings"
)
type IPConfigs []*IPConfig
func (c *IPConfigs) Set(index int, ipc *IPConfig) {
l := len(*c)
if index >= l {
n := make(IPConfigs, index+1)
copy(n, *c)
n[index] = ipc
*c = n
return
}
if l == 0 {
*c = append(*c, ipc)
return
}
(*c)[index] = ipc
}
func (c *IPConfigs) Get(index int) *IPConfig {
if len(*c)-1 >= index {
return (*c)[index]
}
return nil
}
type IPConfig struct {
IPv4CIDR string
IPv4Gateway string
IPv6CIDR string
IPv6Gateway string
}
func (c *IPConfig) String() string {
var str []string
if c.IPv4CIDR != "" {
str = append(str, "ip="+c.IPv4CIDR)
if c.IPv4Gateway != "" {
str = append(str, "gw="+c.IPv4Gateway)
}
}
if c.IPv6CIDR != "" {
str = append(str, "ip6="+c.IPv6CIDR)
if c.IPv6Gateway != "" {
str = append(str, "gw6="+c.IPv6Gateway)
}
}
return strings.Join(str, ",")
}
func parseIPConfig(s string) (c *IPConfig) {
cfg := strings.Split(s, ",")
if len(cfg) == 0 {
return
}
c = new(IPConfig)
for _, val := range cfg {
v := strings.Split(val, "=")
if len(v) != 2 {
continue
}
switch v[0] {
case "ip":
c.IPv4CIDR = v[1]
case "gw":
c.IPv4Gateway = v[1]
case "ip6":
c.IPv6CIDR = v[1]
case "gw6":
c.IPv6Gateway = v[1]
}
}
return
}