2023-03-10 17:52:20 +01:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-13 17:50:16 +01:00
|
|
|
"net/netip"
|
2023-03-12 13:18:54 +01:00
|
|
|
"os"
|
2023-03-10 17:52:20 +01:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// addCmd represents the add command
|
|
|
|
var subnetaddCmd = &cobra.Command{
|
2023-03-13 17:50:16 +01:00
|
|
|
Use: "add subnet subnet-name [vlan]",
|
2023-03-12 13:18:54 +01:00
|
|
|
Short: "Add a new subnet",
|
|
|
|
Long: `Add a new subnet`,
|
2023-03-13 17:50:16 +01:00
|
|
|
Args: cobra.RangeArgs(2, 3),
|
2023-03-12 13:18:54 +01:00
|
|
|
Aliases: []string{"a"},
|
2023-03-10 17:52:20 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
var subnet string
|
|
|
|
var vlanid string
|
2023-03-12 13:18:54 +01:00
|
|
|
var netname string
|
2023-03-10 17:52:20 +01:00
|
|
|
|
2023-03-13 17:50:16 +01:00
|
|
|
if len(args) == 2 {
|
2023-03-10 17:52:20 +01:00
|
|
|
subnet = args[0]
|
2023-03-13 17:50:16 +01:00
|
|
|
netname = args[1]
|
2023-03-12 13:18:54 +01:00
|
|
|
vlanid = "-"
|
2023-03-10 17:52:20 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 17:50:16 +01:00
|
|
|
if len(args) == 3 {
|
2023-03-10 17:52:20 +01:00
|
|
|
subnet = args[0]
|
2023-03-13 17:50:16 +01:00
|
|
|
netname = args[1]
|
|
|
|
vlanid = args[2]
|
2023-03-10 17:52:20 +01:00
|
|
|
}
|
|
|
|
|
2023-03-12 13:18:54 +01:00
|
|
|
// Parse subnet into ParseCIDR to test if it's a valid subnet
|
2023-03-13 17:50:16 +01:00
|
|
|
// _, ipnet, err := net.ParseCIDR(subnet)
|
2023-03-14 17:21:49 +01:00
|
|
|
ipnet, parseerr := netip.ParsePrefix(subnet)
|
2023-03-13 17:50:16 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2023-03-12 13:18:54 +01:00
|
|
|
|
|
|
|
// Exit if parsed value is no valid IP
|
2023-03-14 17:21:49 +01:00
|
|
|
if parseerr != nil {
|
|
|
|
fmt.Println("[ERROR]", parseerr)
|
2023-03-12 13:18:54 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit if parsed value is an IPv6 Address
|
|
|
|
// TODO: Implement IPv6 support
|
2023-03-13 17:50:16 +01:00
|
|
|
if !ipnet.Addr().Is4() {
|
2023-03-12 13:18:54 +01:00
|
|
|
fmt.Printf("[ERROR] IPv6 is not yet supported!\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:50:16 +01:00
|
|
|
subnetobject := Subnet{}
|
|
|
|
subnetobject.Subnet = ipnet
|
|
|
|
subnetobject.Name = netname
|
|
|
|
subnetobject.Vlan = vlanid
|
|
|
|
|
|
|
|
writeerr := WriteSubnet(subnetobject)
|
|
|
|
|
|
|
|
if writeerr != nil {
|
2023-03-14 17:21:49 +01:00
|
|
|
fmt.Println("[ERROR]", writeerr)
|
2023-03-13 17:50:16 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-03-12 13:18:54 +01:00
|
|
|
|
2023-03-14 17:21:49 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
|
|
// and all subcommands, e.g.:
|
|
|
|
// addCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|