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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
subnet.Addresses = append(subnet.Addresses, Address{ip.String(), hostname})
|
subnet.Addresses = append(subnet.Addresses, Address{ip, hostname})
|
||||||
|
|
||||||
writeerr := WriteSubnet(subnet)
|
writeerr := WriteSubnet(subnet)
|
||||||
if writeerr != nil {
|
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"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -22,7 +23,7 @@ type Subnet struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Address struct {
|
type Address struct {
|
||||||
IP string
|
IP netip.Addr
|
||||||
FQDN string
|
FQDN string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,8 +143,9 @@ func WriteSubnet(subnet Subnet) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(subnet.Addresses) != 0 {
|
if len(subnet.Addresses) != 0 {
|
||||||
for _, element := range subnet.Addresses {
|
subnetsorted := SortAddresses(subnet.Addresses)
|
||||||
_, err := file.WriteString(element.IP + ":" + element.FQDN + "\n")
|
for _, element := range subnetsorted {
|
||||||
|
_, err := file.WriteString(element.IP.String() + ":" + element.FQDN + "\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("[ERROR]", err)
|
fmt.Println("[ERROR]", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -189,7 +191,8 @@ func GetSubnet(net netip.Prefix) (Subnet, error) {
|
||||||
|
|
||||||
default:
|
default:
|
||||||
s := strings.Split(scanner.Text(), ":")
|
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)
|
subnet.Addresses = append(subnet.Addresses, a)
|
||||||
}
|
}
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
|
@ -201,3 +204,17 @@ func GetSubnet(net netip.Prefix) (Subnet, error) {
|
||||||
|
|
||||||
return subnet, nil
|
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>
|
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||||
|
|
||||||
*/
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// showCmd represents the show command
|
// showCmd represents the show command
|
||||||
var subnetshowCmd = &cobra.Command{
|
var subnetshowCmd = &cobra.Command{
|
||||||
Use: "show",
|
Use: "show subnet",
|
||||||
Short: "A brief description of your command",
|
Short: "Displays a subnet.",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `Displays a subnets details like name and vlan tag,
|
||||||
and usage of using your command. For example:
|
aswell as a list of containing IP addresses`,
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
Cobra is a CLI library for Go that empowers applications.
|
Aliases: []string{"s"},
|
||||||
This application is a tool to generate the needed files
|
Example: "ipam subnet show 192.168.0.0/24\nipam subnet show 2001:db8::/64",
|
||||||
to quickly create a Cobra application.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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