mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-17 19:08:25 -07:00
73c30590e0
Merge in DNS/adguard-home from 2276-fix-lint-4 to master Updates #2276. Squashed commit of the following: commit 15d49184cd8ce1f8701bf3221e69418ca1778b36 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Dec 8 15:51:34 2020 +0300 util: fix naming commit 3b9a86a0feb8c6e0b167e6e23105e8137b0dda76 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Dec 8 15:41:10 2020 +0300 all: fix lint and naming issues vol. 4
80 lines
1.6 KiB
Go
80 lines
1.6 KiB
Go
// Package util contains various utilities.
|
|
//
|
|
// TODO(a.garipov): Such packages are widely considered an antipattern. Remove
|
|
// this when we refactor our project structure.
|
|
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// ContainsString checks if string is in the slice of strings.
|
|
func ContainsString(strs []string, str string) bool {
|
|
for _, s := range strs {
|
|
if s == str {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// FileExists returns true if file exists.
|
|
func FileExists(fn string) bool {
|
|
_, err := os.Stat(fn)
|
|
return err == nil
|
|
}
|
|
|
|
// RunCommand runs shell command.
|
|
func RunCommand(command string, arguments ...string) (int, string, error) {
|
|
cmd := exec.Command(command, arguments...)
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return 1, "", fmt.Errorf("exec.Command(%s) failed: %v: %s", command, err, string(out))
|
|
}
|
|
|
|
return cmd.ProcessState.ExitCode(), string(out), nil
|
|
}
|
|
|
|
// SplitNext - split string by a byte and return the first chunk
|
|
// Skip empty chunks
|
|
// Whitespace is trimmed
|
|
func SplitNext(str *string, splitBy byte) string {
|
|
i := strings.IndexByte(*str, splitBy)
|
|
s := ""
|
|
if i != -1 {
|
|
s = (*str)[0:i]
|
|
*str = (*str)[i+1:]
|
|
k := 0
|
|
ch := rune(0)
|
|
for k, ch = range *str {
|
|
if byte(ch) != splitBy {
|
|
break
|
|
}
|
|
}
|
|
*str = (*str)[k:]
|
|
} else {
|
|
s = *str
|
|
*str = ""
|
|
}
|
|
return strings.TrimSpace(s)
|
|
}
|
|
|
|
// IsOpenWRT checks if OS is OpenWRT.
|
|
func IsOpenWRT() bool {
|
|
if runtime.GOOS != "linux" {
|
|
return false
|
|
}
|
|
|
|
body, err := ioutil.ReadFile("/etc/os-release")
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return strings.Contains(string(body), "OpenWrt")
|
|
}
|