| 1 | package core |
| 2 | |
| 3 | import ( |
| 4 | "os/exec" |
| 5 | "sort" |
| 6 | |
| 7 | "github.com/evilsocket/islazy/str" |
| 8 | ) |
| 9 | |
| 10 | func UniqueInts(a []int, sorted bool) []int { |
| 11 | tmp := make(map[int]bool, len(a)) |
| 12 | uniq := make([]int, 0, len(a)) |
| 13 | |
| 14 | for _, n := range a { |
| 15 | tmp[n] = true |
| 16 | } |
| 17 | |
| 18 | for n := range tmp { |
| 19 | uniq = append(uniq, n) |
| 20 | } |
| 21 | |
| 22 | if sorted { |
| 23 | sort.Ints(uniq) |
| 24 | } |
| 25 | |
| 26 | return uniq |
| 27 | } |
| 28 | |
| 29 | func HasBinary(executable string) bool { |
| 30 | if path, err := exec.LookPath(executable); err != nil || path == "" { |
| 31 | return false |
| 32 | } |
| 33 | return true |
| 34 | } |
| 35 | |
| 36 | func Exec(executable string, args []string) (string, error) { |
| 37 | path, err := exec.LookPath(executable) |
| 38 | if err != nil { |
| 39 | return "", err |
| 40 | } |
| 41 | |
| 42 | raw, err := exec.Command(path, args...).CombinedOutput() |
| 43 | if err != nil { |
| 44 | return str.Trim(string(raw)), err |
| 45 | } else { |
| 46 | return str.Trim(string(raw)), nil |
| 47 | } |
| 48 | } |