package main import ( "bufio" "fmt" "os" "strings" ) var stdinReader = bufio.NewReader(os.Stdin) // prompt shows label with a "[current]" hint, reads one line, and returns // current unchanged if the user just presses enter. func prompt(label, current string) string { if current != "" { fmt.Printf("%s [%s]: ", label, current) } else { fmt.Printf("%s: ", label) } line, _ := stdinReader.ReadString('\n') line = strings.TrimSpace(line) if line == "" { return current } return line } // interactiveEdit implements the bare `ipadm ` form: add the host // if it doesn't exist yet, otherwise edit it in place. Returns the updated // host list, or an error if validation fails. func interactiveEdit(hosts []Host, name string, cfg netConfig) ([]Host, error) { if err := validateHostname(name); err != nil { return nil, err } idx := findHost(hosts, name) var cur Host if idx >= 0 { cur = hosts[idx] fmt.Printf("Bearbeite bestehenden Host %q (Enter = Wert behalten)\n", name) } else { cur = Host{Name: name} fmt.Printf("Neuer Host %q (Enter bei MAC/Comment = leer lassen)\n", name) } for { ip := prompt("IP-Adresse", cur.IP) if err := validateIP(ip, cfg.lanCIDR, cfg.poolStart, cfg.poolEnd); err != nil { fmt.Fprintln(os.Stderr, "Fehler:", err) continue } cur.IP = ip break } for { mac := prompt("MAC-Adresse (optional, '-' zum Löschen)", macOrDash(cur.MAC)) if mac == "-" { mac = "" } norm, err := normalizeMAC(mac) if err != nil { fmt.Fprintln(os.Stderr, "Fehler:", err) continue } cur.MAC = norm break } cur.Comment = prompt("Kommentar", cur.Comment) if other := findByIP(hosts, cur.IP, idx); other >= 0 { return nil, fmt.Errorf("IP %s ist bereits Host %q zugewiesen", cur.IP, hosts[other].Name) } if other := findByMAC(hosts, cur.MAC, idx); other >= 0 { return nil, fmt.Errorf("MAC %s ist bereits Host %q zugewiesen", cur.MAC, hosts[other].Name) } if idx >= 0 { hosts[idx] = cur } else { hosts = append(hosts, cur) } return hosts, nil } func macOrDash(mac string) string { if mac == "" { return "" } return mac }