2015-12-22 01:05:55 +00:00
|
|
|
package opts
|
2014-02-12 04:04:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-04 23:14:43 +00:00
|
|
|
"strconv"
|
2014-04-30 22:46:56 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2014-02-12 04:04:39 +00:00
|
|
|
)
|
|
|
|
|
2016-07-20 06:58:32 +00:00
|
|
|
// ReadKVStrings reads a file of line terminated key=value pairs, and overrides any keys
|
2016-04-05 12:53:04 +00:00
|
|
|
// present in the file with additional pairs specified in the override parameter
|
2016-07-20 06:58:32 +00:00
|
|
|
func ReadKVStrings(files []string, override []string) ([]string, error) {
|
2015-01-07 00:04:10 +00:00
|
|
|
envVariables := []string{}
|
|
|
|
for _, ef := range files {
|
2015-12-29 16:28:21 +00:00
|
|
|
parsedVars, err := ParseEnvFile(ef)
|
2015-01-07 00:04:10 +00: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 18:59:14 +00:00
|
|
|
// ConvertKVStringsToMap converts ["key=value"] to {"key":"value"}
|
|
|
|
func ConvertKVStringsToMap(values []string) map[string]string {
|
2015-01-07 00:04:10 +00: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 13:46:04 +00: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 19:23:54 +00:00
|
|
|
// ParseRestartPolicy returns the parsed policy or an error indicating what is incorrect
|
2015-12-18 18:36:17 +00:00
|
|
|
func ParseRestartPolicy(policy string) (container.RestartPolicy, error) {
|
|
|
|
p := container.RestartPolicy{}
|
2014-08-04 23:14:43 +00:00
|
|
|
|
|
|
|
if policy == "" {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2016-06-28 22:33:55 +00:00
|
|
|
parts := strings.Split(policy, ":")
|
2014-08-04 23:14:43 +00:00
|
|
|
|
2016-06-28 22:33:55 +00: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 09:33:33 +00:00
|
|
|
}
|
2014-08-04 23:14:43 +00:00
|
|
|
|
2016-06-28 22:33:55 +00:00
|
|
|
p.MaximumRetryCount = count
|
2014-08-04 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 22:33:55 +00:00
|
|
|
p.Name = parts[0]
|
|
|
|
|
2014-08-04 23:14:43 +00:00
|
|
|
return p, nil
|
|
|
|
}
|