mirror of
https://github.com/anyproto/anytype-ts.git
synced 2025-06-08 05:57:02 +09:00
avoid using bash on windows for getting ports
This commit is contained in:
parent
f9a5989f9f
commit
d8200b5bef
1 changed files with 20 additions and 2 deletions
|
@ -48,10 +48,21 @@ func splitStdOutTokens(line string) []string {
|
|||
|
||||
// executes a command and returns the stdout as string
|
||||
func execCommand(command string) (string, error) {
|
||||
if runtime.GOOS == "windows" {
|
||||
return execCommandWin(command)
|
||||
}
|
||||
stdout, err := exec.Command("bash", "-c", command).Output()
|
||||
return string(stdout), err
|
||||
}
|
||||
|
||||
func execCommandWin(command string) (string, error) {
|
||||
// Splitting the command into the executable and the arguments
|
||||
// For Windows, commands are executed through cmd /C
|
||||
cmd := exec.Command("cmd", "/C", command)
|
||||
stdout, err := cmd.Output()
|
||||
return string(stdout), err
|
||||
}
|
||||
|
||||
// checks if a string is contained in an array of strings
|
||||
func contains(s []string, e string) bool {
|
||||
for _, a := range s {
|
||||
|
@ -64,10 +75,11 @@ func contains(s []string, e string) bool {
|
|||
|
||||
// CORE LOGIC
|
||||
|
||||
// Windows: returns a list of open ports for all instances of anytypeHelper.exe found using cli utilities tasklist, netstat and findstr
|
||||
// Windows: returns a list of open ports for all instances of anytypeHelper.exe found using cli utilities tasklist, netstat and findstr
|
||||
func getOpenPortsWindows() (map[string][]string, error) {
|
||||
appName := "anytypeHelper.exe"
|
||||
stdout, err := execCommand(`tasklist | findstr "` + appName + `"`)
|
||||
stdout, err := execCommand(`tasklist`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -75,6 +87,9 @@ func getOpenPortsWindows() (map[string][]string, error) {
|
|||
lines := splitStdOutLines(stdout)
|
||||
pids := map[string]bool{}
|
||||
for _, line := range lines {
|
||||
if !strings.Contains(line, appName) {
|
||||
continue
|
||||
}
|
||||
tokens := splitStdOutTokens(line)
|
||||
pids[tokens[1]] = true
|
||||
}
|
||||
|
@ -85,7 +100,7 @@ func getOpenPortsWindows() (map[string][]string, error) {
|
|||
|
||||
result := map[string][]string{}
|
||||
for pid := range pids {
|
||||
stdout, err := execCommand(`netstat -ano | findstr ${pid} | findstr LISTENING`)
|
||||
stdout, err := execCommand(`netstat -ano`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -93,6 +108,9 @@ func getOpenPortsWindows() (map[string][]string, error) {
|
|||
lines := splitStdOutLines(stdout)
|
||||
ports := map[string]bool{}
|
||||
for _, line := range lines {
|
||||
if !strings.Contains(line, pid) || !strings.Contains(line, "LISTENING") {
|
||||
continue
|
||||
}
|
||||
tokens := splitStdOutTokens(line)
|
||||
port := strings.Split(tokens[1], ":")[1]
|
||||
ports[port] = true
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue