From 29612ccefecfe6017f32001e6289480a5d276eda Mon Sep 17 00:00:00 2001 From: Sami Tabet Date: Thu, 5 Jul 2018 00:32:25 +0200 Subject: [PATCH] Adding support of the long syntax publish notation Signed-off-by: Sami Tabet --- cli/command/container/opts.go | 36 +++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index b8ff5e4a4a..97906b6722 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -370,9 +370,24 @@ func parse(flags *pflag.FlagSet, copts *containerOptions) (*containerConfig, err entrypoint = []string{""} } - ports, portBindings, err := nat.ParsePortSpecs(copts.publish.GetAll()) + publishOpts := copts.publish.GetAll() + var ports map[nat.Port]struct{} + var portBindings map[nat.Port][]nat.PortBinding + + ports, portBindings, err = nat.ParsePortSpecs(publishOpts) + + // If simple port parsing fails try to parse as long format if err != nil { - return nil, err + publishOpts, err = parsePortOpts(publishOpts) + if err != nil { + return nil, err + } + + ports, portBindings, err = nat.ParsePortSpecs(publishOpts) + + if err != nil { + return nil, err + } } // Merge in exposed ports to the map of published ports @@ -661,6 +676,23 @@ func parse(flags *pflag.FlagSet, copts *containerOptions) (*containerConfig, err }, nil } +func parsePortOpts(publishOpts []string) ([]string, error) { + optsList := []string{} + for _, publish := range publishOpts { + params := map[string]string{"protocol": "tcp"} + for _, param := range strings.Split(publish, ",") { + opt := strings.Split(param, "=") + if len(opt) < 2 { + return optsList, errors.Errorf("invalid publish opts format (should be name=value but got '%s')", param) + } + + params[opt[0]] = opt[1] + } + optsList = append(optsList, fmt.Sprintf("%s:%s/%s", params["target"], params["published"], params["protocol"])) + } + return optsList, nil +} + func parseLoggingOpts(loggingDriver string, loggingOpts []string) (map[string]string, error) { loggingOptsMap := opts.ConvertKVStringsToMap(loggingOpts) if loggingDriver == "none" && len(loggingOpts) > 0 {