mirror of
https://github.com/bettercap/bettercap
synced 2025-07-07 13:32:07 -07:00
101 lines
1.7 KiB
Go
101 lines
1.7 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"os/user"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
defaultTrimSet = "\r\n\t "
|
|
)
|
|
|
|
func Trim(s string) string {
|
|
return strings.Trim(s, defaultTrimSet)
|
|
}
|
|
|
|
func TrimRight(s string) string {
|
|
return strings.TrimRight(s, defaultTrimSet)
|
|
}
|
|
|
|
func UniqueInts(a []int, sorted bool) []int {
|
|
tmp := make(map[int]bool)
|
|
uniq := make([]int, 0)
|
|
|
|
for _, n := range a {
|
|
tmp[n] = true
|
|
}
|
|
|
|
for n := range tmp {
|
|
uniq = append(uniq, n)
|
|
}
|
|
|
|
if sorted {
|
|
sort.Ints(uniq)
|
|
}
|
|
|
|
return uniq
|
|
}
|
|
|
|
func SepSplit(sv string, sep string) []string {
|
|
filtered := make([]string, 0)
|
|
for _, part := range strings.Split(sv, sep) {
|
|
part = Trim(part)
|
|
if part != "" {
|
|
filtered = append(filtered, part)
|
|
}
|
|
}
|
|
return filtered
|
|
|
|
}
|
|
|
|
func CommaSplit(csv string) []string {
|
|
return SepSplit(csv, ",")
|
|
}
|
|
|
|
func ExecSilent(executable string, args []string) (string, error) {
|
|
path, err := exec.LookPath(executable)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
raw, err := exec.Command(path, args...).CombinedOutput()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return Trim(string(raw)), nil
|
|
}
|
|
|
|
func Exec(executable string, args []string) (string, error) {
|
|
out, err := ExecSilent(executable, args)
|
|
if err != nil {
|
|
fmt.Printf("ERROR for '%s %s': %s\n", executable, args, err)
|
|
}
|
|
return out, err
|
|
}
|
|
|
|
func Exists(path string) bool {
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func ExpandPath(path string) (string, error) {
|
|
// Check if path is empty
|
|
if path != "" {
|
|
if strings.HasPrefix(path, "~") {
|
|
if usr, err := user.Current(); err != nil {
|
|
return "", err
|
|
}
|
|
// Replace only the first occurrence of ~
|
|
path = strings.Replace(path, "~", usr.HomeDir, 1)
|
|
}
|
|
return filepath.Abs(path)
|
|
}
|
|
return "", nil
|
|
}
|