mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-30 04:19:28 +03:00
0d67aa251d
Merge in DNS/adguard-home from 2546-updater-fix to master Closes #2546. Squashed commit of the following: commit af243c9fad710efe099506fda281e628c3e5ec30 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Jan 13 14:33:37 2021 +0300 updater: fix go 1.14 compat commit 742fba24b300ce51c04acb586996c3c75e56ea20 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Jan 13 13:58:27 2021 +0300 util: imp error check commit c2bdbce8af657a7f4b7e05c018cfacba86e06753 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Jan 11 18:51:26 2021 +0300 all: fix and refactor update checking
79 lines
1.6 KiB
Go
79 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 || !os.IsNotExist(err)
|
|
}
|
|
|
|
// 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")
|
|
}
|