diff --git a/.gitignore b/.gitignore index 475d9c2..e88debd 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ go.work # build output output/ + +# vs code stuff +.vscode/ diff --git a/classes/classes.go b/classes/classes.go deleted file mode 100644 index 92452cb..0000000 --- a/classes/classes.go +++ /dev/null @@ -1,10 +0,0 @@ -/* -Copyright © 2023 Laura Kalb -*/ - -package classes - -type Subnet struct { - subnet string - name string -} diff --git a/cmd/root.go b/cmd/root.go index b1517d9..f1b0963 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -55,27 +55,36 @@ func initConfig() { fmt.Println(err) os.Exit(1) } + + var ipamdir string = homedir + "/.ipam/" // Search config in home directory with name ".cobra" (without extension). - viper.AddConfigPath(homedir + "/.ipam/") - viper.SetConfigName("ipam.yml") + viper.AddConfigPath(ipamdir) + viper.SetConfigName("ipam") + viper.SetConfigType("yaml") viper.SetDefault("EnablePowerDNS", false) - viper.SetDefault("DataPath", homedir+"/.ipam/data/") - - println(homedir + "/.ipam/") + viper.SetDefault("DataPath", ipamdir+"/data/") if err := viper.ReadInConfig(); err != nil { - //if errfolder := os.MkdirAll(homedir+"/.ipam/", 0664); err != nil { - // println("[ERROR] Can't create config dir!", errfolder) - // os.Exit(1) - //} + _, patherr := os.Stat(ipamdir) + if patherr != nil { + mkerr := os.MkdirAll(ipamdir, 0755) + if mkerr != nil { + println("[ERROR] Can't create ipam config directory!", mkerr) + } + } - //if errconfig := viper.WriteConfig(); err != nil { - // println("[ERROR] Can't create config file!", errconfig) - // os.Exit(1) - //} - - println("[ERROR] Can't read config file!", err) + // I have no idea what's happening here... + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + writeerr := viper.SafeWriteConfig() + if writeerr != nil { + println("[ERROR] Can't write config file!", writeerr) + } + } else { + println("[ERROR] Can't read config file!", err) + } } + fmt.Println(viper.AllKeys()) + } diff --git a/cmd/storage.go b/cmd/storage.go new file mode 100644 index 0000000..cb2fa2c --- /dev/null +++ b/cmd/storage.go @@ -0,0 +1,42 @@ +/* +Copyright © 2023 Laura Kalb +*/ + +package cmd + +import ( + "net" +) + +type Subnet struct { + Subnet net.IPNet + Name string + Vlan string + Addresses []Address +} + +type Address struct { + IP string + FQDN string +} + +// GetSubnet tries to load the most fitting IP subnet file on disk. +// It takes an IP object and tries to get the best subnet (meaning +// the subnet with the smallest subnet size). +// +// Returns the best subnet as Subnet object and true if a suitable +// subnet was found, otherwise an empty Subnet object and false. +func GetSubnet(ip net.IP) (Subnet, bool) { + + return Subnet{}, false +} + +// WriteSubnet takes a given Subnet object and tries to write it to +// file. +// +// Returns nil on success or the error that happened. +func WriteSubnet(subnet Subnet) error { + //if subnet.Subnet + + return nil +} diff --git a/storage/storage.go b/storage/storage.go deleted file mode 100644 index 8c1acc3..0000000 --- a/storage/storage.go +++ /dev/null @@ -1,5 +0,0 @@ -/* -Copyright © 2023 Laura Kalb -*/ - -package storage