@@ -47,6 +47,20 @@ func (l *Link) Alias() string {
4747 return alias
4848}
4949
50+ func nextContiguous (ports []nat.Port , value int , index int ) int {
51+ if index + 1 == len (ports ) {
52+ return index
53+ }
54+ for i := index + 1 ; i < len (ports ); i ++ {
55+ if ports [i ].Int () > value + 1 {
56+ return i - 1
57+ }
58+
59+ value ++
60+ }
61+ return len (ports ) - 1
62+ }
63+
5064func (l * Link ) ToEnv () []string {
5165 env := []string {}
5266 alias := strings .Replace (strings .ToUpper (l .Alias ()), "-" , "_" , - 1 )
@@ -55,12 +69,35 @@ func (l *Link) ToEnv() []string {
5569 env = append (env , fmt .Sprintf ("%s_PORT=%s://%s:%s" , alias , p .Proto (), l .ChildIP , p .Port ()))
5670 }
5771
58- // Load exposed ports into the environment
59- for _ , p := range l .Ports {
72+ //sort the ports so that we can bulk the continuous ports together
73+ nat .Sort (l .Ports , func (ip , jp nat.Port ) bool {
74+ // If the two ports have the same number, tcp takes priority
75+ // Sort in desc order
76+ return ip .Int () < jp .Int () || (ip .Int () == jp .Int () && strings .ToLower (ip .Proto ()) == "tcp" )
77+ })
78+
79+ for i := 0 ; i < len (l .Ports ); {
80+ p := l .Ports [i ]
81+ j := nextContiguous (l .Ports , p .Int (), i )
82+ if j > i + 1 {
83+ env = append (env , fmt .Sprintf ("%s_PORT_%s_%s_START=%s://%s:%s" , alias , p .Port (), strings .ToUpper (p .Proto ()), p .Proto (), l .ChildIP , p .Port ()))
84+ env = append (env , fmt .Sprintf ("%s_PORT_%s_%s_ADDR=%s" , alias , p .Port (), strings .ToUpper (p .Proto ()), l .ChildIP ))
85+ env = append (env , fmt .Sprintf ("%s_PORT_%s_%s_PROTO=%s" , alias , p .Port (), strings .ToUpper (p .Proto ()), p .Proto ()))
86+ env = append (env , fmt .Sprintf ("%s_PORT_%s_%s_PORT_START=%s" , alias , p .Port (), strings .ToUpper (p .Proto ()), p .Port ()))
87+
88+ q := l .Ports [j ]
89+ env = append (env , fmt .Sprintf ("%s_PORT_%s_%s_END=%s://%s:%s" , alias , p .Port (), strings .ToUpper (q .Proto ()), q .Proto (), l .ChildIP , q .Port ()))
90+ env = append (env , fmt .Sprintf ("%s_PORT_%s_%s_PORT_END=%s" , alias , p .Port (), strings .ToUpper (q .Proto ()), q .Port ()))
91+
92+ i = j + 1
93+ continue
94+ }
95+
6096 env = append (env , fmt .Sprintf ("%s_PORT_%s_%s=%s://%s:%s" , alias , p .Port (), strings .ToUpper (p .Proto ()), p .Proto (), l .ChildIP , p .Port ()))
6197 env = append (env , fmt .Sprintf ("%s_PORT_%s_%s_ADDR=%s" , alias , p .Port (), strings .ToUpper (p .Proto ()), l .ChildIP ))
6298 env = append (env , fmt .Sprintf ("%s_PORT_%s_%s_PORT=%s" , alias , p .Port (), strings .ToUpper (p .Proto ()), p .Port ()))
6399 env = append (env , fmt .Sprintf ("%s_PORT_%s_%s_PROTO=%s" , alias , p .Port (), strings .ToUpper (p .Proto ()), p .Proto ()))
100+ i ++
64101 }
65102
66103 // Load the linked container's name into the environment
@@ -125,7 +162,7 @@ func (l *Link) toggle(action string, ignoreErrors bool) error {
125162
126163 out := make ([]string , len (l .Ports ))
127164 for i , p := range l .Ports {
128- out [i ] = fmt . Sprintf ( "%s/%s" , p . Port (), p . Proto () )
165+ out [i ] = string ( p )
129166 }
130167 job .SetenvList ("Ports" , out )
131168
0 commit comments