ipam/cmd/ip-delete.go

76 lines
1.7 KiB
Go
Raw Normal View History

/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
*/
package cmd
import (
2023-03-15 16:43:20 +01:00
"fmt"
"net/netip"
"os"
"github.com/spf13/cobra"
)
// deleteCmd represents the delete command
var ipdeleteCmd = &cobra.Command{
Use: "delete ipaddress",
Short: "Delete an IP address",
Long: `Delete an IP address`,
Aliases: []string{"d"},
Args: cobra.ExactArgs(1),
2023-03-14 17:21:49 +01:00
Example: "ipam ip delete 192.168.0.1",
Run: func(cmd *cobra.Command, args []string) {
2023-03-15 16:43:20 +01:00
ip, parseerr := netip.ParseAddr(args[0])
if parseerr != nil {
fmt.Println("[ERROR]", parseerr)
os.Exit(1)
}
subnet, subnetexists := FindBestSubnet(ip)
if !subnetexists {
fmt.Printf("[ERROR] Couldn't find IP %v\n", ip.String())
os.Exit(1)
}
address, _ := subnet.GetIP(ip)
subnet, removeerr := subnet.RemoveIP(ip)
if removeerr != nil {
fmt.Println("[ERROR]", removeerr)
os.Exit(1)
}
2023-03-24 16:32:19 +01:00
writeerr := subnet.WriteSubnet()
2023-03-15 16:43:20 +01:00
if writeerr != nil {
fmt.Println("[ERROR]", writeerr)
os.Exit(1)
}
if address.FQDN == "" {
fmt.Printf("deleted ip %v\n", address.IP.String())
} else {
fmt.Printf("deleted ip %v (%v)\n", address.IP.String(), address.FQDN)
2023-03-23 13:53:46 +01:00
dnserr := DeleteDNSFqdn(address.FQDN, address.IP)
if dnserr != nil {
fmt.Println("[ERROR]", writeerr)
os.Exit(1)
}
2023-03-15 16:43:20 +01:00
}
},
}
func init() {
ipCmd.AddCommand(ipdeleteCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}