Just read code and saw inconsistencies in variable decls and stuff.
Signed-off-by: Alexander Morozov <lk4d4@docker.com>
| ... | ... |
@@ -5,6 +5,7 @@ import ( |
| 5 | 5 |
"mime" |
| 6 | 6 |
"path/filepath" |
| 7 | 7 |
"sort" |
| 8 |
+ "strconv" |
|
| 8 | 9 |
"strings" |
| 9 | 10 |
|
| 10 | 11 |
"github.com/Sirupsen/logrus" |
| ... | ... |
@@ -37,22 +38,17 @@ func (r byPrivatePort) Less(i, j int) bool { return r[i].PrivatePort < r[j].Priv
|
| 37 | 37 |
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp" |
| 38 | 38 |
// it's used by command 'docker ps' |
| 39 | 39 |
func DisplayablePorts(ports []types.Port) string {
|
| 40 |
- var ( |
|
| 41 |
- result = []string{}
|
|
| 42 |
- hostMappings = []string{}
|
|
| 43 |
- firstInGroupMap map[string]int |
|
| 44 |
- lastInGroupMap map[string]int |
|
| 45 |
- ) |
|
| 46 |
- firstInGroupMap = make(map[string]int) |
|
| 47 |
- lastInGroupMap = make(map[string]int) |
|
| 40 |
+ type portGroup struct {
|
|
| 41 |
+ first int |
|
| 42 |
+ last int |
|
| 43 |
+ } |
|
| 44 |
+ groupMap := make(map[string]*portGroup) |
|
| 45 |
+ var result []string |
|
| 46 |
+ var hostMappings []string |
|
| 48 | 47 |
sort.Sort(byPrivatePort(ports)) |
| 49 | 48 |
for _, port := range ports {
|
| 50 |
- var ( |
|
| 51 |
- current = port.PrivatePort |
|
| 52 |
- portKey = port.Type |
|
| 53 |
- firstInGroup int |
|
| 54 |
- lastInGroup int |
|
| 55 |
- ) |
|
| 49 |
+ current := port.PrivatePort |
|
| 50 |
+ portKey := port.Type |
|
| 56 | 51 |
if port.IP != "" {
|
| 57 | 52 |
if port.PublicPort != current {
|
| 58 | 53 |
hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
|
| ... | ... |
@@ -60,45 +56,38 @@ func DisplayablePorts(ports []types.Port) string {
|
| 60 | 60 |
} |
| 61 | 61 |
portKey = fmt.Sprintf("%s/%s", port.IP, port.Type)
|
| 62 | 62 |
} |
| 63 |
- firstInGroup = firstInGroupMap[portKey] |
|
| 64 |
- lastInGroup = lastInGroupMap[portKey] |
|
| 63 |
+ group := groupMap[portKey] |
|
| 65 | 64 |
|
| 66 |
- if firstInGroup == 0 {
|
|
| 67 |
- firstInGroupMap[portKey] = current |
|
| 68 |
- lastInGroupMap[portKey] = current |
|
| 65 |
+ if group == nil {
|
|
| 66 |
+ groupMap[portKey] = &portGroup{first: current, last: current}
|
|
| 69 | 67 |
continue |
| 70 | 68 |
} |
| 71 |
- |
|
| 72 |
- if current == (lastInGroup + 1) {
|
|
| 73 |
- lastInGroupMap[portKey] = current |
|
| 69 |
+ if current == (group.last + 1) {
|
|
| 70 |
+ group.last = current |
|
| 74 | 71 |
continue |
| 75 | 72 |
} |
| 76 |
- result = append(result, formGroup(portKey, firstInGroup, lastInGroup)) |
|
| 77 |
- firstInGroupMap[portKey] = current |
|
| 78 |
- lastInGroupMap[portKey] = current |
|
| 73 |
+ |
|
| 74 |
+ result = append(result, formGroup(portKey, group.first, group.last)) |
|
| 75 |
+ groupMap[portKey] = &portGroup{first: current, last: current}
|
|
| 79 | 76 |
} |
| 80 |
- for portKey, firstInGroup := range firstInGroupMap {
|
|
| 81 |
- result = append(result, formGroup(portKey, firstInGroup, lastInGroupMap[portKey])) |
|
| 77 |
+ for portKey, g := range groupMap {
|
|
| 78 |
+ result = append(result, formGroup(portKey, g.first, g.last)) |
|
| 82 | 79 |
} |
| 83 | 80 |
result = append(result, hostMappings...) |
| 84 | 81 |
return strings.Join(result, ", ") |
| 85 | 82 |
} |
| 86 | 83 |
|
| 87 | 84 |
func formGroup(key string, start, last int) string {
|
| 88 |
- var ( |
|
| 89 |
- group string |
|
| 90 |
- parts = strings.Split(key, "/") |
|
| 91 |
- groupType = parts[0] |
|
| 92 |
- ip = "" |
|
| 93 |
- ) |
|
| 85 |
+ parts := strings.Split(key, "/") |
|
| 86 |
+ groupType := parts[0] |
|
| 87 |
+ var ip string |
|
| 94 | 88 |
if len(parts) > 1 {
|
| 95 | 89 |
ip = parts[0] |
| 96 | 90 |
groupType = parts[1] |
| 97 | 91 |
} |
| 98 |
- if start == last {
|
|
| 99 |
- group = fmt.Sprintf("%d", start)
|
|
| 100 |
- } else {
|
|
| 101 |
- group = fmt.Sprintf("%d-%d", start, last)
|
|
| 92 |
+ group := strconv.Itoa(start) |
|
| 93 |
+ if start != last {
|
|
| 94 |
+ group = fmt.Sprintf("%s-%d", group, last)
|
|
| 102 | 95 |
} |
| 103 | 96 |
if ip != "" {
|
| 104 | 97 |
group = fmt.Sprintf("%s:%s->%s", ip, group, group)
|