misc.go 1.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cmd

import (
	"errors"
	"log"
	"net"
	"net/url"
	"strings"
)

// TODO: broken
func IPSliceToStrings(ip interface{}) []string {
	if v, ok := ip.([]net.IP); ok {
		iplist := make([]string, 0, len(v))
		for _, ip := range v {
			iplist = append(iplist, ip.String())
		}
		return iplist
	} else {
		log.Fatal(ok)
	}
	log.Fatal("Unable to convert ip addresses")
	return nil
}

// URLSlice stores urls from cmdline arguments
type URLSlice []*url.URL

func (u *URLSlice) String() string {
	urls := make([]string, 0, len(*u))
	for _, uri := range *u {
		urls = append(urls, uri.String())
	}
	return strings.Join(urls, ",")
}

func (u *URLSlice) Set(rawurl string) error {
	for _, singleurl := range strings.Split(rawurl, ",") {
		uri, err := url.Parse(singleurl)
		if err != nil {
			return err
		}
		*u = append(*u, uri)
	}
	return nil
}

func (u *URLSlice) Type() string {
	return "URLSlice"
}

// IPSlice stores ip addresses from cmdline arguments
type IPSLice []*net.IP

func (i *IPSLice) String() string {
	ips := make([]string, 0, len(*i))
	for _, ip := range *i {
		ips = append(ips, ip.String())
	}
	return strings.Join(ips, ",")
}

func (i *IPSLice) Set(ipstring string) error {
	for _, singleip := range strings.Split(ipstring, ",") {
		ip := net.ParseIP(singleip)
		if ip == nil {
			return errors.New("Unable to parse IP " + singleip)
		}
		*i = append(*i, &ip)
	}
	return nil
}

func (i *IPSLice) Type() string {
	return "IPSlice"
}