Split processing into individual calls, add information and only rename if main option returns anything

This commit is contained in:
Alex D. 2024-08-23 11:40:37 +00:00
parent 6549f3467f
commit 5cdea1d6dd
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
2 changed files with 24 additions and 25 deletions

33
main.go
View File

@ -10,14 +10,15 @@ import (
func main() {
var (
err error
mainlog = log.New(os.Stderr, "net-predictable: ", log.Lmsgprefix|log.Ltime)
ifnparam string
force bool
main IfNameType
err error
mainlog = log.New(os.Stderr, "net-predictable: ", log.Lmsgprefix|log.Ltime)
ifnparam string
force, info bool
main IfNameType
)
flag.StringVar(&ifnparam, "ifname", "", "Interface name to rename")
flag.BoolVar(&force, "force", false, "Force update name of interface")
flag.BoolVar(&info, "info", false, "Show interface details")
flag.IntVar((*int)(&main), "main", int(IfNameMAC), fmt.Sprintf("Main interface name to use: %v",
map[string]IfNameType{
"Kernel": IfNameKern,
@ -46,6 +47,7 @@ func main() {
return
}
mainlog.SetPrefix(fmt.Sprintf("net-predictable (%s): ", ifname))
var l netlink.Link
if l, err = netlink.LinkByName(ifname); err != nil {
mainlog.Fatalln("Interface", ifname, "doesn't exist or disappeared before it could be selected")
@ -61,13 +63,24 @@ func main() {
if should, err = cif.shouldRun(); err != nil {
mainlog.Println(err)
}
if !force && !should {
if !info && (!force && !should) {
mainlog.Println("Interface is already renamed, exiting")
return
}
if err = cif.Process(); err != nil {
mainlog.Println("Failed to process interface:", err)
if err = cif.ProcMAC(); err != nil {
mainlog.Println("Failed to process interface MAC path:", err)
}
if err = cif.ProcLoc(); err != nil {
mainlog.Println("Failed to process interface PCI+USB path:", err)
}
// Just print information
if info {
mainlog.Println("Selected Interface:", ifname)
mainlog.Println("PCI+USB Name:", cif.names[IfNameLoc])
mainlog.Println("MAC Name:", cif.names[IfNameMAC])
return
}
@ -75,11 +88,13 @@ func main() {
// This must be set before the next are following as altname cannot be added if a interface with the same name exists
if ifname == cif.names[main] {
mainlog.Println("Interface already named correctly, skipping")
} else {
} else if cif.names[main] != "" {
mainlog.Printf("Renaming %s to %s\n", ifname, cif.names[main])
if err = netlink.LinkSetName(l, cif.names[main]); err != nil {
mainlog.Fatalf("Could not rename interface %s: %s\n", ifname, err)
}
} else {
mainlog.Println("Main interface naming method is unavailable for this interface, skipping")
}
// Alt names

16
misc.go
View File

@ -89,19 +89,3 @@ func (i iface) shouldRun() (should bool, err error) {
}
return
}
func (i iface) Process() (err error) {
if lerr := i.ProcLoc(); lerr != nil {
err = lerr
}
if lerr := i.ProcMAC(); lerr != nil {
err = lerr
}
// It can error but as long as we get a identifier along the path it's okay
if i.names[IfNameLoc] == "" && i.names[IfNameMAC] == "" {
return
}
err = nil
return
}