ipam/cmd/ip-add.go

101 lines
2.7 KiB
Go
Raw Normal View History

2023-03-10 17:52:20 +01:00
/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
*/
package cmd
import (
2023-03-25 15:16:26 +01:00
"fmt"
"net/netip"
"os"
"os/user"
"time"
2023-03-10 17:52:20 +01:00
2023-03-25 15:16:26 +01:00
"github.com/spf13/cobra"
2023-03-10 17:52:20 +01:00
)
var ipaddCmd = &cobra.Command{
2023-03-25 15:16:26 +01:00
Use: "add ipaddress [hostname]",
Short: "Add new IP address",
Long: `Add new IP address`,
Aliases: []string{"a"},
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
var ipaddress, hostname string
if len(args) == 1 {
ipaddress = args[0]
hostname = ""
} else {
ipaddress = args[0]
hostname = args[1]
}
ip, parseerr := netip.ParseAddr(ipaddress)
// Exit if parsed value is no valid IP
if parseerr != nil {
fmt.Println("[ERROR]", parseerr)
os.Exit(1)
}
// Exit if parsed value is an IPv6 Address
// TODO: Implement IPv6 support
2023-03-25 16:51:07 +01:00
//if !ip.Is4() {
// fmt.Printf("[ERROR] IPv6 is not yet supported!\n")
// os.Exit(1)
//}
2023-03-25 15:16:26 +01:00
subnet, subnetexists := FindBestSubnet(ip)
if !subnetexists {
fmt.Printf("[ERROR] Found no suitable subnet for IP %v\n", ipaddress)
fmt.Printf("[ERROR] Maybe you need to add it first?\n")
os.Exit(1)
}
if subnet.HasIP(ip) {
fmt.Printf("[ERROR] IP %v already exists in subnet %v\n", ip.String(), subnet.Subnet.String())
os.Exit(1)
}
currentuser, _ := user.Current()
2023-03-25 16:51:07 +01:00
timestamp := time.Now()
subnet.Addresses = append(subnet.Addresses, Address{ip, hostname, timestamp, currentuser.Username})
subnet.ChangedBy = currentuser.Username
subnet.ChangedAt = timestamp
2023-03-25 15:16:26 +01:00
writeerr := subnet.WriteSubnet()
if writeerr != nil {
fmt.Println("[ERROR]", writeerr)
os.Exit(1)
}
if hostname == "" {
fmt.Printf("added ip:\nip: %v\n", ipaddress)
} else {
fmt.Printf("added ip:\nip: %v\nhostname: %v\n", ipaddress, hostname)
dnserr := AddDNSFqdn(hostname, ip)
if dnserr != nil {
fmt.Println("[ERROR]", writeerr)
os.Exit(1)
}
}
},
2023-03-10 17:52:20 +01:00
}
func init() {
2023-03-25 15:16:26 +01:00
ipCmd.AddCommand(ipaddCmd)
2023-03-10 17:52:20 +01:00
2023-03-25 15:16:26 +01:00
// Here you will define your flags and configuration settings.
2023-03-10 17:52:20 +01:00
2023-03-25 15:16:26 +01:00
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addCmd.PersistentFlags().String("foo", "", "A help for foo")
2023-03-10 17:52:20 +01:00
2023-03-25 15:16:26 +01:00
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
2023-03-10 17:52:20 +01:00
}