mirror of
https://codeberg.org/lauralani/ipam.git
synced 2024-11-27 22:00:01 +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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// addCmd represents the add command
|
|
||||||
var ipaddCmd = &cobra.Command{
|
var ipaddCmd = &cobra.Command{
|
||||||
Use: "add",
|
Use: "add ipaddress [hostname]",
|
||||||
Short: "A brief description of your command",
|
Short: "Add new IP address",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `Add new IP address`,
|
||||||
and usage of using your command. For example:
|
Aliases: []string{"a"},
|
||||||
|
Args: cobra.RangeArgs(1, 2),
|
||||||
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.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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")
|
||||||
|
}
|
12
cmd/ip.go
12
cmd/ip.go
|
@ -12,14 +12,10 @@ import (
|
||||||
|
|
||||||
// ipCmd represents the ip command
|
// ipCmd represents the ip command
|
||||||
var ipCmd = &cobra.Command{
|
var ipCmd = &cobra.Command{
|
||||||
Use: "ip",
|
Use: "ip",
|
||||||
Short: "manage ip addresses",
|
Short: "manage ip addresses",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `Add, delete, edit and show IP addresses`,
|
||||||
and usage of using your command. For example:
|
Aliases: []string{"i"},
|
||||||
|
|
||||||
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.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("ip called")
|
fmt.Println("ip called")
|
||||||
},
|
},
|
||||||
|
|
|
@ -6,32 +6,28 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// addCmd represents the add command
|
// addCmd represents the add command
|
||||||
var subnetaddCmd = &cobra.Command{
|
var subnetaddCmd = &cobra.Command{
|
||||||
Use: "add [subnet] [vlan]",
|
Use: "add subnet [vlan]",
|
||||||
Short: "Add a new subnet",
|
Short: "Add a new subnet",
|
||||||
Long: `Add a new subnet`,
|
Long: `Add a new subnet`,
|
||||||
|
Args: cobra.RangeArgs(1, 2),
|
||||||
|
Aliases: []string{"a"},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var subnet string
|
var subnet string
|
||||||
var vlanid string
|
var vlanid string
|
||||||
|
var netname 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
subnet = args[0]
|
subnet = args[0]
|
||||||
|
|
||||||
fmt.Printf("VLAN Tag: ")
|
vlanid = "-"
|
||||||
fmt.Scan(&vlanid)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) == 2 {
|
if len(args) == 2 {
|
||||||
|
@ -39,7 +35,34 @@ var subnetaddCmd = &cobra.Command{
|
||||||
vlanid = args[1]
|
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
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,11 @@ import (
|
||||||
|
|
||||||
// deleteCmd represents the delete command
|
// deleteCmd represents the delete command
|
||||||
var subnetdeleteCmd = &cobra.Command{
|
var subnetdeleteCmd = &cobra.Command{
|
||||||
Use: "delete",
|
Use: "delete",
|
||||||
Short: "delete subnet",
|
Short: "delete subnet",
|
||||||
Long: `Delete a subnet from the cmdb.`,
|
Long: `Delete a subnet from the cmdb.`,
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
Aliases: []string{"d"},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
fmt.Println("Error: Too few arguments!")
|
fmt.Println("Error: Too few arguments!")
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||||
|
|
||||||
*/
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
|
@ -12,14 +11,11 @@ import (
|
||||||
|
|
||||||
// listCmd represents the list command
|
// listCmd represents the list command
|
||||||
var subnetlistCmd = &cobra.Command{
|
var subnetlistCmd = &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "A brief description of your command",
|
Short: "List all subnets",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `List all subnets`,
|
||||||
and usage of using your command. For example:
|
Aliases: []string{"l"},
|
||||||
|
Args: cobra.ExactArgs(0),
|
||||||
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.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("subnet list called")
|
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.
|
Cobra is a CLI library for Go that empowers applications.
|
||||||
This application is a tool to generate the needed files
|
This application is a tool to generate the needed files
|
||||||
to quickly create a Cobra application.`,
|
to quickly create a Cobra application.`,
|
||||||
|
Aliases: []string{"s"},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("subnet called")
|
fmt.Println("subnet called")
|
||||||
},
|
},
|
||||||
|
|
4
go.mod
4
go.mod
|
@ -2,8 +2,10 @@ module git.sr.ht/~lauralani/cmdb
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
|
require github.com/spf13/cobra v1.6.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/spf13/cobra v1.6.1
|
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
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 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
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/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=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
Loading…
Reference in a new issue