core.go 789 B

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