From 3590ec1c1eb1daf59bf864ffa12958408427a2a9 Mon Sep 17 00:00:00 2001 From: lauralani Date: Sun, 12 Mar 2023 13:18:54 +0100 Subject: [PATCH] add commands, aliasses and some validations --- classes/classes.go | 10 ++++++++ cmd/ip-add.go | 51 +++++++++++++++++++++++++++++-------- cmd/ip-delete.go | 36 ++++++++++++++++++++++++++ cmd/ip-edit.go | 35 +++++++++++++++++++++++++ cmd/ip-show.go | 36 ++++++++++++++++++++++++++ cmd/ip.go | 12 +++------ cmd/subnet-add.go | 51 +++++++++++++++++++++++++++---------- cmd/subnet-delete.go | 8 +++--- cmd/subnet-list.go | 14 ++++------ cmd/subnet.go | 1 + go.mod | 4 ++- go.sum | 1 + {lib => storage}/storage.go | 0 13 files changed, 214 insertions(+), 45 deletions(-) create mode 100644 classes/classes.go create mode 100644 cmd/ip-delete.go create mode 100644 cmd/ip-edit.go create mode 100644 cmd/ip-show.go rename {lib => storage}/storage.go (100%) diff --git a/classes/classes.go b/classes/classes.go new file mode 100644 index 0000000..92452cb --- /dev/null +++ b/classes/classes.go @@ -0,0 +1,10 @@ +/* +Copyright © 2023 Laura Kalb +*/ + +package classes + +type Subnet struct { + subnet string + name string +} diff --git a/cmd/ip-add.go b/cmd/ip-add.go index 87c0fb7..82ddddb 100644 --- a/cmd/ip-add.go +++ b/cmd/ip-add.go @@ -6,22 +6,53 @@ package cmd import ( "fmt" + "net" + "os" "github.com/spf13/cobra" ) -// addCmd represents the add command var ipaddCmd = &cobra.Command{ - Use: "add", - 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: "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) { - fmt.Println("ip add called") + var ipaddress, hostname string + + if len(args) == 1 { + ipaddress = args[0] + hostname = "" + } else { + ipaddress = args[0] + hostname = args[1] + } + + ip := net.ParseIP(ipaddress) + + // Exit if parsed value is no valid IP + if ip == nil { + fmt.Printf("[ERROR] not a valid IP: %v\n", ipaddress) + os.Exit(1) + } + + // Exit if parsed value is an IPv6 Address + // TODO: Implement IPv6 support + if ip.To4() == nil { + fmt.Printf("[ERROR] IPv6 is not yet supported!\n") + os.Exit(1) + } + + // TODO: Check if there is already a subnet that can contain this IP, err if not + + if hostname == "" { + fmt.Printf("Adding IP %v\n", ipaddress) + } else { + fmt.Printf("Adding IP %v with hostname %v\n", ipaddress, hostname) + } + + // TODO: Save to file }, } diff --git a/cmd/ip-delete.go b/cmd/ip-delete.go new file mode 100644 index 0000000..632057a --- /dev/null +++ b/cmd/ip-delete.go @@ -0,0 +1,36 @@ +/* +Copyright © 2023 Laura Kalb +*/ +package cmd + +import ( + "fmt" + + "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), + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("ip delete called") + }, +} + +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") +} diff --git a/cmd/ip-edit.go b/cmd/ip-edit.go new file mode 100644 index 0000000..2c56b03 --- /dev/null +++ b/cmd/ip-edit.go @@ -0,0 +1,35 @@ +/* +Copyright © 2023 Laura Kalb +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var ipeditCmd = &cobra.Command{ + Use: "edit", + Short: "Edit an IP address", + Long: `Edit an IP address`, + Aliases: []string{"e"}, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("edit called") + }, +} + +func init() { + ipCmd.AddCommand(ipeditCmd) + + // 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/ip-show.go b/cmd/ip-show.go new file mode 100644 index 0000000..c27c2d1 --- /dev/null +++ b/cmd/ip-show.go @@ -0,0 +1,36 @@ +/* +Copyright © 2023 Laura Kalb +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// showCmd represents the show command +var ipshowCmd = &cobra.Command{ + Use: "show", + Short: "Show IP and associated name", + Long: `Show IP and associated name`, + Aliases: []string{"s"}, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("ip show called") + }, +} + +func init() { + ipCmd.AddCommand(ipshowCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // showCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // showCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/ip.go b/cmd/ip.go index d7ce7bd..e9a397c 100644 --- a/cmd/ip.go +++ b/cmd/ip.go @@ -12,14 +12,10 @@ import ( // ipCmd represents the ip command var ipCmd = &cobra.Command{ - Use: "ip", - Short: "manage ip addresses", - 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: "ip", + Short: "manage ip addresses", + Long: `Add, delete, edit and show IP addresses`, + Aliases: []string{"i"}, Run: func(cmd *cobra.Command, args []string) { fmt.Println("ip called") }, diff --git a/cmd/subnet-add.go b/cmd/subnet-add.go index 41c22f8..e682fed 100644 --- a/cmd/subnet-add.go +++ b/cmd/subnet-add.go @@ -6,32 +6,28 @@ package cmd import ( "fmt" + "net" + "os" "github.com/spf13/cobra" ) // addCmd represents the add command var subnetaddCmd = &cobra.Command{ - Use: "add [subnet] [vlan]", - Short: "Add a new subnet", - Long: `Add a new subnet`, + Use: "add subnet [vlan]", + Short: "Add a new subnet", + Long: `Add a new subnet`, + Args: cobra.RangeArgs(1, 2), + Aliases: []string{"a"}, Run: func(cmd *cobra.Command, args []string) { var subnet string var vlanid string - - if len(args) == 0 { - fmt.Printf("IP Subnet (Format: 192.168.0.0/24): ") - fmt.Scan(&subnet) - - fmt.Printf("VLAN Tag: ") - fmt.Scan(&vlanid) - } + var netname string if len(args) == 1 { subnet = args[0] - fmt.Printf("VLAN Tag: ") - fmt.Scan(&vlanid) + vlanid = "-" } if len(args) == 2 { @@ -39,7 +35,34 @@ var subnetaddCmd = &cobra.Command{ vlanid = args[1] } - fmt.Printf("Adding Subnet %v with VLAN Tag %v.\n", subnet, vlanid) + // Parse subnet into ParseCIDR to test if it's a valid subnet + _, ipnet, err := net.ParseCIDR(subnet) + + // Exit if parsed value is no valid IP + if err != nil { + fmt.Println("[ERROR]", err) + os.Exit(1) + } + + // Exit if parsed value is an IPv6 Address + // TODO: Implement IPv6 support + if ipnet.IP.To4() == nil { + fmt.Printf("[ERROR] IPv6 is not yet supported!\n") + os.Exit(1) + } + + // Ask for Subnet Name + // TODO: Check if net name only contains letters, numbers and hyphens + fmt.Printf("Subnet name: ") + fmt.Scan(&netname) + + if vlanid == "-" { + fmt.Printf("Adding Subnet %v.\n", subnet) + } else { + fmt.Printf("Adding Subnet %v with VLAN Tag %v.\n", subnet, vlanid) + } + + // TODO: Save subnet to file }, } diff --git a/cmd/subnet-delete.go b/cmd/subnet-delete.go index 5644b44..92db756 100644 --- a/cmd/subnet-delete.go +++ b/cmd/subnet-delete.go @@ -12,9 +12,11 @@ import ( // deleteCmd represents the delete command var subnetdeleteCmd = &cobra.Command{ - Use: "delete", - Short: "delete subnet", - Long: `Delete a subnet from the cmdb.`, + Use: "delete", + Short: "delete subnet", + Long: `Delete a subnet from the cmdb.`, + Args: cobra.ExactArgs(1), + Aliases: []string{"d"}, Run: func(cmd *cobra.Command, args []string) { if len(args) < 1 { fmt.Println("Error: Too few arguments!") diff --git a/cmd/subnet-list.go b/cmd/subnet-list.go index 5246cca..bcb0889 100644 --- a/cmd/subnet-list.go +++ b/cmd/subnet-list.go @@ -1,6 +1,5 @@ /* Copyright © 2023 Laura Kalb - */ package cmd @@ -12,14 +11,11 @@ import ( // listCmd represents the list command var subnetlistCmd = &cobra.Command{ - Use: "list", - 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: "list", + Short: "List all subnets", + Long: `List all subnets`, + Aliases: []string{"l"}, + Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { fmt.Println("subnet list called") }, diff --git a/cmd/subnet.go b/cmd/subnet.go index dfc94fd..7a95673 100644 --- a/cmd/subnet.go +++ b/cmd/subnet.go @@ -20,6 +20,7 @@ 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.`, + Aliases: []string{"s"}, Run: func(cmd *cobra.Command, args []string) { fmt.Println("subnet called") }, diff --git a/go.mod b/go.mod index 18fdedf..b20b6d9 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,10 @@ module git.sr.ht/~lauralani/cmdb go 1.20 +require github.com/spf13/cobra v1.6.1 + require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f7673bd..21e2240 100644 --- a/go.sum +++ b/go.sum @@ -8,4 +8,5 @@ github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUq github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/lib/storage.go b/storage/storage.go similarity index 100% rename from lib/storage.go rename to storage/storage.go