"Fossies" - the Fresh Open Source Software Archive 
Member "AdGuardHome-0.104.3/internal/util/helpers.go" (19 Nov 2020, 1799 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "helpers.go":
0.104.1_vs_0.104.3.
1 // Package util contains various utilities.
2 //
3 // TODO(a.garipov): Such packages are widely considered an antipattern. Remove
4 // this when we refactor our project structure.
5 package util
6
7 import (
8 "fmt"
9 "io/ioutil"
10 "os"
11 "os/exec"
12 "path"
13 "runtime"
14 "strings"
15 )
16
17 // ContainsString checks if string is in the slice of strings.
18 func ContainsString(strs []string, str string) bool {
19 for _, s := range strs {
20 if s == str {
21 return true
22 }
23 }
24 return false
25 }
26
27 // FileExists returns true if file exists.
28 func FileExists(fn string) bool {
29 _, err := os.Stat(fn)
30 return err == nil
31 }
32
33 // RunCommand runs shell command.
34 func RunCommand(command string, arguments ...string) (int, string, error) {
35 cmd := exec.Command(command, arguments...)
36 out, err := cmd.Output()
37 if err != nil {
38 return 1, "", fmt.Errorf("exec.Command(%s) failed: %v: %s", command, err, string(out))
39 }
40
41 return cmd.ProcessState.ExitCode(), string(out), nil
42 }
43
44 func FuncName() string {
45 pc := make([]uintptr, 10) // at least 1 entry needed
46 runtime.Callers(2, pc)
47 f := runtime.FuncForPC(pc[0])
48 return path.Base(f.Name())
49 }
50
51 // SplitNext - split string by a byte and return the first chunk
52 // Skip empty chunks
53 // Whitespace is trimmed
54 func SplitNext(str *string, splitBy byte) string {
55 i := strings.IndexByte(*str, splitBy)
56 s := ""
57 if i != -1 {
58 s = (*str)[0:i]
59 *str = (*str)[i+1:]
60 k := 0
61 ch := rune(0)
62 for k, ch = range *str {
63 if byte(ch) != splitBy {
64 break
65 }
66 }
67 *str = (*str)[k:]
68 } else {
69 s = *str
70 *str = ""
71 }
72 return strings.TrimSpace(s)
73 }
74
75 // IsOpenWRT checks if OS is OpenWRT.
76 func IsOpenWRT() bool {
77 if runtime.GOOS != "linux" {
78 return false
79 }
80
81 body, err := ioutil.ReadFile("/etc/os-release")
82 if err != nil {
83 return false
84 }
85
86 return strings.Contains(string(body), "OpenWrt")
87 }