i3-blocks-go/blocks/wireguard/main.go
Simon Vieille 79fe14e059
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
use flag instead os os.Args
2022-08-30 15:42:28 +02:00

64 lines
1.1 KiB
Go

package main
import (
"flag"
"fmt"
r "gitnet.fr/deblan/i3-blocks-go/rendering"
"net"
"os"
"os/exec"
"strings"
)
func main() {
argIface := flag.String("iface", "wg0", "the iface")
argName := flag.String("name", "wg0", "the name")
flag.Parse()
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("black1")
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)
}