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