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