2015-12-21 20:05:55 -05:00
|
|
|
package opts
|
2014-02-11 23:04:39 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-04 19:14:43 -04:00
|
|
|
"strconv"
|
2014-04-30 18:46:56 -04:00
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
2016-07-20 02:58:32 -04:00
|
|
|
// ReadKVStrings reads a file of line terminated key=value pairs, and overrides any keys
|
2016-04-05 08:53:04 -04:00
|
|
|
// present in the file with additional pairs specified in the override parameter
|
2016-07-20 02:58:32 -04:00
|
|
|
func ReadKVStrings(files []string, override []string) ([]string, error) {
|
2015-01-06 19:04:10 -05:00
|
|
|
envVariables := []string{}
|
|
|
|
for _, ef := range files {
|
2015-12-29 11:28:21 -05:00
|
|
|
parsedVars, err := ParseEnvFile(ef)
|
2015-01-06 19:04:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
envVariables = append(envVariables, parsedVars...)
|
|
|
|
}
|
|
|
|
// parse the '-e' and '--env' after, to allow override
|
|
|
|
envVariables = append(envVariables, override...)
|
|
|
|
|
|
|
|
return envVariables, nil
|
|
|
|
}
|
|
|
|
|
2014-11-14 13:59:14 -05:00
|
|
|
// ConvertKVStringsToMap converts ["key=value"] to {"key":"value"}
|
|
|
|
func ConvertKVStringsToMap(values []string) map[string]string {
|
2015-01-06 19:04:10 -05:00
|
|
|
result := make(map[string]string, len(values))
|
|
|
|
for _, value := range values {
|
|
|
|
kv := strings.SplitN(value, "=", 2)
|
|
|
|
if len(kv) == 1 {
|
|
|
|
result[kv[0]] = ""
|
|
|
|
} else {
|
|
|
|
result[kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-12-03 08:46:04 -05:00
|
|
|
// ConvertKVStringsToMapWithNil converts ["key=value"] to {"key":"value"}
|
|
|
|
// but set unset keys to nil - meaning the ones with no "=" in them.
|
|
|
|
// We use this in cases where we need to distinguish between
|
|
|
|
// FOO= and FOO
|
|
|
|
// where the latter case just means FOO was mentioned but not given a value
|
|
|
|
func ConvertKVStringsToMapWithNil(values []string) map[string]*string {
|
|
|
|
result := make(map[string]*string, len(values))
|
|
|
|
for _, value := range values {
|
|
|
|
kv := strings.SplitN(value, "=", 2)
|
|
|
|
if len(kv) == 1 {
|
|
|
|
result[kv[0]] = nil
|
|
|
|
} else {
|
|
|
|
result[kv[0]] = &kv[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:23:54 -04:00
|
|
|
// ParseRestartPolicy returns the parsed policy or an error indicating what is incorrect
|
2015-12-18 13:36:17 -05:00
|
|
|
func ParseRestartPolicy(policy string) (container.RestartPolicy, error) {
|
|
|
|
p := container.RestartPolicy{}
|
2014-08-04 19:14:43 -04:00
|
|
|
|
|
|
|
if policy == "" {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2016-06-28 18:33:55 -04:00
|
|
|
parts := strings.Split(policy, ":")
|
2014-08-04 19:14:43 -04:00
|
|
|
|
2016-06-28 18:33:55 -04:00
|
|
|
if len(parts) > 2 {
|
|
|
|
return p, fmt.Errorf("invalid restart policy format")
|
|
|
|
}
|
|
|
|
if len(parts) == 2 {
|
|
|
|
count, err := strconv.Atoi(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return p, fmt.Errorf("maximum retry count must be an integer")
|
2015-07-03 05:33:33 -04:00
|
|
|
}
|
2014-08-04 19:14:43 -04:00
|
|
|
|
2016-06-28 18:33:55 -04:00
|
|
|
p.MaximumRetryCount = count
|
2014-08-04 19:14:43 -04:00
|
|
|
}
|
|
|
|
|
2016-06-28 18:33:55 -04:00
|
|
|
p.Name = parts[0]
|
|
|
|
|
2014-08-04 19:14:43 -04:00
|
|
|
return p, nil
|
|
|
|
}
|