2017-04-17 18:08:24 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-26 12:45:29 -05:00
|
|
|
"os"
|
2017-04-17 18:08:24 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadKVStrings reads a file of line terminated key=value pairs, and overrides any keys
|
|
|
|
// present in the file with additional pairs specified in the override parameter
|
|
|
|
func ReadKVStrings(files []string, override []string) ([]string, error) {
|
2018-01-26 12:45:29 -05:00
|
|
|
return readKVStrings(files, override, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadKVEnvStrings reads a file of line terminated key=value pairs, and overrides any keys
|
|
|
|
// present in the file with additional pairs specified in the override parameter.
|
|
|
|
// If a key has no value, it will get the value from the environment.
|
|
|
|
func ReadKVEnvStrings(files []string, override []string) ([]string, error) {
|
2018-07-01 17:55:38 -04:00
|
|
|
return readKVStrings(files, override, os.LookupEnv)
|
2018-01-26 12:45:29 -05:00
|
|
|
}
|
|
|
|
|
2018-07-01 17:55:38 -04:00
|
|
|
func readKVStrings(files []string, override []string, emptyFn func(string) (string, bool)) ([]string, error) {
|
2020-06-26 08:37:26 -04:00
|
|
|
var variables []string
|
2017-04-17 18:08:24 -04:00
|
|
|
for _, ef := range files {
|
2018-01-26 12:45:29 -05:00
|
|
|
parsedVars, err := parseKeyValueFile(ef, emptyFn)
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-26 12:45:29 -05:00
|
|
|
variables = append(variables, parsedVars...)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
// parse the '-e' and '--env' after, to allow override
|
2018-01-26 12:45:29 -05:00
|
|
|
variables = append(variables, override...)
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2018-01-26 12:45:29 -05:00
|
|
|
return variables, nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertKVStringsToMap converts ["key=value"] to {"key":"value"}
|
|
|
|
func ConvertKVStringsToMap(values []string) map[string]string {
|
|
|
|
result := make(map[string]string, len(values))
|
|
|
|
for _, value := range values {
|
2022-12-27 10:24:23 -05:00
|
|
|
k, v, _ := strings.Cut(value, "=")
|
|
|
|
result[k] = v
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-07-13 06:29:49 -04:00
|
|
|
//
|
|
|
|
// FOO= and FOO
|
|
|
|
//
|
2017-04-17 18:08:24 -04:00
|
|
|
// 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 {
|
2022-12-27 10:24:23 -05:00
|
|
|
k, v, ok := strings.Cut(value, "=")
|
|
|
|
if !ok {
|
|
|
|
result[k] = nil
|
2017-04-17 18:08:24 -04:00
|
|
|
} else {
|
2022-12-27 10:24:23 -05:00
|
|
|
result[k] = &v
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRestartPolicy returns the parsed policy or an error indicating what is incorrect
|
|
|
|
func ParseRestartPolicy(policy string) (container.RestartPolicy, error) {
|
|
|
|
if policy == "" {
|
2023-08-28 05:46:49 -04:00
|
|
|
// for backward-compatibility, we don't set the default ("no")
|
|
|
|
// policy here, because older versions of the engine may not
|
|
|
|
// support it.
|
|
|
|
return container.RestartPolicy{}, nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-08-28 05:46:49 -04:00
|
|
|
p := container.RestartPolicy{}
|
|
|
|
k, v, ok := strings.Cut(policy, ":")
|
|
|
|
if ok && k == "" {
|
|
|
|
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: no policy provided before colon")
|
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
if v != "" {
|
|
|
|
count, err := strconv.Atoi(v)
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
2023-08-28 05:46:49 -04:00
|
|
|
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: maximum retry count must be an integer")
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
p.MaximumRetryCount = count
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:24:23 -05:00
|
|
|
p.Name = k
|
2017-04-17 18:08:24 -04:00
|
|
|
return p, nil
|
|
|
|
}
|