/
zfs.go
50 lines (42 loc) · 1.06 KB
/
zfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package modules
import (
"fmt"
"github.com/davidscholberg/go-i3barjson"
"os/exec"
"strings"
)
// Zfs represents the configuration data for the ZFS block
type Zfs struct {
BlockConfigBase `yaml:",inline"`
PoolName string `yaml:"zpool_name"`
}
// UpdateBlock updates the ZFS block
func (c Zfs) UpdateBlock(b *i3barjson.Block) {
b.Color = c.Color
fullTextFmt := fmt.Sprintf("%s%%s", c.Label)
zpoolCmd := exec.Command("sudo", "zpool", "status", c.PoolName)
out, err := zpoolCmd.Output()
if err != nil {
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, err.Error())
return
}
zpoolLines := strings.Split(string(out), "\n")
for _, zpoolLine := range zpoolLines {
line := strings.TrimSpace(zpoolLine)
if strings.HasPrefix(line, "state") {
split := strings.Split(line, ":")
status := strings.TrimSpace(split[1])
if status == "ONLINE" {
b.Urgent = false
} else {
b.Urgent = true
}
b.FullText = fmt.Sprintf(fullTextFmt, status)
return
}
}
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, "NOT FOUND")
return
}