From d01892150eed9d58210eb40b7c005d5fa8e93238 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Sat, 1 Oct 2016 21:56:29 +0200 Subject: rework program flow This commit is a complete rebuild of pkictl. Before everything was all over the place and adding new commands was kind of a hassle. Now each command has its own file and can be adjusted on a command basis. Options are still used by the same name, but can now use different descriptions. --- type.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 type.go (limited to 'type.go') diff --git a/type.go b/type.go new file mode 100644 index 0000000..c64d98d --- /dev/null +++ b/type.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "net" + "strings" +) + +type ( + ipList []net.IP + stringList []string +) + +// return list of IPs as comma separated list +func (il *ipList) String() string { + var res string + list := ([]net.IP)(*il) + for i := range list { + if i > 0 { + res += ", " + } + res += list[i].String() + } + return res +} + +// receive multiple ip lists as comma separated strings +func (il *ipList) Set(value string) error { + if len(value) == 0 { + return nil + } + var ip net.IP + + parts := strings.Split(value, ",") + for i := range parts { + ip = net.ParseIP(strings.Trim(parts[i], " \t")) + if ip == nil { + // TODO encapsulate error in meaningfull error + return fmt.Errorf("not a valid IP") + } + *il = append(*il, ip) + } + return nil +} + +// return string list as a comma separated list +func (al *stringList) String() string { + return strings.Join(([]string)(*al), ", ") +} + +// receive multiple string lists as comma separated strings +func (al *stringList) Set(value string) error { + if len(value) == 0 { + return nil + } + parts := strings.Split(value, ",") + for i := range parts { + *al = append(*al, strings.Trim(parts[i], " \t")) + } + return nil +} -- cgit v1.2.3-70-g09d2