Merge component 'cli' from git@github.com:docker/cli master

This commit is contained in:
GordonTheTurtle
2018-08-01 16:36:51 +00:00
6 changed files with 176 additions and 36 deletions

View File

@ -1,8 +1,8 @@
# GitHub code owners
# See https://github.com/blog/2392-introducing-code-owners
cli/command/stack/** @vdemeester
cli/command/stack/** @vdemeester @silvin-lubecki
cli/compose/** @vdemeester
contrib/completion/bash/** @albers
contrib/completion/zsh/** @sdurrheimer
docs/** @mistyhacks @vdemeester @thaJeztah
docs/** @vdemeester @thaJeztah

View File

@ -41,7 +41,6 @@
# TODO Describe the docs maintainers role.
people = [
"misty",
"thajeztah"
]
@ -95,11 +94,6 @@
Email = "justin.cormack@docker.com"
GitHub = "justincormack"
[people.misty]
Name = "Misty Stanley-Jones"
Email = "misty@docker.com"
GitHub = "mistyhacks"
[people.programmerq]
Name = "Jeff Anderson"
Email = "jeff@docker.com"

View File

@ -93,25 +93,91 @@ func Substitute(template string, mapping Mapping) (string, error) {
return SubstituteWith(template, mapping, pattern, DefaultSubstituteFuncs...)
}
// ExtractVariables returns a map of all the variables defined in the specified
// composefile (dict representation) and their default value if any.
func ExtractVariables(configDict map[string]interface{}) map[string]string {
return recurseExtract(configDict)
}
func recurseExtract(value interface{}) map[string]string {
m := map[string]string{}
switch value := value.(type) {
case string:
if v, is := extractVariable(value); is {
m[v.name] = v.value
}
case map[string]interface{}:
for _, elem := range value {
submap := recurseExtract(elem)
for key, value := range submap {
m[key] = value
}
}
case []interface{}:
for _, elem := range value {
if v, is := extractVariable(elem); is {
m[v.name] = v.value
}
}
}
return m
}
type extractedValue struct {
name string
value string
}
func extractVariable(value interface{}) (extractedValue, bool) {
sValue, ok := value.(string)
if !ok {
return extractedValue{}, false
}
matches := pattern.FindStringSubmatch(sValue)
if len(matches) == 0 {
return extractedValue{}, false
}
groups := matchGroups(matches)
if escaped := groups["escaped"]; escaped != "" {
return extractedValue{}, false
}
val := groups["named"]
if val == "" {
val = groups["braced"]
}
name := val
var defaultValue string
switch {
case strings.Contains(val, ":?"):
name, _ = partition(val, ":?")
case strings.Contains(val, "?"):
name, _ = partition(val, "?")
case strings.Contains(val, ":-"):
name, defaultValue = partition(val, ":-")
case strings.Contains(val, "-"):
name, defaultValue = partition(val, "-")
}
return extractedValue{name: name, value: defaultValue}, true
}
// Soft default (fall back if unset or empty)
func softDefault(substitution string, mapping Mapping) (string, bool, error) {
if !strings.Contains(substitution, ":-") {
return "", false, nil
}
name, defaultValue := partition(substitution, ":-")
value, ok := mapping(name)
if !ok || value == "" {
return defaultValue, true, nil
}
return value, true, nil
return withDefault(substitution, mapping, "-:")
}
// Hard default (fall back if-and-only-if empty)
func hardDefault(substitution string, mapping Mapping) (string, bool, error) {
if !strings.Contains(substitution, "-") {
return withDefault(substitution, mapping, "-")
}
func withDefault(substitution string, mapping Mapping, sep string) (string, bool, error) {
if !strings.Contains(substitution, sep) {
return "", false, nil
}
name, defaultValue := partition(substitution, "-")
name, defaultValue := partition(substitution, sep)
value, ok := mapping(name)
if !ok {
return defaultValue, true, nil
@ -120,26 +186,20 @@ func hardDefault(substitution string, mapping Mapping) (string, bool, error) {
}
func requiredNonEmpty(substitution string, mapping Mapping) (string, bool, error) {
if !strings.Contains(substitution, ":?") {
return "", false, nil
}
name, errorMessage := partition(substitution, ":?")
value, ok := mapping(name)
if !ok || value == "" {
return "", true, &InvalidTemplateError{
Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
}
}
return value, true, nil
return withRequired(substitution, mapping, ":?", func(v string) bool { return v != "" })
}
func required(substitution string, mapping Mapping) (string, bool, error) {
if !strings.Contains(substitution, "?") {
return withRequired(substitution, mapping, "?", func(_ string) bool { return true })
}
func withRequired(substitution string, mapping Mapping, sep string, valid func(string) bool) (string, bool, error) {
if !strings.Contains(substitution, sep) {
return "", false, nil
}
name, errorMessage := partition(substitution, "?")
name, errorMessage := partition(substitution, sep)
value, ok := mapping(name)
if !ok {
if !ok || !valid(value) {
return "", true, &InvalidTemplateError{
Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
}

View File

@ -172,3 +172,91 @@ func TestSubstituteWithCustomFunc(t *testing.T) {
_, err = SubstituteWith("ok ${NOTHERE}", defaultMapping, pattern, errIsMissing)
assert.Check(t, is.ErrorContains(err, "required variable"))
}
func TestExtractVariables(t *testing.T) {
testCases := []struct {
dict map[string]interface{}
expected map[string]string
}{
{
dict: map[string]interface{}{},
expected: map[string]string{},
},
{
dict: map[string]interface{}{
"foo": "bar",
},
expected: map[string]string{},
},
{
dict: map[string]interface{}{
"foo": "$bar",
},
expected: map[string]string{
"bar": "",
},
},
{
dict: map[string]interface{}{
"foo": "${bar}",
},
expected: map[string]string{
"bar": "",
},
},
{
dict: map[string]interface{}{
"foo": "${bar?:foo}",
},
expected: map[string]string{
"bar": "",
},
},
{
dict: map[string]interface{}{
"foo": "${bar?foo}",
},
expected: map[string]string{
"bar": "",
},
},
{
dict: map[string]interface{}{
"foo": "${bar:-foo}",
},
expected: map[string]string{
"bar": "foo",
},
},
{
dict: map[string]interface{}{
"foo": "${bar-foo}",
},
expected: map[string]string{
"bar": "foo",
},
},
{
dict: map[string]interface{}{
"foo": "${bar:-foo}",
"bar": map[string]interface{}{
"foo": "${fruit:-banana}",
"bar": "vegetable",
},
"baz": []interface{}{
"foo",
"$toto",
},
},
expected: map[string]string{
"bar": "foo",
"fruit": "banana",
"toto": "",
},
},
}
for _, tc := range testCases {
actual := ExtractVariables(tc.dict)
assert.Check(t, is.DeepEqual(actual, tc.expected))
}
}

View File

@ -1,2 +0,0 @@
Tianon Gravi <admwiggin@gmail.com> (@tianon)
Jessie Frazelle <jess@docker.com> (@jfrazelle)

View File

@ -1352,7 +1352,7 @@ The following `run` command options work with container networking:
--expose=[]: Expose a port or a range of ports inside the container.
These are additional to those exposed by the `EXPOSE` instruction
-P : Publish all exposed ports to the host interfaces
-p=[] : Publish a container᾿s port or a range of ports to the host
-p=[] : Publish a container's port or a range of ports to the host
format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
Both hostPort and containerPort can be specified as a
range of ports. When specifying ranges for both, the