mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-01-08 23:17:21 +03:00
7e64205d44
Closes #2803.
Squashed commit of the following:
commit cb36cc8811160bb39a32fb8eddf962d0ebe9035a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Mar 12 14:21:46 2021 +0300
home: imp more
commit 9ea7ccec8bb293881cf724d7ad57e6744243d8b9
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Mar 12 13:58:10 2021 +0300
all: imp naming, refactor http srv shutdown
commit f29221007c16fd3e7230bf2c1ac37b365f3e29aa
Merge: 2247c05b bfbf73f3
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Mar 12 13:35:17 2021 +0300
Merge branch 'master' into 2803-shadow-4
commit 2247c05b5521346aaf362d81ccdd64fee31f1e6d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Jan 29 20:53:21 2021 +0300
home: rm var shadowing, vol. 4
109 lines
2.2 KiB
Go
109 lines
2.2 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 (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"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 returns true if host OS is OpenWrt.
|
|
func IsOpenWrt() bool {
|
|
if runtime.GOOS != "linux" {
|
|
return false
|
|
}
|
|
|
|
const etcDir = "/etc"
|
|
|
|
// TODO(e.burkov): Take care of dealing with fs package after updating
|
|
// Go version to 1.16.
|
|
fileInfos, err := ioutil.ReadDir(etcDir)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
// fNameSubstr is a part of a name of the desired file.
|
|
const fNameSubstr = "release"
|
|
osNameData := []byte("OpenWrt")
|
|
|
|
for _, fileInfo := range fileInfos {
|
|
if fileInfo.IsDir() {
|
|
continue
|
|
}
|
|
|
|
if !strings.Contains(fileInfo.Name(), fNameSubstr) {
|
|
continue
|
|
}
|
|
|
|
var body []byte
|
|
body, err = ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name()))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
if bytes.Contains(body, osNameData) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|