mirror of
https://codeberg.org/lauralani/ipam.git
synced 2024-11-23 20:20:39 +01:00
add commands, aliasses and some validations
This commit is contained in:
parent
8f462aa520
commit
3590ec1c1e
13 changed files with 214 additions and 45 deletions
10
classes/classes.go
Normal file
10
classes/classes.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
/*
|
||||
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||
*/
|
||||
|
||||
package classes
|
||||
|
||||
type Subnet struct {
|
||||
subnet string
|
||||
name string
|
||||
}
|
|
@ -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
|
||||
},
|
||||
}
|
||||
|
||||
|
|
36
cmd/ip-delete.go
Normal file
36
cmd/ip-delete.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||
*/
|
||||
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")
|
||||
}
|
35
cmd/ip-edit.go
Normal file
35
cmd/ip-edit.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||
*/
|
||||
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")
|
||||
}
|
36
cmd/ip-show.go
Normal file
36
cmd/ip-show.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||
*/
|
||||
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")
|
||||
}
|
|
@ -14,12 +14,8 @@ import (
|
|||
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.`,
|
||||
Long: `Add, delete, edit and show IP addresses`,
|
||||
Aliases: []string{"i"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("ip called")
|
||||
},
|
||||
|
|
|
@ -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]",
|
||||
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]
|
||||
}
|
||||
|
||||
// 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
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,8 @@ var subnetdeleteCmd = &cobra.Command{
|
|||
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!")
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
/*
|
||||
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||
|
||||
*/
|
||||
package cmd
|
||||
|
||||
|
@ -13,13 +12,10 @@ 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.`,
|
||||
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")
|
||||
},
|
||||
|
|
|
@ -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")
|
||||
},
|
||||
|
|
4
go.mod
4
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
|
||||
)
|
||||
|
|
1
go.sum
1
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=
|
||||
|
|
Loading…
Reference in a new issue