Skip to content

Commit abd52a0

Browse files
authored
refactor(shell): restructure process handling for better maintainability (#452)
1 parent c67a44a commit abd52a0

File tree

12 files changed

+167
-205
lines changed

12 files changed

+167
-205
lines changed

cmd/commands/cd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ func cdCmd(ctx *cli.Context) error {
5555
if err != nil {
5656
return err
5757
}
58-
return shell.GetProcess().Open(env.GetPid())
58+
return shell.Open(env.GetPid())
5959
}

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ require (
1010
github.com/PuerkitoBio/goquery v1.9.3
1111
github.com/bodgit/sevenzip v1.5.1
1212
github.com/lithammer/fuzzysearch v1.1.8
13-
github.com/mitchellh/go-ps v1.0.0
1413
github.com/pterm/pterm v0.12.79
1514
github.com/schollz/progressbar/v3 v3.14.2
15+
github.com/shirou/gopsutil/v4 v4.25.3
1616
github.com/ulikunitz/xz v0.5.12
1717
github.com/urfave/cli/v2 v2.27.1
1818
github.com/yuin/gopher-lua v1.1.1
@@ -29,18 +29,25 @@ require (
2929
github.com/bodgit/windows v1.0.1 // indirect
3030
github.com/containerd/console v1.0.4 // indirect
3131
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
32+
github.com/ebitengine/purego v0.8.2 // indirect
33+
github.com/go-ole/go-ole v1.2.6 // indirect
3234
github.com/gookit/color v1.5.4 // indirect
3335
github.com/hashicorp/errwrap v1.0.0 // indirect
3436
github.com/hashicorp/go-multierror v1.1.1 // indirect
3537
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
3638
github.com/klauspost/compress v1.17.7 // indirect
39+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
3740
github.com/mattn/go-runewidth v0.0.15 // indirect
3841
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
3942
github.com/pierrec/lz4/v4 v4.1.21 // indirect
43+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
4044
github.com/rivo/uniseg v0.4.7 // indirect
4145
github.com/russross/blackfriday/v2 v2.1.0 // indirect
46+
github.com/tklauser/go-sysconf v0.3.12 // indirect
47+
github.com/tklauser/numcpus v0.6.1 // indirect
4248
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
4349
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
50+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
4451
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
4552
golang.org/x/net v0.29.0 // indirect
4653
golang.org/x/term v0.29.0 // indirect

go.sum

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6N
6060
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6161
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6262
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
63+
github.com/ebitengine/purego v0.8.2 h1:jPPGWs2sZ1UgOSgD2bClL0MJIqu58nOmIcBuXr62z1I=
64+
github.com/ebitengine/purego v0.8.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
6365
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
6466
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
6567
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
6668
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.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
70+
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
6771
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
6872
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
6973
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -82,6 +86,9 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
8286
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
8387
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
8488
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
89+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
90+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
91+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
8592
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
8693
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
8794
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
@@ -120,18 +127,20 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
120127
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
121128
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
122129
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
130+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
131+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
123132
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
124133
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
125134
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
126135
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
127136
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
128137
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
129-
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
130-
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
131138
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
132139
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
133140
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
134141
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
142+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
143+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
135144
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
136145
github.com/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eFF7DZI=
137146
github.com/pterm/pterm v0.12.29/go.mod h1:WI3qxgvoQFFGKGjGnJR849gU0TsEOvKn5Q8LlY1U7lg=
@@ -153,6 +162,8 @@ github.com/schollz/progressbar/v3 v3.14.2 h1:EducH6uNLIWsr560zSV1KrTeUb/wZGAHqyM
153162
github.com/schollz/progressbar/v3 v3.14.2/go.mod h1:aQAZQnhF4JGFtRJiw/eobaXpsqpVQAftEQ+hLGXaRc4=
154163
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
155164
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
165+
github.com/shirou/gopsutil/v4 v4.25.3 h1:SeA68lsu8gLggyMbmCn8cmp97V1TI9ld9sVzAUcKcKE=
166+
github.com/shirou/gopsutil/v4 v4.25.3/go.mod h1:xbuxyoZj+UsgnZrENu3lQivsngRR5BdjbJwf2fv4szA=
156167
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
157168
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
158169
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -163,8 +174,12 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
163174
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
164175
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
165176
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
166-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
167-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
177+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
178+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
179+
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
180+
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
181+
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
182+
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
168183
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
169184
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
170185
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
@@ -177,6 +192,8 @@ github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQut
177192
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
178193
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
179194
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
195+
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
196+
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
180197
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
181198
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
182199
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
@@ -263,11 +280,13 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w
263280
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
264281
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
265282
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
283+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
266284
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
267285
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
268286
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
269287
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
270288
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
289+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
271290
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
272291
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
273292
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -279,6 +298,8 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
279298
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
280299
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
281300
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
301+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
302+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
282303
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
283304
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
284305
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

internal/manager.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
"strconv"
3232
"strings"
3333

34-
"github.com/mitchellh/go-ps"
3534
"github.com/pterm/pterm"
35+
"github.com/shirou/gopsutil/v4/process"
3636
"github.com/urfave/cli/v2"
3737
"github.com/version-fox/vfox/internal/cache"
3838
"github.com/version-fox/vfox/internal/config"
@@ -740,12 +740,13 @@ func (m *Manager) CleanTmp() {
740740
_ = os.WriteFile(cleanFlagPath, []byte(strconv.FormatInt(util.GetBeginOfToday(), 10)), os.ModePerm)
741741

742742
procExists := make(map[string]struct{})
743-
if procList, err := ps.Processes(); err == nil {
743+
744+
if procList, err := process.Pids(); err == nil {
744745
for _, v := range procList {
745-
if v != nil {
746-
procExists[strconv.Itoa(v.Pid())] = struct{}{}
747-
}
746+
procExists[fmt.Sprintf("%d", v)] = struct{}{}
748747
}
748+
} else {
749+
return
749750
}
750751

751752
dir, err := os.ReadDir(m.PathMeta.TempPath)

internal/manager_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package internal
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strconv"
7+
"testing"
8+
"time"
9+
10+
"github.com/shirou/gopsutil/v4/process"
11+
)
12+
13+
func detectPidNotExists(t *testing.T, pid int32, num int32) int32 {
14+
if num < 1 {
15+
t.Fatalf("num must be greater than 1")
16+
}
17+
18+
for i := pid + 1; i < pid+num; i++ {
19+
exists, err := process.PidExists(i)
20+
if err != nil {
21+
t.Fatalf("failed to check pid %d existence: %v", i, err)
22+
}
23+
24+
if !exists {
25+
return i
26+
}
27+
}
28+
29+
return pid + num
30+
}
31+
32+
func TestCleanTmp(t *testing.T) {
33+
tmpRoot := filepath.Join(os.TempDir(), "vfox-cleantmp-test")
34+
_ = os.RemoveAll(tmpRoot)
35+
defer os.RemoveAll(tmpRoot)
36+
now := time.Now()
37+
today := strconv.FormatInt(time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix(), 10)
38+
// Create a simulated temporary directory structure
39+
yesterday := strconv.FormatInt(now.Add(-250*time.Hour).Unix(), 10)
40+
pid := os.Getpid()
41+
otherPid := int(detectPidNotExists(t, int32(pid+100), 10000))
42+
dirs := []string{
43+
filepath.Join(tmpRoot, today+"-"+strconv.Itoa(pid)),
44+
// filepath.Join(tmpRoot, yesterday+"-"+strconv.Itoa(pid)),
45+
filepath.Join(tmpRoot, yesterday+"-"+strconv.Itoa(otherPid)),
46+
filepath.Join(tmpRoot, yesterday+"-"+strconv.Itoa(pid)),
47+
}
48+
for _, dir := range dirs {
49+
if err := os.MkdirAll(dir, 0o755); err != nil {
50+
t.Fatalf("failed to create dir: %v", err)
51+
}
52+
}
53+
cleanFlagPath := filepath.Join(tmpRoot, cleanupFlagFilename)
54+
// Write yesterday to cleanFlagPath
55+
if err := os.WriteFile(cleanFlagPath, []byte(yesterday), 0o644); err != nil {
56+
t.Fatalf("failed to write cleanFlagPath: %v", err)
57+
}
58+
59+
// Construct Manager
60+
m := &Manager{PathMeta: &PathMeta{CurTmpPath: tmpRoot, TempPath: tmpRoot}}
61+
62+
// Execute cleanup
63+
m.CleanTmp()
64+
65+
// today-pid should be retained
66+
if _, err := os.Stat(filepath.Join(tmpRoot, today+"-"+strconv.Itoa(pid))); os.IsNotExist(err) {
67+
t.Errorf("today-pid dir should exist, but got removed")
68+
}
69+
70+
// yesterday-otherPid should be deleted
71+
if _, err := os.Stat(filepath.Join(tmpRoot, yesterday+"-"+strconv.Itoa(otherPid))); err == nil {
72+
t.Errorf("yesterday-otherPid dir should be removed, but still exists")
73+
}
74+
75+
// yesterday-pid should be retained
76+
if _, err := os.Stat(filepath.Join(tmpRoot, yesterday+"-"+strconv.Itoa(pid))); os.IsNotExist(err) {
77+
t.Errorf("yesterday-pid dir should exist, but got removed")
78+
}
79+
}

internal/path.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ const (
4747
TEMP_DIR = "temp"
4848
)
4949

50+
func newTempPath() string {
51+
pid := env.GetPid()
52+
timestamp := util.GetBeginOfToday()
53+
name := fmt.Sprintf("%d-%d", timestamp, pid)
54+
return name
55+
}
56+
5057
func newPathMeta() (*PathMeta, error) {
5158
userHomeDir, err := os.UserHomeDir()
5259
if err != nil {
@@ -69,9 +76,7 @@ func newPathMeta() (*PathMeta, error) {
6976
}
7077
curTmpPath := os.Getenv(HookCurTmpPath)
7178
if curTmpPath == "" {
72-
pid := env.GetPid()
73-
timestamp := util.GetBeginOfToday()
74-
name := fmt.Sprintf("%d-%d", timestamp, pid)
79+
name := newTempPath()
7580
curTmpPath = filepath.Join(tmpPath, name)
7681
}
7782
if !util.FileExists(curTmpPath) {

internal/sdk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ func (b *Sdk) Use(version Version, scope UseScope) error {
429429
if err = toolVersion.Save(); err != nil {
430430
return fmt.Errorf("failed to save tool versions, err:%w", err)
431431
}
432-
return shell.GetProcess().Open(os.Getppid())
432+
return shell.Open(os.Getppid())
433433
} else {
434434
return b.useInHook(version, scope)
435435
}
@@ -502,7 +502,7 @@ func (b *Sdk) useInHook(version Version, scope UseScope) error {
502502

503503
pterm.Printf("Now using %s.\n", pterm.LightGreen(b.label(version)))
504504
if !env.IsHookEnv() {
505-
return shell.GetProcess().Open(os.Getppid())
505+
return shell.Open(os.Getppid())
506506
}
507507
return nil
508508
}

internal/shell/linux_process.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)