Skip to content

Commit

Permalink
Merge pull request #15882 from vdemeester/14756-lint-opts-trust
Browse files Browse the repository at this point in the history
Carry #14813 on linting package opts and trust
  • Loading branch information
cpuguy83 committed Aug 27, 2015
2 parents 0ca2c3a + 0bd016b commit ba019dc
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 55 deletions.
6 changes: 3 additions & 3 deletions daemon/config_unix.go
Expand Up @@ -70,10 +70,10 @@ func (config *Config) InstallFlags(cmd *flag.FlagSet, usageFn func(string) strin
cmd.StringVar(&config.Bridge.Iface, []string{"b", "-bridge"}, "", usageFn("Attach containers to a network bridge"))
cmd.StringVar(&config.Bridge.FixedCIDR, []string{"-fixed-cidr"}, "", usageFn("IPv4 subnet for fixed IPs"))
cmd.StringVar(&config.Bridge.FixedCIDRv6, []string{"-fixed-cidr-v6"}, "", usageFn("IPv6 subnet for fixed IPs"))
cmd.Var(opts.NewIpOpt(&config.Bridge.DefaultGatewayIPv4, ""), []string{"-default-gateway"}, usageFn("Container default gateway IPv4 address"))
cmd.Var(opts.NewIpOpt(&config.Bridge.DefaultGatewayIPv6, ""), []string{"-default-gateway-v6"}, usageFn("Container default gateway IPv6 address"))
cmd.Var(opts.NewIPOpt(&config.Bridge.DefaultGatewayIPv4, ""), []string{"-default-gateway"}, usageFn("Container default gateway IPv4 address"))
cmd.Var(opts.NewIPOpt(&config.Bridge.DefaultGatewayIPv6, ""), []string{"-default-gateway-v6"}, usageFn("Container default gateway IPv6 address"))
cmd.BoolVar(&config.Bridge.InterContainerCommunication, []string{"#icc", "-icc"}, true, usageFn("Enable inter-container communication"))
cmd.Var(opts.NewIpOpt(&config.Bridge.DefaultIP, "0.0.0.0"), []string{"#ip", "-ip"}, usageFn("Default IP when binding container ports"))
cmd.Var(opts.NewIPOpt(&config.Bridge.DefaultIP, "0.0.0.0"), []string{"#ip", "-ip"}, usageFn("Default IP when binding container ports"))
cmd.BoolVar(&config.Bridge.EnableUserlandProxy, []string{"-userland-proxy"}, true, usageFn("Use userland proxy for loopback traffic"))
cmd.BoolVar(&config.EnableCors, []string{"#api-enable-cors", "#-api-enable-cors"}, false, usageFn("Enable CORS headers in the remote API, this is deprecated by --api-cors-header"))
cmd.StringVar(&config.CorsHeaders, []string{"-api-cors-header"}, "", usageFn("Set CORS headers in the remote API"))
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon.go
Expand Up @@ -669,7 +669,7 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo
if err := system.MkdirAll(trustDir, 0700); err != nil {
return nil, err
}
trustService, err := trust.NewTrustStore(trustDir)
trustService, err := trust.NewStore(trustDir)
if err != nil {
return nil, fmt.Errorf("could not create trust store: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions graph/tags.go
Expand Up @@ -40,7 +40,7 @@ type TagStore struct {
pushingPool map[string]chan struct{}
registryService *registry.Service
eventsService *events.Events
trustService *trust.TrustStore
trustService *trust.Store
}

// Repository maps tags to image IDs.
Expand Down Expand Up @@ -77,7 +77,7 @@ type TagStoreConfig struct {
// Events is the events service to use for logging.
Events *events.Events
// Trust is the trust service to use for push and pull operations.
Trust *trust.TrustStore
Trust *trust.Store
}

// NewTagStore creates a new TagStore at specified path, using the parameters
Expand Down
2 changes: 1 addition & 1 deletion graph/tags_unit_test.go
Expand Up @@ -63,7 +63,7 @@ func mkTestTagStore(root string, t *testing.T) *TagStore {
t.Fatal(err)
}

trust, err := trust.NewTrustStore(root + "/trust")
trust, err := trust.NewStore(root + "/trust")
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions hack/make/validate-lint
Expand Up @@ -44,6 +44,7 @@ packages=(
graph/tags
image
integration-cli
opts
pkg/archive
pkg/broadcastwriter
pkg/chrootarchive
Expand Down Expand Up @@ -97,6 +98,7 @@ packages=(
pkg/version
registry
runconfig
trust
utils
volume
volume/local
Expand Down
4 changes: 2 additions & 2 deletions opts/envfile.go
Expand Up @@ -9,13 +9,13 @@ import (
)

var (
// EnvironmentVariableRegexp A regexp to validate correct environment variables
// EnvironmentVariableRegexp is a regexp to validate correct environment variables
// Environment variables set by the user must have a name consisting solely of
// alphabetics, numerics, and underscores - the first of which must not be numeric.
EnvironmentVariableRegexp = regexp.MustCompile("^[[:alpha:]_][[:alpha:][:digit:]_]*$")
)

// ParseEnvFile Read in a line delimited file with environment variables enumerated
// ParseEnvFile reads a file with environment variables enumerated by lines
func ParseEnvFile(filename string) ([]string, error) {
fh, err := os.Open(filename)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions opts/hosts_unix.go
Expand Up @@ -4,4 +4,5 @@ package opts

import "fmt"

// DefaultHost constant defines the default host string used by docker on other hosts than Windows
var DefaultHost = fmt.Sprintf("unix://%s", DefaultUnixSocket)
1 change: 1 addition & 0 deletions opts/hosts_windows.go
Expand Up @@ -4,4 +4,5 @@ package opts

import "fmt"

// DefaultHost constant defines the default host string used by docker on Windows
var DefaultHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultHTTPPort)
19 changes: 13 additions & 6 deletions opts/ip.go
Expand Up @@ -5,20 +5,25 @@ import (
"net"
)

// IpOpt type that hold an IP
type IpOpt struct {
// IPOpt holds an IP. It is used to store values from CLI flags.
type IPOpt struct {
*net.IP
}

func NewIpOpt(ref *net.IP, defaultVal string) *IpOpt {
o := &IpOpt{
// 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
}

func (o *IpOpt) Set(val string) error {
// Set sets an IPv4 or IPv6 address from a given string. If the given
// string is not parsable 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)
Expand All @@ -27,7 +32,9 @@ func (o *IpOpt) Set(val string) error {
return nil
}

func (o *IpOpt) String() string {
// 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 ""
}
Expand Down
12 changes: 6 additions & 6 deletions opts/ip_test.go
Expand Up @@ -10,7 +10,7 @@ func TestIpOptString(t *testing.T) {
var ip net.IP

for _, address := range addresses {
stringAddress := NewIpOpt(&ip, address).String()
stringAddress := NewIPOpt(&ip, address).String()
if stringAddress != address {
t.Fatalf("IpOpt string should be `%s`, not `%s`", address, stringAddress)
}
Expand All @@ -21,7 +21,7 @@ func TestNewIpOptInvalidDefaultVal(t *testing.T) {
ip := net.IPv4(127, 0, 0, 1)
defaultVal := "Not an ip"

ipOpt := NewIpOpt(&ip, defaultVal)
ipOpt := NewIPOpt(&ip, defaultVal)

expected := "127.0.0.1"
if ipOpt.String() != expected {
Expand All @@ -33,7 +33,7 @@ func TestNewIpOptValidDefaultVal(t *testing.T) {
ip := net.IPv4(127, 0, 0, 1)
defaultVal := "192.168.1.1"

ipOpt := NewIpOpt(&ip, defaultVal)
ipOpt := NewIPOpt(&ip, defaultVal)

expected := "192.168.1.1"
if ipOpt.String() != expected {
Expand All @@ -43,11 +43,11 @@ func TestNewIpOptValidDefaultVal(t *testing.T) {

func TestIpOptSetInvalidVal(t *testing.T) {
ip := net.IPv4(127, 0, 0, 1)
ipOpt := &IpOpt{IP: &ip}
ipOpt := &IPOpt{IP: &ip}

invalidIp := "invalid ip"
invalidIP := "invalid ip"
expectedError := "invalid ip is not an ip address"
err := ipOpt.Set(invalidIp)
err := ipOpt.Set(invalidIP)
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected an Error with [%v], got [%v]", expectedError, err.Error())
}
Expand Down
53 changes: 28 additions & 25 deletions opts/opts.go
Expand Up @@ -27,18 +27,19 @@ var (
DefaultUnixSocket = "/var/run/docker.sock"
)

// ListOpts type that hold a list of values and a validation function.
// ListOpts holds a list of values and a validation function.
type ListOpts struct {
values *[]string
validator ValidatorFctType
}

// NewListOpts Create a new ListOpts with the specified validator.
// NewListOpts creates a new ListOpts with the specified validator.
func NewListOpts(validator ValidatorFctType) ListOpts {
var values []string
return *NewListOptsRef(&values, validator)
}

// NewListOptsRef creates a new ListOpts with the specified values and validator.
func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
return &ListOpts{
values: values,
Expand All @@ -64,7 +65,7 @@ func (opts *ListOpts) Set(value string) error {
return nil
}

// Delete remove the given element from the slice.
// Delete removes the specified element from the slice.
func (opts *ListOpts) Delete(key string) {
for i, k := range *opts.values {
if k == key {
Expand All @@ -85,13 +86,13 @@ func (opts *ListOpts) GetMap() map[string]struct{} {
return ret
}

// GetAll returns the values' slice.
// GetAll returns the values of slice.
// FIXME: Can we remove this?
func (opts *ListOpts) GetAll() []string {
return (*opts.values)
}

// Get checks the existence of the given key.
// Get checks the existence of the specified key.
func (opts *ListOpts) Get(key string) bool {
for _, k := range *opts.values {
if k == key {
Expand All @@ -106,7 +107,7 @@ func (opts *ListOpts) Len() int {
return len((*opts.values))
}

//MapOpts type that holds a map of values and a validation function.
//MapOpts holds a map of values and a validation function.
type MapOpts struct {
values map[string]string
validator ValidatorFctType
Expand All @@ -131,6 +132,7 @@ func (opts *MapOpts) Set(value string) error {
return nil
}

// GetAll returns the values of MapOpts as a map.
func (opts *MapOpts) GetAll() map[string]string {
return opts.values
}
Expand All @@ -139,6 +141,7 @@ func (opts *MapOpts) String() string {
return fmt.Sprintf("%v", map[string]string((opts.values)))
}

// NewMapOpts creates a new MapOpts with the specified map of values and a validator.
func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
if values == nil {
values = make(map[string]string)
Expand All @@ -149,13 +152,13 @@ func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
}
}

// ValidatorFctType validator that return a validate string and/or an error
// ValidatorFctType defines a validator function that returns a validated string and/or an error.
type ValidatorFctType func(val string) (string, error)

// ValidatorFctListType validator that return a validate list of string and/or an error
// ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
type ValidatorFctListType func(val string) ([]string, error)

// ValidateAttach Validates that the specified string is a valid attach option.
// ValidateAttach validates that the specified string is a valid attach option.
func ValidateAttach(val string) (string, error) {
s := strings.ToLower(val)
for _, str := range []string{"stdin", "stdout", "stderr"} {
Expand All @@ -166,7 +169,7 @@ func ValidateAttach(val string) (string, error) {
return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR")
}

// ValidateLink Validates that the specified string has a valid link format (containerName:alias).
// ValidateLink validates that the specified string has a valid link format (containerName:alias).
func ValidateLink(val string) (string, error) {
if _, _, err := parsers.ParseLink(val); err != nil {
return val, err
Expand Down Expand Up @@ -194,18 +197,18 @@ func ValidDeviceMode(mode string) bool {
return true
}

// ValidateDevice Validate a path for devices
// ValidateDevice validates a path for devices
// It will make sure 'val' is in the form:
// [host-dir:]container-path[:mode]
// It will also validate the device mode.
// It also validates the device mode.
func ValidateDevice(val string) (string, error) {
return validatePath(val, ValidDeviceMode)
}

// ValidatePath Validate a path for volumes
// ValidatePath validates a path for volumes
// It will make sure 'val' is in the form:
// [host-dir:]container-path[:rw|ro]
// It will also validate the mount mode.
// It also validates the mount mode.
func ValidatePath(val string) (string, error) {
return validatePath(val, volume.ValidMountMode)
}
Expand Down Expand Up @@ -250,8 +253,8 @@ func validatePath(val string, validator func(string) bool) (string, error) {
return val, nil
}

// ValidateEnv Validate an environment variable and returns it
// It will use EnvironmentVariableRegexp to ensure the name of the environment variable is valid.
// ValidateEnv validates an environment variable and returns it.
// It uses EnvironmentVariableRegexp to ensure the name of the environment variable is valid.
// If no value is specified, it returns the current value using os.Getenv.
func ValidateEnv(val string) (string, error) {
arr := strings.Split(val, "=")
Expand All @@ -267,7 +270,7 @@ func ValidateEnv(val string) (string, error) {
return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
}

// ValidateIPAddress Validates an Ip address
// ValidateIPAddress validates an Ip address.
func ValidateIPAddress(val string) (string, error) {
var ip = net.ParseIP(strings.TrimSpace(val))
if ip != nil {
Expand All @@ -276,7 +279,7 @@ func ValidateIPAddress(val string) (string, error) {
return "", fmt.Errorf("%s is not an ip address", val)
}

// ValidateMACAddress Validates a MAC address
// ValidateMACAddress validates a MAC address.
func ValidateMACAddress(val string) (string, error) {
_, err := net.ParseMAC(strings.TrimSpace(val))
if err != nil {
Expand All @@ -285,8 +288,8 @@ func ValidateMACAddress(val string) (string, error) {
return val, nil
}

// ValidateDNSSearch Validates domain for resolvconf search configuration.
// A zero length domain is represented by .
// ValidateDNSSearch validates domain for resolvconf search configuration.
// A zero length domain is represented by a dot (.).
func ValidateDNSSearch(val string) (string, error) {
if val = strings.Trim(val, " "); val == "." {
return val, nil
Expand All @@ -305,8 +308,8 @@ func validateDomain(val string) (string, error) {
return "", fmt.Errorf("%s is not a valid domain", val)
}

// ValidateExtraHost Validate that the given string is a valid extrahost and returns it
// ExtraHost are in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6)
// ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
// ExtraHost are in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6).
func ValidateExtraHost(val string) (string, error) {
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
arr := strings.SplitN(val, ":", 2)
Expand All @@ -319,16 +322,16 @@ func ValidateExtraHost(val string) (string, error) {
return val, nil
}

// ValidateLabel Validate that the given string is a valid label, and returns it
// Labels are in the form on key=value
// ValidateLabel validates that the specified string is a valid label, and returns it.
// Labels are in the form on key=value.
func ValidateLabel(val string) (string, error) {
if strings.Count(val, "=") < 1 {
return "", fmt.Errorf("bad attribute format: %s", val)
}
return val, nil
}

// ValidateHost Validate that the given string is a valid host and returns it
// ValidateHost validates that the specified string is a valid host and returns it.
func ValidateHost(val string) (string, error) {
host, err := parsers.ParseHost(DefaultHTTPHost, DefaultUnixSocket, val)
if err != nil {
Expand Down

0 comments on commit ba019dc

Please sign in to comment.