Skip to content

Commit

Permalink
fix: searches golang engine environment for port.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Jan 6, 2025
1 parent d706956 commit 84bd94e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
17 changes: 10 additions & 7 deletions engine/golang/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ package golang

import (
"strconv"
"strings"

"gatehill.io/imposter/engine/procutil"
)

var matcher = procutil.ProcessMatcher{
ProcessName: "imposter-go",
CommandPattern: "imposter-go$",
GetPort: func(cmdline []string) int {
GetPort: func(cmdline []string, env []string) int {
// Check environment variables
for _, arg := range cmdline {
if portStr := procutil.ReadArg([]string{arg}, "IMPOSTER_PORT", ""); portStr != "" {
if port, err := strconv.Atoi(portStr); err == nil {
return port
}
for _, e := range env {
if !strings.HasPrefix(e, "IMPOSTER_PORT=") {
continue
}
portRaw := strings.TrimPrefix(e, "IMPOSTER_PORT=")
if port, err := strconv.Atoi(portRaw); err == nil {
return port
}
}
return 0
return 8080
},
}
9 changes: 2 additions & 7 deletions engine/jvm/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package jvm
import (
"strconv"

"gatehill.io/imposter/engine"
"gatehill.io/imposter/engine/procutil"
)

var matcher = procutil.ProcessMatcher{
ProcessName: "java",
CommandPattern: "([/\\\\]imposter.*\\.jar$|^io.gatehill.imposter.cmd.ImposterLauncher$)",
GetPort: func(cmdline []string) int {
GetPort: func(cmdline []string, env []string) int {
portRaw := procutil.ReadArg(cmdline, "listenPort", "l")
if portRaw != "" {
if port, err := strconv.Atoi(portRaw); err == nil {
Expand All @@ -20,7 +19,7 @@ var matcher = procutil.ProcessMatcher{
if isTlsEnabled(cmdline) {
return 8443
}
return 0
return 8080
},
}

Expand All @@ -33,7 +32,3 @@ func isTlsEnabled(cmdline []string) bool {
}
return false
}

func findImposterJvmProcesses() ([]engine.ManagedMock, error) {
return procutil.FindImposterProcesses(matcher)
}
13 changes: 8 additions & 5 deletions engine/procutil/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ProcessMatcher struct {
// CommandPattern is a regex pattern to match against command line arguments
CommandPattern string
// GetPort is a function that determines the port from command line arguments
GetPort func([]string) int
GetPort func(cmdline []string, env []string) int
}

// FindImposterProcesses finds all imposter processes matching the given matcher
Expand All @@ -44,12 +44,15 @@ func FindImposterProcesses(matcher ProcessMatcher) ([]engine.ManagedMock, error)
if !isImposterProc(cmdline, procName, matcher) {
continue
}
logger.Tracef("found %s Imposter process %d: %v", matcher.ProcessName, p.Pid, cmdline)

port := matcher.GetPort(cmdline)
if port == 0 {
port = 8080 // default port
env, err := p.Environ()
if err != nil {
logger.Warnf("error reading environment variables for process %d: %v", p.Pid, err)
env = []string{}
}
logger.Tracef("found %s Imposter process %d: %v, env: %v", matcher.ProcessName, p.Pid, cmdline, env)

port := matcher.GetPort(cmdline, env)

mock := engine.ManagedMock{
ID: fmt.Sprintf("%d", p.Pid),
Expand Down

0 comments on commit 84bd94e

Please sign in to comment.