2020-11-16 19:01:12 +03:00
|
|
|
// Package util contains various utilities.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Such packages are widely considered an antipattern. Remove
|
|
|
|
// this when we refactor our project structure.
|
2020-02-13 18:42:07 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2021-03-01 17:20:16 +03:00
|
|
|
"bytes"
|
2020-03-20 15:05:43 +03:00
|
|
|
"io/ioutil"
|
2021-03-01 17:20:16 +03:00
|
|
|
"path/filepath"
|
2020-02-13 18:42:07 +03:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SplitNext - split string by a byte and return the first chunk
|
2020-03-20 15:05:43 +03:00
|
|
|
// Skip empty chunks
|
2020-02-13 18:42:07 +03:00
|
|
|
// 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:]
|
2020-03-20 15:05:43 +03:00
|
|
|
k := 0
|
|
|
|
ch := rune(0)
|
|
|
|
for k, ch = range *str {
|
|
|
|
if byte(ch) != splitBy {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*str = (*str)[k:]
|
2020-02-13 18:42:07 +03:00
|
|
|
} else {
|
|
|
|
s = *str
|
|
|
|
*str = ""
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(s)
|
|
|
|
}
|
2020-03-17 15:00:40 +03:00
|
|
|
|
2021-03-12 14:32:08 +03:00
|
|
|
// IsOpenWrt returns true if host OS is OpenWrt.
|
|
|
|
func IsOpenWrt() bool {
|
2020-03-20 15:05:43 +03:00
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-01 17:20:16 +03:00
|
|
|
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)
|
2020-03-20 15:05:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-01 17:20:16 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:38:39 +03:00
|
|
|
var body []byte
|
|
|
|
body, err = ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name()))
|
2021-03-01 17:20:16 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Contains(body, osNameData) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|