i3-blocks-go/blocks/ip/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

75 lines
1.3 KiB
Go

package main
import (
"flag"
"fmt"
"github.com/atotto/clipboard"
r "gitnet.fr/deblan/i3-blocks-go/rendering"
"net"
"os"
"regexp"
"strings"
)
func main() {
argIface := flag.String("iface", "eth0", "the iface")
argVersion := flag.String("version", "ip4", "ip4 or ip6")
argName := flag.String("name", "eth0", "the name")
flag.Parse()
var (
iface *net.Interface
addrs []net.Addr
// ip net.IP
)
iface, err := net.InterfaceByName(*argIface)
if err != nil {
return
}
addrs, err = iface.Addrs()
var ip string
for _, addr := range addrs {
if ip == "" {
a := addr.String()
if *argVersion == "ip4" && !strings.Contains(a, ":") {
ip = a
} else if *argVersion == "ip6" && strings.Contains(a, ":") && !strings.Contains(a, "fe80") {
ip = a
}
}
}
if ip == "" {
return
}
reg := regexp.MustCompile(`/\d+$`)
ip = reg.ReplaceAllString(ip, "")
if os.Getenv("BLOCK_BUTTON") == "1" {
clipboard.WriteAll(ip)
}
label := r.TextWithPadding(
*argName,
r.FB{
Foreground: r.Color("grey1"),
Background: r.Color("black2"),
},
)
value := r.TextWithPadding(ip, r.FB{})
options := r.NewBlockOptions()
options.FullText = fmt.Sprintf("%s%s", label, value)
block := r.Block(fmt.Sprintf("ip_%s_%s", argIface, argVersion), options)
fmt.Println(block)
}