Skip to content

Commit

Permalink
Merge pull request #137 from rsteube/remove-go-ps-dependency
Browse files Browse the repository at this point in the history
removed go-ps dependency
  • Loading branch information
rsteube authored Dec 10, 2020
2 parents d273bb4 + eda8e84 commit 08b481b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
61 changes: 37 additions & 24 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 08b481b

Please sign in to comment.