parseDockerIPv4Port parses the input string which is expected to be the output of "docker port" command and returns the first IPv4 port found.
(out string)
| 709 | // parseDockerIPv4Port parses the input string which is expected to be the output of "docker port" |
| 710 | // command and returns the first IPv4 port found. |
| 711 | func parseDockerIPv4Port(out string) (int, error) { |
| 712 | // The "docker port" output may be multiple lines if both IPv4 and IPv6 are supported, |
| 713 | // so we need to parse each line. |
| 714 | for line := range strings.SplitSeq(out, "\n") { |
| 715 | matches := dockerIPv4PortPattern.FindStringSubmatch(strings.TrimSpace(line)) |
| 716 | if len(matches) != 2 { |
| 717 | continue |
| 718 | } |
| 719 | |
| 720 | port, err := strconv.Atoi(matches[1]) |
| 721 | if err != nil { |
| 722 | continue |
| 723 | } |
| 724 | |
| 725 | return port, nil |
| 726 | } |
| 727 | |
| 728 | // We've not been able to parse the output format. |
| 729 | return 0, errors.New("unknown output format") |
| 730 | } |
no outgoing calls