i3-blocks-go/blocks/wireguard/main.go

62 lines
1.0 KiB
Go

package main
import (
"fmt"
r "gitnet.fr/deblan/i3-blocks-go/rendering"
"net"
"os"
"os/exec"
"strings"
)
func main() {
argIface := os.Args[1]
argName := os.Args[2]
var (
iface *net.Interface
addrs []net.Addr
)
var ip string
iface, err := net.InterfaceByName(argIface)
if err == nil {
addrs, _ = iface.Addrs()
for _, addr := range addrs {
if ip == "" {
a := addr.String()
if !strings.Contains(a, ":") {
ip = a
}
}
}
}
var fb r.FB
var command *exec.Cmd
if ip == "" {
fb.Foreground = r.Color("grey1")
fb.Background = r.Color("black3")
command = exec.Command("sudo", "wg-quick", "up", argIface)
} else {
fb.Foreground = r.Color("black2")
fb.Background = r.Color("green")
command = exec.Command("sudo", "wg-quick", "down", argIface)
}
if os.Getenv("BLOCK_BUTTON") == "1" {
command.Run()
}
options := r.NewBlockOptions()
options.FullText = r.TextWithPadding(argName, fb)
block := r.Block(fmt.Sprintf("wireguard_%s", argIface), options)
fmt.Println(block)
}