|
|
@@ -12,7 +12,7 @@ import (
|
|
|
|
|
|
func findByPID() []int {
|
|
|
var ports []int
|
|
|
- pidRegex := regexp.MustCompile(`(?i)opencode`)
|
|
|
+ pidRegex := regexp.MustCompile(`(?i)(opencode|bun)`)
|
|
|
|
|
|
tasklistOut, err := exec.Command("tasklist", "/FO", "CSV", "/NH").Output()
|
|
|
if err != nil {
|
|
|
@@ -68,19 +68,39 @@ func findByPID() []int {
|
|
|
func findByCmdline() []int {
|
|
|
var ports []int
|
|
|
|
|
|
+ // 查找 opencode 进程
|
|
|
wmicOut, err := exec.Command("wmic", "process", "where", "name like '%opencode%'", "get", "commandline", "/FORMAT:LIST").Output()
|
|
|
- if err != nil {
|
|
|
- return ports
|
|
|
+ if err == nil {
|
|
|
+ for _, line := range strings.Split(string(wmicOut), "\n") {
|
|
|
+ if strings.Contains(line, "--port") {
|
|
|
+ fields := strings.Fields(line)
|
|
|
+ for i, f := range fields {
|
|
|
+ if f == "--port" && i+1 < len(fields) {
|
|
|
+ if port, err := strconv.Atoi(fields[i+1]); err == nil {
|
|
|
+ if !contains(ports, port) {
|
|
|
+ ports = append(ports, port)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- for _, line := range strings.Split(string(wmicOut), "\n") {
|
|
|
- if strings.Contains(line, "--port") {
|
|
|
- fields := strings.Fields(line)
|
|
|
- for i, f := range fields {
|
|
|
- if f == "--port" && i+1 < len(fields) {
|
|
|
- if port, err := strconv.Atoi(fields[i+1]); err == nil {
|
|
|
- if !contains(ports, port) {
|
|
|
- ports = append(ports, port)
|
|
|
+ // 查找 bun 进程运行的 opencode
|
|
|
+ if len(ports) == 0 {
|
|
|
+ bunOut, err := exec.Command("wmic", "process", "where", "name like '%bun%'", "get", "commandline", "/FORMAT:LIST").Output()
|
|
|
+ if err == nil {
|
|
|
+ for _, line := range strings.Split(string(bunOut), "\n") {
|
|
|
+ if strings.Contains(line, "opencode") && strings.Contains(line, "--port") {
|
|
|
+ fields := strings.Fields(line)
|
|
|
+ for i, f := range fields {
|
|
|
+ if f == "--port" && i+1 < len(fields) {
|
|
|
+ if port, err := strconv.Atoi(fields[i+1]); err == nil {
|
|
|
+ if !contains(ports, port) {
|
|
|
+ ports = append(ports, port)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -88,8 +108,9 @@ func findByCmdline() []int {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 使用 PowerShell 作为后备方案
|
|
|
if len(ports) == 0 {
|
|
|
- powershellOut, err := exec.Command("powershell", "-Command", "Get-CimInstance Win32_Process | Where-Object {$_.Name -like '*opencode*'} | Select-Object CommandLine").Output()
|
|
|
+ powershellOut, err := exec.Command("powershell", "-Command", "Get-CimInstance Win32_Process | Where-Object {$_.Name -like '*opencode*' -or ($_.Name -like '*bun*' -and $_.CommandLine -like '*opencode*')} | Select-Object CommandLine").Output()
|
|
|
if err == nil {
|
|
|
for _, line := range strings.Split(string(powershellOut), "\n") {
|
|
|
if strings.Contains(line, "--port") {
|