2014-08-10 01:12:52 +00:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
2014-08-10 03:50:46 +00:00
|
|
|
"fmt"
|
2014-08-10 01:12:52 +00:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2015-08-27 07:33:21 +00:00
|
|
|
// IPOpt holds an IP. It is used to store values from CLI flags.
|
2015-07-21 16:15:36 +00:00
|
|
|
type IPOpt struct {
|
2014-08-10 01:12:52 +00:00
|
|
|
*net.IP
|
|
|
|
}
|
|
|
|
|
2015-08-27 07:33:21 +00:00
|
|
|
// NewIPOpt creates a new IPOpt from a reference net.IP and a
|
|
|
|
// string representation of an IP. If the string is not a valid
|
|
|
|
// IP it will fallback to the specified reference.
|
2015-07-21 16:15:36 +00:00
|
|
|
func NewIPOpt(ref *net.IP, defaultVal string) *IPOpt {
|
|
|
|
o := &IPOpt{
|
2014-08-10 01:12:52 +00:00
|
|
|
IP: ref,
|
|
|
|
}
|
|
|
|
o.Set(defaultVal)
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2015-07-21 16:15:36 +00:00
|
|
|
// Set sets an IPv4 or IPv6 address from a given string. If the given
|
2015-12-13 16:00:39 +00:00
|
|
|
// string is not parseable as an IP address it returns an error.
|
2015-07-21 16:15:36 +00:00
|
|
|
func (o *IPOpt) Set(val string) error {
|
2014-08-10 03:50:46 +00:00
|
|
|
ip := net.ParseIP(val)
|
|
|
|
if ip == nil {
|
2014-08-13 06:45:40 +00:00
|
|
|
return fmt.Errorf("%s is not an ip address", val)
|
2014-08-10 03:50:46 +00:00
|
|
|
}
|
2015-06-05 16:44:10 +00:00
|
|
|
*o.IP = ip
|
2014-08-10 01:12:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-27 07:33:21 +00:00
|
|
|
// String returns the IP address stored in the IPOpt. If stored IP is a
|
2015-07-21 16:15:36 +00:00
|
|
|
// nil pointer, it returns an empty string.
|
|
|
|
func (o *IPOpt) String() string {
|
2015-06-11 07:53:39 +00:00
|
|
|
if *o.IP == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2015-06-05 16:44:10 +00:00
|
|
|
return o.IP.String()
|
2014-08-10 01:12:52 +00:00
|
|
|
}
|
2016-06-21 20:42:47 +00:00
|
|
|
|
|
|
|
// Type returns the type of the option
|
|
|
|
func (o *IPOpt) Type() string {
|
|
|
|
return "ip"
|
|
|
|
}
|