diff options
author | Gibheer <gibheer+git@zero-knowledge.org> | 2016-10-01 21:56:29 +0200 |
---|---|---|
committer | Gibheer <gibheer+git@zero-knowledge.org> | 2016-10-01 21:56:29 +0200 |
commit | d01892150eed9d58210eb40b7c005d5fa8e93238 (patch) | |
tree | f9d37f3d5b4f0d9afd01755801826713f47d83c3 /type.go | |
parent | faaf7d8859895767b5e64d32c14d561d6fdb5a14 (diff) |
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.
Diffstat (limited to 'type.go')
-rw-r--r-- | type.go | 61 |
1 files changed, 61 insertions, 0 deletions
@@ -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 +} |