forked from davidscholberg/goblocks
/
iface.go
111 lines (97 loc) · 2.4 KB
/
iface.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package modules
import (
"bytes"
"fmt"
"github.com/davidscholberg/go-i3barjson"
"net"
"text/template"
)
// Interface represents the configuration for the network interface block.
type Interface struct {
BlockConfigBase `yaml:",inline"`
IfaceName string `yaml:"interface_name"`
IfaceFormat string `yaml:"interface_format"`
}
// ifaceInfo contains the status info for the interface being monitored.
// The field names correspond directly to the template fields in
// Interface.IfaceFormat.
type ifaceInfo struct {
Status string
Ipv4Addr string
Ipv4Cidr string
Ipv4LocalAddr string
Ipv4LocalCidr string
Ipv6Addr string
Ipv6Cidr string
Ipv6LocalAddr string
Ipv6LocalCidr string
}
// UpdateBlock updates the network interface block.
func (c Interface) UpdateBlock(b *i3barjson.Block) {
var info ifaceInfo
b.Color = c.Color
fullTextFmt := fmt.Sprintf("%s%%s", c.Label)
// set default interface_format for backwards compat
if c.IfaceFormat == "" {
c.IfaceFormat = "{{.Status}}"
}
iface, err := net.InterfaceByName(c.IfaceName)
if err != nil {
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, err.Error())
return
}
if iface.Flags&net.FlagUp != 0 {
b.Urgent = false
info.Status = "up"
} else {
b.Urgent = true
info.Status = "down"
}
addrs, err := iface.Addrs()
if err != nil {
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, err.Error())
return
}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, err.Error())
return
}
// Checking for address family
if ip.To4() != nil {
if ip.IsLinkLocalUnicast() {
info.Ipv4LocalAddr = ip.String()
info.Ipv4LocalCidr = addr.String()
} else {
info.Ipv4Addr = ip.String()
info.Ipv4Cidr = addr.String()
}
} else {
if ip.IsLinkLocalUnicast() {
info.Ipv6LocalAddr = ip.String()
info.Ipv6LocalCidr = addr.String()
} else {
info.Ipv6Addr = ip.String()
info.Ipv6Cidr = addr.String()
}
}
}
t, err := template.New("iface").Parse(c.IfaceFormat)
if err != nil {
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, err.Error())
return
}
buf := new(bytes.Buffer)
err = t.Execute(buf, info)
if err != nil {
b.Urgent = true
b.FullText = fmt.Sprintf(fullTextFmt, err.Error())
return
}
b.FullText = fmt.Sprintf(fullTextFmt, buf.String())
}