Skip to content

Commit c0319b4

Browse files
authored
Use gopsutil instead of go-sysinfo for shell detection (elastic#1080)
1 parent 9818f7a commit c0319b4

File tree

5 files changed

+60
-94
lines changed

5 files changed

+60
-94
lines changed

cmd/stack.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,7 @@ func setupStackCommand() *cobraext.Command {
204204
}
205205

206206
if shellName == cobraext.ShellInitShellDetect {
207-
shellName, err = stack.AutodetectedShell()
208-
if err != nil {
209-
return fmt.Errorf("cannot detect parent shell from current process: %w", err)
210-
}
207+
shellName = stack.AutodetectedShell()
211208
fmt.Fprintf(cmd.OutOrStderr(), "Detected shell: %s\n", shellName)
212209
} else {
213210
stack.SelectShell(shellName)

go.mod

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ require (
1111
github.com/cespare/xxhash/v2 v2.2.0
1212
github.com/elastic/go-elasticsearch/v7 v7.17.7
1313
github.com/elastic/go-licenser v0.4.1
14-
github.com/elastic/go-sysinfo v1.9.0
1514
github.com/elastic/go-ucfg v0.8.6
1615
github.com/elastic/package-spec/v2 v2.2.0
1716
github.com/fatih/color v1.13.0
@@ -26,13 +25,13 @@ require (
2625
github.com/olekukonko/tablewriter v0.0.5
2726
github.com/pkg/errors v0.9.1
2827
github.com/pmezard/go-difflib v1.0.0
28+
github.com/shirou/gopsutil/v3 v3.22.11
2929
github.com/spf13/cobra v1.6.1
3030
github.com/stretchr/testify v1.8.1
3131
golang.org/x/oauth2 v0.3.0
3232
golang.org/x/tools v0.4.0
3333
gopkg.in/yaml.v3 v3.0.1
3434
gotest.tools/gotestsum v1.8.2
35-
gotest.tools/v3 v3.4.0
3635
helm.sh/helm/v3 v3.10.3
3736
honnef.co/go/tools v0.3.3
3837
k8s.io/apimachinery v0.26.0
@@ -58,7 +57,6 @@ require (
5857
github.com/davecgh/go-spew v1.1.1 // indirect
5958
github.com/dnephin/pflag v1.0.7 // indirect
6059
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
61-
github.com/elastic/go-windows v1.0.1 // indirect
6260
github.com/elastic/gojsonschema v1.2.1 // indirect
6361
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
6462
github.com/emirpasic/gods v1.18.1 // indirect
@@ -69,6 +67,7 @@ require (
6967
github.com/go-errors/errors v1.4.2 // indirect
7068
github.com/go-git/gcfg v1.5.0 // indirect
7169
github.com/go-logr/logr v1.2.3 // indirect
70+
github.com/go-ole/go-ole v1.2.6 // indirect
7271
github.com/go-openapi/errors v0.20.3 // indirect
7372
github.com/go-openapi/jsonpointer v0.19.5 // indirect
7473
github.com/go-openapi/jsonreference v0.20.0 // indirect
@@ -95,6 +94,7 @@ require (
9594
github.com/klauspost/pgzip v1.2.5 // indirect
9695
github.com/kr/pretty v0.3.0 // indirect
9796
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
97+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
9898
github.com/mailru/easyjson v0.7.7 // indirect
9999
github.com/mattn/go-colorable v0.1.13 // indirect
100100
github.com/mattn/go-isatty v0.0.16 // indirect
@@ -113,19 +113,22 @@ require (
113113
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
114114
github.com/pierrec/lz4/v4 v4.1.17 // indirect
115115
github.com/pjbgf/sha1cd v0.2.3 // indirect
116-
github.com/prometheus/procfs v0.8.0 // indirect
116+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
117117
github.com/rivo/uniseg v0.4.3 // indirect
118118
github.com/russross/blackfriday/v2 v2.1.0 // indirect
119119
github.com/sergi/go-diff v1.2.0 // indirect
120120
github.com/shopspring/decimal v1.3.1 // indirect
121121
github.com/skeema/knownhosts v1.1.0 // indirect
122122
github.com/spf13/pflag v1.0.5 // indirect
123+
github.com/tklauser/go-sysconf v0.3.11 // indirect
124+
github.com/tklauser/numcpus v0.6.0 // indirect
123125
github.com/ulikunitz/xz v0.5.11 // indirect
124126
github.com/xanzy/ssh-agent v0.3.3 // indirect
125127
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
126128
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
127129
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
128130
github.com/xlab/treeprint v1.1.0 // indirect
131+
github.com/yusufpapurcu/wmi v1.2.2 // indirect
129132
go.etcd.io/etcd/api/v3 v3.5.6 // indirect
130133
go.mongodb.org/mongo-driver v1.11.1 // indirect
131134
go.starlark.net v0.0.0-20221205180719-3fd0dac74452 // indirect
@@ -145,7 +148,7 @@ require (
145148
gopkg.in/inf.v0 v0.9.1 // indirect
146149
gopkg.in/warnings.v0 v0.1.2 // indirect
147150
gopkg.in/yaml.v2 v2.4.0 // indirect
148-
howett.net/plist v1.0.0 // indirect
151+
gotest.tools/v3 v3.4.0 // indirect
149152
k8s.io/api v0.26.0 // indirect
150153
k8s.io/apiextensions-apiserver v0.26.0 // indirect
151154
k8s.io/component-base v0.26.0 // indirect

go.sum

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,8 @@ github.com/elastic/go-elasticsearch/v7 v7.17.7 h1:pcYNfITNPusl+cLwLN6OLmVT+F73El
8787
github.com/elastic/go-elasticsearch/v7 v7.17.7/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
8888
github.com/elastic/go-licenser v0.4.1 h1:1xDURsc8pL5zYT9R29425J3vkHdt4RT5TNEMeRN48x4=
8989
github.com/elastic/go-licenser v0.4.1/go.mod h1:V56wHMpmdURfibNBggaSBfqgPxyT1Tldns1i87iTEvU=
90-
github.com/elastic/go-sysinfo v1.9.0 h1:usICqY/Nw4Mpn9f4LdtpFrKxXroJDe81GaxxUlCckIo=
91-
github.com/elastic/go-sysinfo v1.9.0/go.mod h1:eBD1wEGVaRnRLGecc9iG1z8eOv5HnEdz9+nWd8UAxcE=
9290
github.com/elastic/go-ucfg v0.8.6 h1:stUeyh2goTgGX+/wb9gzKvTv0YB0231LTpKUgCKj4U0=
9391
github.com/elastic/go-ucfg v0.8.6/go.mod h1:4E8mPOLSUV9hQ7sgLEJ4bvt0KhMuDJa8joDT2QGAEKA=
94-
github.com/elastic/go-windows v1.0.1 h1:AlYZOldA+UJ0/2nBuqWdo90GFCgG9xuyw9SYzGUtJm0=
95-
github.com/elastic/go-windows v1.0.1/go.mod h1:FoVvqWSun28vaDQPbj2Elfc0JahhPB7WQEGa3c814Ss=
9692
github.com/elastic/gojsonschema v1.2.1 h1:cUMbgsz0wyEB4x7xf3zUEvUVDl6WCz2RKcQPul8OsQc=
9793
github.com/elastic/gojsonschema v1.2.1/go.mod h1:biw5eBS2Z4T02wjATMRSfecfjCmwaDPvuaqf844gLrg=
9894
github.com/elastic/package-spec/v2 v2.2.0 h1:hNTtw1PmM9BcehF5g7E4NNNIXNTxravcbrwATucYOAU=
@@ -138,6 +134,8 @@ github.com/go-git/go-git/v5 v5.5.1/go.mod h1:uz5PQ3d0gz7mSgzZhSJToM6ALPaKCdSnl58
138134
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
139135
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
140136
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
137+
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
138+
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
141139
github.com/go-openapi/errors v0.20.2/go.mod h1:cM//ZKUKyO06HSwqAelJ5NsEMMcpa6VpXe8DOa1Mi1M=
142140
github.com/go-openapi/errors v0.20.3 h1:rz6kiC84sqNQoqrtulzaL/VERgkoCyB6WdEkc2ujzUc=
143141
github.com/go-openapi/errors v0.20.3/go.mod h1:Z3FlZ4I8jEGxjUK+bugx3on2mIAk4txuAOhlsB1FSgk=
@@ -186,6 +184,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
186184
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
187185
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
188186
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
187+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
189188
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
190189
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
191190
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
@@ -251,6 +250,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
251250
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
252251
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
253252
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
253+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
254+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
254255
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
255256
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
256257
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
@@ -318,9 +319,9 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
318319
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
319320
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
320321
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
322+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
323+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
321324
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
322-
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
323-
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
324325
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
325326
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
326327
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
@@ -332,6 +333,8 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
332333
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
333334
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
334335
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
336+
github.com/shirou/gopsutil/v3 v3.22.11 h1:kxsPKS+Eeo+VnEQ2XCaGJepeP6KY53QoRTETx3+1ndM=
337+
github.com/shirou/gopsutil/v3 v3.22.11/go.mod h1:xl0EeL4vXJ+hQMAGN8B9VFpxukEMA0XdevQOe5MZ1oY=
335338
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
336339
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
337340
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
@@ -360,6 +363,10 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
360363
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
361364
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
362365
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
366+
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
367+
github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI=
368+
github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms=
369+
github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4=
363370
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
364371
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
365372
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
@@ -386,6 +393,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
386393
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
387394
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
388395
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
396+
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
397+
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
389398
go.etcd.io/etcd/api/v3 v3.5.6 h1:Cy2qx3npLcYqTKqGJzMypnMv2tiRyifZJ17BlWIWA7A=
390399
go.etcd.io/etcd/api/v3 v3.5.6/go.mod h1:KFtNaxGDw4Yx/BA4iPPwevUTAuqcsPxzyX8PHydchN8=
391400
go.mongodb.org/mongo-driver v1.10.0/go.mod h1:wsihk0Kdgv8Kqu1Anit4sfK+22vSFbUrAVEYRhCXrA8=
@@ -464,14 +473,15 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
464473
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
465474
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
466475
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
467-
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
476+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
468477
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
469478
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
470479
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
471480
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
472481
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
473482
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
474483
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
484+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
475485
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
476486
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
477487
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -588,7 +598,6 @@ gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
588598
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
589599
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
590600
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
591-
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
592601
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
593602
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
594603
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@@ -612,8 +621,6 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
612621
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
613622
honnef.co/go/tools v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA=
614623
honnef.co/go/tools v0.3.3/go.mod h1:jzwdWgg7Jdq75wlfblQxO4neNaFFSvgc1tD5Wv8U0Yw=
615-
howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
616-
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
617624
k8s.io/api v0.26.0 h1:IpPlZnxBpV1xl7TGk/X6lFtpgjgntCg8PJ+qrPHAC7I=
618625
k8s.io/api v0.26.0/go.mod h1:k6HDTaIFC8yn1i6pSClSqIwLABIcLV9l5Q4EcngKnQg=
619626
k8s.io/apiextensions-apiserver v0.26.0 h1:Gy93Xo1eg2ZIkNX/8vy5xviVSxwQulsnUdQ00nEdpDo=

internal/stack/shellinit.go

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
"path/filepath"
1212
"strings"
1313

14-
"github.com/elastic/go-sysinfo"
15-
"github.com/elastic/go-sysinfo/types"
14+
"github.com/shirou/gopsutil/v3/process"
1615

1716
"github.com/elastic/elastic-package/internal/environment"
1817
"github.com/elastic/elastic-package/internal/logger"
@@ -29,21 +28,19 @@ var (
2928
)
3029

3130
var shellType string
32-
var shellDetectError error
3331

3432
func init() {
35-
shellType, shellDetectError = detectShell()
33+
shellType = detectShell()
3634
}
3735

3836
// SelectShell selects the shell to use.
3937
func SelectShell(shell string) {
4038
shellType = shell
41-
shellDetectError = nil
4239
}
4340

4441
// AutodetectedShell returns an error if shell could not be detected.
45-
func AutodetectedShell() (string, error) {
46-
return shellType, shellDetectError
42+
func AutodetectedShell() string {
43+
return shellType
4744
}
4845

4946
// ShellInit method exposes environment variables that can be used for testing purposes.
@@ -129,43 +126,41 @@ func getShellName(exe string) string {
129126
return shell
130127
}
131128

132-
func detectShell() (string, error) {
129+
func detectShell() string {
130+
shell, err := getParentShell()
131+
if err != nil {
132+
logger.Debugf("Failed to determine parent process info while detecting shell, will assume bash: %v", err)
133+
return "bash"
134+
}
135+
136+
return shell
137+
}
138+
139+
func getParentShell() (string, error) {
133140
ppid := os.Getppid()
134-
parentInfo, err := getParentInfo(ppid)
135-
if errors.Is(err, types.ErrNotImplemented) {
136-
// Sysinfo doesn't implement some features in some platforms.
137-
// This mainly affects osx when building without CGO.
138-
// Assume bash in that case.
139-
// See https://github.com/elastic/elastic-package/issues/1030.
140-
logger.Debug("Failed to determine parent process info while detecting shell, will assume bash")
141-
return "bash", nil
141+
p, err := process.NewProcess(int32(ppid))
142+
if err != nil {
143+
return "", err
142144
}
145+
exe, err := p.Exe()
143146
if err != nil {
144147
return "", err
145148
}
146149

147-
shell := getShellName(parentInfo.Exe)
150+
shell := getShellName(exe)
148151
if shell == "go" {
149-
parentParentInfo, err := getParentInfo(parentInfo.PPID)
152+
parent, err := p.Parent()
150153
if err != nil {
151-
return "", fmt.Errorf("cannot retrieve parent process info: %w", err)
154+
return "", err
152155
}
153-
return getShellName(parentParentInfo.Exe), nil
154-
}
155156

156-
return shell, nil
157-
}
158-
159-
func getParentInfo(ppid int) (types.ProcessInfo, error) {
160-
parent, err := sysinfo.Process(ppid)
161-
if err != nil {
162-
return types.ProcessInfo{}, fmt.Errorf("cannot retrieve information for process %d: %w", ppid, err)
163-
}
157+
exe, err := parent.Exe()
158+
if err != nil {
159+
return "", err
160+
}
164161

165-
parentInfo, err := parent.Info()
166-
if err != nil {
167-
return types.ProcessInfo{}, fmt.Errorf("cannot retrieve information for parent of process %d: %w", ppid, err)
162+
return getShellName(exe), nil
168163
}
169164

170-
return parentInfo, nil
165+
return shell, nil
171166
}

internal/stack/shellinit_internal_test.go

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
package stack
66

77
import (
8-
"os"
9-
"reflect"
108
"strings"
119
"testing"
1210

13-
"github.com/elastic/go-sysinfo"
14-
"github.com/elastic/go-sysinfo/types"
15-
"gotest.tools/v3/assert"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1613
)
1714

1815
func TestCodeTemplate(t *testing.T) {
@@ -64,41 +61,8 @@ func Test_getShellName(t *testing.T) {
6461
}
6562
}
6663

67-
func Test_getParentInfo(t *testing.T) {
68-
ppid := os.Getppid()
69-
parent, err := sysinfo.Process(ppid)
70-
if err != nil {
71-
panic(err)
72-
}
73-
info, err := parent.Info()
74-
if err != nil {
75-
panic(err)
76-
}
77-
78-
type args struct {
79-
ppid int
80-
}
81-
tests := []struct {
82-
name string
83-
args args
84-
want types.ProcessInfo
85-
wantErr bool
86-
}{
87-
// TODO: Add test cases.
88-
{"test parent", args{ppid}, info, false},
89-
{"bogus ppid", args{999999}, types.ProcessInfo{}, true},
90-
{"no parent", args{1}, types.ProcessInfo{}, true},
91-
}
92-
for _, tt := range tests {
93-
t.Run(tt.name, func(t *testing.T) {
94-
got, err := getParentInfo(tt.args.ppid)
95-
if (err != nil) != tt.wantErr {
96-
t.Errorf("getParentInfo() error = %v, wantErr %v", err, tt.wantErr)
97-
return
98-
}
99-
if !reflect.DeepEqual(got, tt.want) {
100-
t.Errorf("getParentInfo() = %v, want %v", got, tt.want)
101-
}
102-
})
103-
}
64+
func TestGetParentShell(t *testing.T) {
65+
shell, err := getParentShell()
66+
require.NoError(t, err)
67+
assert.NotEmpty(t, shell)
10468
}

0 commit comments

Comments
 (0)