-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(closes #831 ) * Added ports pre checking To find out whether they are being used by other applications. * Check which process is using target port, if any * Moved excludeWhitespace() function to util package
- Loading branch information
Showing
6 changed files
with
112 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/nanovms/ops/util/slice" | ||
) | ||
|
||
// Checks which process is using given port number | ||
func checkPortUserPID(portNumber string) (string, error) { | ||
var ( | ||
pid string | ||
cmd = exec.Command("lsof", "-i", ":"+portNumber) | ||
) | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return pid, err | ||
} | ||
|
||
scanner := bufio.NewScanner(strings.NewReader(string(out))) | ||
scanner.Scan() // check header first | ||
cols := slice.ExcludeWhitespaces(strings.Split(scanner.Text(), " ")) | ||
headerIdx := -1 | ||
for i, v := range cols { | ||
if strings.ToLower(v) == "pid" { | ||
headerIdx = i | ||
break | ||
} | ||
} | ||
|
||
scanner.Scan() | ||
line := strings.Trim(scanner.Text(), " ") | ||
if line != "" { | ||
cols = slice.ExcludeWhitespaces(strings.Split(line, " ")) | ||
pid = cols[headerIdx] | ||
} | ||
|
||
return pid, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package slice | ||
|
||
import "strings" | ||
|
||
// ExcludeWhitespaces removes whitespaces from given slice | ||
func ExcludeWhitespaces(arr []string) []string { | ||
result := make([]string, 0) | ||
for _, h := range arr { | ||
if strings.Trim(h, " ") == "" { | ||
continue | ||
} | ||
result = append(result, h) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package slice | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestExcludeWhitespaces(t *testing.T) { | ||
arr := []string{ | ||
"lorem", "ipsum", " ", "dolor", "", "sit", "amet", | ||
} | ||
trimmed := ExcludeWhitespaces(arr) | ||
for _, v := range trimmed { | ||
if v == "" { | ||
t.Error("found unstripped whice space") | ||
} | ||
} | ||
|
||
expected := "lorem,ipsum,dolor,sit,amet" | ||
str := strings.Join(trimmed, ",") | ||
if str != expected { | ||
t.Errorf("expected '%s', got '%s'", expected, str) | ||
} | ||
} |