mirror of https://github.com/docker/cli.git
opts: remove unused IPOpt option
This option was created Moby [6d59a566759da5729d7eb89a8e1888fc612f03cf], and used for the daemon config [353b7c8ec77b30fa83dac5ec0778193f6de8b437]. It was migrated from the Moby repository inf34ca0a354
, but was never used by the CLI, and there are no external consumers. If we would need an IP-address option, spf13/pflags now provides those, so there's no need to implement this ourselves. [6d59a566759da5729d7eb89a8e1888fc612f03cf]:6d59a56675
[353b7c8ec77b30fa83dac5ec0778193f6de8b437]:353b7c8ec7
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
79c5d4a329
commit
f0816bf679
47
opts/ip.go
47
opts/ip.go
|
@ -1,47 +0,0 @@
|
||||||
package opts
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
// IPOpt holds an IP. It is used to store values from CLI flags.
|
|
||||||
type IPOpt struct {
|
|
||||||
*net.IP
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
func NewIPOpt(ref *net.IP, defaultVal string) *IPOpt {
|
|
||||||
o := &IPOpt{
|
|
||||||
IP: ref,
|
|
||||||
}
|
|
||||||
o.Set(defaultVal)
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets an IPv4 or IPv6 address from a given string. If the given
|
|
||||||
// string is not parseable as an IP address it returns an error.
|
|
||||||
func (o *IPOpt) Set(val string) error {
|
|
||||||
ip := net.ParseIP(val)
|
|
||||||
if ip == nil {
|
|
||||||
return fmt.Errorf("%s is not an ip address", val)
|
|
||||||
}
|
|
||||||
*o.IP = ip
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns the IP address stored in the IPOpt. If stored IP is a
|
|
||||||
// nil pointer, it returns an empty string.
|
|
||||||
func (o *IPOpt) String() string {
|
|
||||||
if *o.IP == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return o.IP.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type returns the type of the option
|
|
||||||
func (o *IPOpt) Type() string {
|
|
||||||
return "ip"
|
|
||||||
}
|
|
|
@ -1,54 +0,0 @@
|
||||||
package opts
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestIpOptString(t *testing.T) {
|
|
||||||
addresses := []string{"", "0.0.0.0"}
|
|
||||||
var ip net.IP
|
|
||||||
|
|
||||||
for _, address := range addresses {
|
|
||||||
stringAddress := NewIPOpt(&ip, address).String()
|
|
||||||
if stringAddress != address {
|
|
||||||
t.Fatalf("IpOpt string should be `%s`, not `%s`", address, stringAddress)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewIpOptInvalidDefaultVal(t *testing.T) {
|
|
||||||
ip := net.IPv4(127, 0, 0, 1)
|
|
||||||
defaultVal := "Not an ip"
|
|
||||||
|
|
||||||
ipOpt := NewIPOpt(&ip, defaultVal)
|
|
||||||
|
|
||||||
expected := "127.0.0.1"
|
|
||||||
if ipOpt.String() != expected {
|
|
||||||
t.Fatalf("Expected [%v], got [%v]", expected, ipOpt.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewIpOptValidDefaultVal(t *testing.T) {
|
|
||||||
ip := net.IPv4(127, 0, 0, 1)
|
|
||||||
defaultVal := "192.168.1.1"
|
|
||||||
|
|
||||||
ipOpt := NewIPOpt(&ip, defaultVal)
|
|
||||||
|
|
||||||
expected := "192.168.1.1"
|
|
||||||
if ipOpt.String() != expected {
|
|
||||||
t.Fatalf("Expected [%v], got [%v]", expected, ipOpt.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIpOptSetInvalidVal(t *testing.T) {
|
|
||||||
ip := net.IPv4(127, 0, 0, 1)
|
|
||||||
ipOpt := &IPOpt{IP: &ip}
|
|
||||||
|
|
||||||
invalidIP := "invalid ip"
|
|
||||||
expectedError := "invalid ip is not an ip address"
|
|
||||||
err := ipOpt.Set(invalidIP)
|
|
||||||
if err == nil || err.Error() != expectedError {
|
|
||||||
t.Fatalf("Expected an Error with [%v], got [%v]", expectedError, err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue