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