From 5af155dfc7690e4e1cb24af36008fc0ca0b330ad Mon Sep 17 00:00:00 2001 From: lauralani Date: Tue, 14 Mar 2023 10:38:08 +0100 Subject: [PATCH] add storage ops, ip add/list and changed classes --- cmd/ip-add.go | 2 +- cmd/ip-list.go | 35 +++++++++++++++++++++++++++++++++ cmd/storage.go | 25 ++++++++++++++++++++---- cmd/subnet-show.go | 48 ++++++++++++++++++++++++++++++++++++---------- 4 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 cmd/ip-list.go diff --git a/cmd/ip-add.go b/cmd/ip-add.go index 1c9f2aa..007ab4f 100644 --- a/cmd/ip-add.go +++ b/cmd/ip-add.go @@ -52,7 +52,7 @@ var ipaddCmd = &cobra.Command{ os.Exit(1) } - subnet.Addresses = append(subnet.Addresses, Address{ip.String(), hostname}) + subnet.Addresses = append(subnet.Addresses, Address{ip, hostname}) writeerr := WriteSubnet(subnet) if writeerr != nil { diff --git a/cmd/ip-list.go b/cmd/ip-list.go new file mode 100644 index 0000000..00ba7d9 --- /dev/null +++ b/cmd/ip-list.go @@ -0,0 +1,35 @@ +/* +Copyright © 2023 Laura Kalb +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var iplistCmd = &cobra.Command{ + Use: "edit", + Short: "List all IP addresses with subnets", + Long: `List all IP addresses with subnets`, + Aliases: []string{"l"}, + Args: cobra.ExactArgs(0), + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("ip list called") + }, +} + +func init() { + ipCmd.AddCommand(iplistCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // editCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // editCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/storage.go b/cmd/storage.go index 9b4d9b8..fe0a3ea 100644 --- a/cmd/storage.go +++ b/cmd/storage.go @@ -9,6 +9,7 @@ import ( "fmt" "net/netip" "os" + "sort" "strings" "github.com/spf13/viper" @@ -22,7 +23,7 @@ type Subnet struct { } type Address struct { - IP string + IP netip.Addr FQDN string } @@ -142,8 +143,9 @@ func WriteSubnet(subnet Subnet) error { } if len(subnet.Addresses) != 0 { - for _, element := range subnet.Addresses { - _, err := file.WriteString(element.IP + ":" + element.FQDN + "\n") + subnetsorted := SortAddresses(subnet.Addresses) + for _, element := range subnetsorted { + _, err := file.WriteString(element.IP.String() + ":" + element.FQDN + "\n") if err != nil { fmt.Println("[ERROR]", err) os.Exit(1) @@ -189,7 +191,8 @@ func GetSubnet(net netip.Prefix) (Subnet, error) { default: s := strings.Split(scanner.Text(), ":") - a := Address{s[0], s[1]} + ip, _ := netip.ParseAddr(s[0]) + a := Address{ip, s[1]} subnet.Addresses = append(subnet.Addresses, a) } counter = counter + 1 @@ -201,3 +204,17 @@ func GetSubnet(net netip.Prefix) (Subnet, error) { return subnet, nil } + +// SortAddresses sorts the given list of IP addresses +// using netip.Addr.Less() and returns the sorted slice. +func SortAddresses(list []Address) []Address { + + if len(list) <= 1 { + return list + } + + sort.Slice(list, func(i, j int) bool { + return list[i].IP.Less(list[j].IP) + }) + return list +} diff --git a/cmd/subnet-show.go b/cmd/subnet-show.go index 61fbadb..7b340e4 100644 --- a/cmd/subnet-show.go +++ b/cmd/subnet-show.go @@ -1,27 +1,55 @@ /* Copyright © 2023 Laura Kalb - */ package cmd import ( "fmt" + "net/netip" + "os" "github.com/spf13/cobra" ) // showCmd represents the show command var subnetshowCmd = &cobra.Command{ - Use: "show", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Use: "show subnet", + Short: "Displays a subnet.", + Long: `Displays a subnets details like name and vlan tag, +aswell as a list of containing IP addresses`, + Args: cobra.ExactArgs(1), + Aliases: []string{"s"}, + Example: "ipam subnet show 192.168.0.0/24\nipam subnet show 2001:db8::/64", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("subnet show called") + net, parseerr := netip.ParsePrefix(args[0]) + + if parseerr != nil { + fmt.Println("[ERROR]", parseerr) + os.Exit(1) + } + + if !SubnetExists(net) { + fmt.Printf("[ERROR] no subnet found for prefix: %v\n", args[0]) + os.Exit(1) + } + + subnet, subneterr := GetSubnet(net) + if subneterr != nil { + fmt.Println("[ERROR]", subneterr) + os.Exit(1) + } + fmt.Printf("\nName: %v\n", subnet.Name) + fmt.Printf("Vlan: %v\n", subnet.Vlan) + fmt.Printf("Prefix: %v\n\n", subnet.Subnet) + + fmt.Printf("%v:\n", subnet.Subnet) + for _, element := range subnet.Addresses { + if element.FQDN == "" { + fmt.Printf("\t%v\n", element.IP.String()) + } else { + fmt.Printf("\t%v: %v\n", element.IP.String(), element.FQDN) + } + } }, }