Skip to content

Commit fb9cf0e

Browse files
committed
Improved port switching code
1 parent d87f853 commit fb9cf0e

File tree

1 file changed

+18
-0
lines changed
  • images/baseimage/entrypoints/lib

1 file changed

+18
-0
lines changed

images/baseimage/entrypoints/lib/lib.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"os/exec"
1010
"github.com/phayes/freeport"
11+
"regexp"
1112
)
1213

1314
var args = os.Args[1:]
@@ -27,13 +28,30 @@ func ExecuteArgs() int {
2728

2829
if portSwitch {
2930
port = freeport
31+
replaced := false
32+
// find numbers in arguments and replace with free port
3033
for i, s := range argv {
3134
if _, errA := strconv.Atoi(s); errA == nil {
3235
fmt.Printf("Changing port %q to %q for new container instance.\n", s, strconv.Itoa(port))
3336
argv[i] = strconv.Itoa(port)
3437
}
3538
}
39+
40+
// attempt to replace in structures like --port=4433
41+
if replaced == false {
42+
for j, t := range argv {
43+
regex := regexp.MustCompile("[0-9]+")
44+
found := regex.FindAllString(t, -1)
45+
// ensure argument only has one number and ends with this number
46+
if len(found) == 1 && strings.HasSuffix(t, found[0]) && strings.Contains(t, "=") {
47+
fmt.Printf("Changing port %q of parameter %q to %q for new container instance.\n", found[0], t, strconv.Itoa(port))
48+
argv[j] = strings.Replace(argv[j], found[0], strconv.Itoa(port), -1)
49+
}
50+
}
51+
}
52+
3653
} else {
54+
// try to set port according to arguments
3755
for _, s := range argv {
3856
if val, errA := strconv.Atoi(s); errA == nil {
3957
port = val

0 commit comments

Comments
 (0)