ipam/cmd/subnet-add.go

88 lines
1.8 KiB
Go
Raw Normal View History

2023-03-10 17:52:20 +01:00
/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
*/
package cmd
import (
"fmt"
"net/netip"
"os"
"os/user"
"time"
2023-03-10 17:52:20 +01:00
"github.com/spf13/cobra"
2023-03-10 17:52:20 +01:00
)
// addCmd represents the add command
var subnetaddCmd = &cobra.Command{
Use: "add subnet subnet-name [vlan]",
Short: "Add a new subnet",
Long: `Add a new subnet`,
Args: cobra.RangeArgs(2, 3),
Aliases: []string{"a"},
Run: func(cmd *cobra.Command, args []string) {
var subnet string
var vlanid string
var netname string
if len(args) == 2 {
subnet = args[0]
netname = args[1]
vlanid = "-"
}
if len(args) == 3 {
subnet = args[0]
netname = args[1]
vlanid = args[2]
}
// Parse subnet into ParseCIDR to test if it's a valid subnet
// _, ipnet, err := net.ParseCIDR(subnet)
ipnet, parseerr := netip.ParsePrefix(subnet)
// Exit if subnet already exists, no need to add it then
if SubnetExists(ipnet) {
fmt.Printf("[ERROR] Subnet already exists: %v\n", subnet)
os.Exit(1)
}
// 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
// if !ipnet.Addr().Is4() {
// fmt.Printf("[ERROR] IPv6 is not yet supported!\n")
// os.Exit(1)
// }
currentuser, _ := user.Current()
subnetobject := Subnet{}
subnetobject.Subnet = ipnet
subnetobject.Name = netname
subnetobject.Vlan = vlanid
subnetobject.ChangedAt = time.Now()
subnetobject.ChangedBy = currentuser.Username
writeerr := subnetobject.WriteSubnet()
if writeerr != nil {
fmt.Println("[ERROR]", writeerr)
os.Exit(1)
}
fmt.Printf("added subnet:\nnet: %v\nname: %v\nvlan: %v\n", subnet, netname, vlanid)
},
2023-03-10 17:52:20 +01:00
}
func init() {
subnetCmd.AddCommand(subnetaddCmd)
2023-03-10 17:52:20 +01:00
}