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

64 lines
1.1 KiB
Go
Raw Normal View History

2022-08-28 14:59:15 +02:00
package main
import (
2022-08-30 15:42:28 +02:00
"flag"
2022-08-28 14:59:15 +02:00
"fmt"
r "gitnet.fr/deblan/i3-blocks-go/rendering"
"net"
"os"
"os/exec"
"strings"
)
func main() {
2022-08-30 15:42:28 +02:00
argIface := flag.String("iface", "wg0", "the iface")
argName := flag.String("name", "wg0", "the name")
flag.Parse()
2022-08-28 14:59:15 +02:00
var (
iface *net.Interface
addrs []net.Addr
)
var ip string
2022-08-30 15:42:28 +02:00
iface, err := net.InterfaceByName(*argIface)
2022-08-28 14:59:15 +02:00
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")
2022-08-30 15:42:28 +02:00
command = exec.Command("sudo", "wg-quick", "up", *argIface)
2022-08-28 14:59:15 +02:00
} else {
2022-08-29 19:43:10 +02:00
fb.Foreground = r.Color("black1")
2022-08-29 19:58:52 +02:00
fb.Background = r.Color("green")
2022-08-30 15:42:28 +02:00
command = exec.Command("sudo", "wg-quick", "down", *argIface)
2022-08-28 14:59:15 +02:00
}
if os.Getenv("BLOCK_BUTTON") == "1" {
command.Run()
}
options := r.NewBlockOptions()
2022-08-30 15:42:28 +02:00
options.FullText = r.TextWithPadding(*argName, fb)
block := r.Block(fmt.Sprintf("wireguard_%s", *argIface), options)
2022-08-28 14:59:15 +02:00
fmt.Println(block)
}