From eda8e846fe17627497222007f5dcdcb2dad53fb3 Mon Sep 17 00:00:00 2001 From: rsteube Date: Thu, 10 Dec 2020 18:33:07 +0100 Subject: [PATCH] removed go-ps dependency --- carapace.go | 61 ++++++++++++++++++++++++++++++++--------------------- go.mod | 1 - go.sum | 2 -- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/carapace.go b/carapace.go index 89eabe0dc..a3dc56939 100644 --- a/carapace.go +++ b/carapace.go @@ -8,9 +8,10 @@ import ( "io/ioutil" "log" "os" + "os/exec" + "runtime" "strings" - ps "github.com/mitchellh/go-ps" "github.com/rsteube/carapace/internal/bash" "github.com/rsteube/carapace/internal/elvish" "github.com/rsteube/carapace/internal/fish" @@ -211,31 +212,43 @@ func traverse(cmd *cobra.Command, args []string) (*cobra.Command, []string) { } func determineShell() string { - process, err := ps.FindProcess(os.Getpid()) - for { - if process, err = ps.FindProcess(process.PPid()); err != nil || process == nil { - return "" + for _, executable := range processExecutables() { + switch executable { + case "bash": + return "bash" + case "elvish": + return "elvish" + case "fish": + return "fish" + case "osh": + return "oil" + case "powershell.exe": + return "powershell" + case "pwsh": + return "powershell" + case "pwsh.exe": + return "powershell" + case "xonsh": + return "xonsh" + case "zsh": + return "zsh" + } + } + return "" +} + +func processExecutables() []string { + if runtime.GOOS == "windows" { + return []string{"powershell.exe"} // TODO hardcoded for now, but there might be elvish or sth. else on window + } else { + if output, err := exec.Command("ps", "-o", "comm").Output(); err != nil { + return []string{} } else { - switch process.Executable() { - case "bash": - return "bash" - case "elvish": - return "elvish" - case "fish": - return "fish" - case "osh": - return "oil" - case "powershell.exe": - return "powershell" - case "pwsh": - return "powershell" - case "pwsh.exe": - return "powershell" - case "xonsh": - return "xonsh" - case "zsh": - return "zsh" + lines := strings.Split(string(output), "\n")[1:] // skip header + for i, j := 0, len(lines)-1; i < j; i, j = i+1, j-1 { // reverse slice + lines[i], lines[j] = lines[j], lines[i] } + return lines } } } diff --git a/go.mod b/go.mod index 054165e0a..d9d523174 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/rsteube/carapace go 1.12 require ( - github.com/mitchellh/go-ps v1.0.0 github.com/spf13/cobra v0.0.7 github.com/spf13/pflag v1.0.5 ) diff --git a/go.sum b/go.sum index 00b5d2cd3..e176a19c8 100644 --- a/go.sum +++ b/go.sum @@ -55,8 +55,6 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc= -github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=