mirror of
https://codeberg.org/lauralani/ipam.git
synced 2024-11-23 20:20:39 +01:00
add storage ops, ip add/list and changed classes
This commit is contained in:
parent
0fa1474f6c
commit
5af155dfc7
4 changed files with 95 additions and 15 deletions
|
@ -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 {
|
||||
|
|
35
cmd/ip-list.go
Normal file
35
cmd/ip-list.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||
*/
|
||||
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")
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -1,27 +1,55 @@
|
|||
/*
|
||||
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||
|
||||
*/
|
||||
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)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue