Skip to content

Commit 1d28bc7

Browse files
authored
fix: revert "hotfix: wmic deprecated (#402)" (#411)
This reverts commit f822f0f.
1 parent 98d91c8 commit 1d28bc7

File tree

3 files changed

+19
-52
lines changed

3 files changed

+19
-52
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ require (
2121

2222
require (
2323
atomicgo.dev/schedule v0.1.0 // indirect
24-
github.com/StackExchange/wmi v1.2.1 // indirect
2524
github.com/andybalholm/brotli v1.1.0 // indirect
2625
github.com/andybalholm/cascadia v1.3.2 // indirect
2726
github.com/bodgit/plumbing v1.3.0 // indirect
2827
github.com/bodgit/windows v1.0.1 // indirect
2928
github.com/containerd/console v1.0.4 // indirect
3029
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
31-
github.com/go-ole/go-ole v1.2.5 // indirect
3230
github.com/gookit/color v1.5.4 // indirect
3331
github.com/hashicorp/errwrap v1.0.0 // indirect
3432
github.com/hashicorp/go-multierror v1.1.1 // indirect

go.sum

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ github.com/MarvinJWendt/testza v0.5.2 h1:53KDo64C1z/h/d/stCYCPY69bt/OSwjq5KpFNwi
3636
github.com/MarvinJWendt/testza v0.5.2/go.mod h1:xu53QFE5sCdjtMCKk8YMQ2MnymimEctc4n3EjyIYvEY=
3737
github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
3838
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
39-
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
40-
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
4139
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
4240
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
4341
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
@@ -66,8 +64,6 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
6664
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
6765
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
6866
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
69-
github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
70-
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
7167
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
7268
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
7369
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -267,7 +263,6 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w
267263
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
268264
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
269265
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
270-
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
271266
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
272267
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
273268
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

internal/shell/windows_process.go

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,9 @@ import (
2222
"fmt"
2323
"os"
2424
"os/exec"
25-
26-
"github.com/StackExchange/wmi"
27-
"github.com/version-fox/vfox/internal/env"
25+
"strings"
2826
)
2927

30-
type Win32_Process struct {
31-
ExecutablePath string
32-
CommandLine string
33-
ProcessId uint32
34-
}
35-
3628
type windowsProcess struct{}
3729

3830
var process = windowsProcess{}
@@ -42,44 +34,26 @@ func GetProcess() Process {
4234
}
4335

4436
func (w windowsProcess) Open(pid int) error {
45-
// Check if shell has hooks configured
46-
if !env.IsHookEnv() {
47-
return handleNoHookFallback(pid)
48-
}
49-
50-
// Query WMI for process info
51-
var processes []Win32_Process
52-
query := fmt.Sprintf("SELECT ExecutablePath FROM Win32_Process WHERE ProcessId = %d", pid)
53-
if err := wmi.Query(query, &processes); err != nil {
54-
return fmt.Errorf("WMI query failed: %w", err)
55-
}
56-
57-
if len(processes) == 0 {
58-
return fmt.Errorf("process with PID %d not found", pid)
37+
// On Windows, os.FindProcess does not actually find the process.
38+
// So, we use this workaround to get the parent process name.
39+
cmd := exec.Command("tasklist", "/FI", fmt.Sprintf("PID eq %d", pid), "/NH", "/FO", "CSV")
40+
output, err := cmd.Output()
41+
if err != nil {
42+
return err
5943
}
60-
61-
// Get executable path
62-
path := processes[0].ExecutablePath
63-
if path == "" {
64-
return fmt.Errorf("executable path not found for PID %d", pid)
44+
cmd = exec.Command("wmic", "process", "where", fmt.Sprintf("ProcessId=%d", pid), "get", "ExecutablePath", "/format:list")
45+
output, err = cmd.Output()
46+
if err != nil {
47+
return err
6548
}
66-
67-
// Launch new shell process with proper environment
68-
cmd := exec.Command(path)
69-
cmd.Env = os.Environ()
70-
cmd.Stdin = os.Stdin
71-
cmd.Stdout = os.Stdout
72-
cmd.Stderr = os.Stderr
73-
74-
if err := cmd.Run(); err != nil {
75-
return fmt.Errorf("failed to launch shell: %w", err)
49+
path := strings.TrimPrefix(strings.TrimSpace(string(output)), "ExecutablePath=")
50+
command := exec.Command(path)
51+
command.Env = os.Environ()
52+
command.Stdin = os.Stdin
53+
command.Stdout = os.Stdout
54+
command.Stderr = os.Stderr
55+
if err := command.Run(); err != nil {
56+
return fmt.Errorf("open a new shell failed, err:%w", err)
7657
}
77-
78-
return nil
79-
}
80-
81-
func handleNoHookFallback(pid int) error {
82-
// Fall back to global scope if no hooks
83-
fmt.Println("Warning: The current shell lacks hook support. Switching to global scope.")
8458
return nil
8559
}

0 commit comments

Comments
 (0)