Skip to content

Commit 3cf53d8

Browse files
committed
add utils
1 parent 5d12151 commit 3cf53d8

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

utils/exec.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @Author: leafney
3+
* @Date: 2022-12-23 20:29
4+
* @Project: rpi-monitor
5+
* @HomePage: https://github.com/leafney
6+
* @Description:
7+
*/
8+
9+
package utils
10+
11+
import (
12+
"os/exec"
13+
"strings"
14+
)
15+
16+
// RunCommand execute shell command
17+
func RunCommand(str string) (string, error) {
18+
cmd := exec.Command("/bin/sh", "-c", str)
19+
outBytes, err := cmd.CombinedOutput()
20+
if err != nil {
21+
return "", err
22+
}
23+
24+
// Clean the output and remove special characters
25+
outStr := strings.TrimSpace(string(outBytes))
26+
return outStr, nil
27+
}

utils/utils.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @Author: leafney
3+
* @Date: 2022-12-23 20:24
4+
* @Project: rpi-monitor
5+
* @HomePage: https://github.com/leafney
6+
* @Description:
7+
*/
8+
9+
package utils
10+
11+
import (
12+
"os"
13+
"strconv"
14+
)
15+
16+
func StrToFloat64(s string) float64 {
17+
if s == "" {
18+
return 0.0
19+
}
20+
if i, err := strconv.ParseFloat(s, 64); err != nil {
21+
return 0.0
22+
} else {
23+
return i
24+
}
25+
}
26+
27+
func StrToFloat64WithDef(s string, def float64) float64 {
28+
if s == "" {
29+
return def
30+
}
31+
if i, err := strconv.ParseFloat(s, 64); err != nil {
32+
return def
33+
} else {
34+
return i
35+
}
36+
}
37+
38+
func FIsExist(path string) bool {
39+
_, err := os.Stat(path)
40+
return err == nil || os.IsExist(err)
41+
}

0 commit comments

Comments
 (0)