ipam/cmd/root.go

82 lines
2.3 KiB
Go
Raw Normal View History

2023-03-10 17:52:20 +01:00
/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
*/
package cmd
import (
2023-03-12 16:58:30 +01:00
"fmt"
2023-03-10 17:52:20 +01:00
"os"
"github.com/spf13/cobra"
2023-03-12 16:58:30 +01:00
"github.com/spf13/viper"
2023-03-10 17:52:20 +01:00
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
2023-03-12 16:58:30 +01:00
Use: "ipam",
Short: "A cli based ipam",
Long: `A cli based ipam.
2023-03-11 13:49:35 +01:00
You can manage subnets, single ip addresses within those, and the corresponding A records.
2023-03-12 16:58:30 +01:00
PowerDNS and IPV6-Support will follow`,
2023-03-10 17:52:20 +01:00
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
2023-03-12 16:58:30 +01:00
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ipam.yaml)")
2023-03-10 17:52:20 +01:00
// Cobra also supports local flags, which will only run
// when this action is called directly.
2023-03-12 16:58:30 +01:00
cobra.OnInitialize(initConfig)
2023-03-10 17:52:20 +01:00
rootCmd.Flags().BoolP("debug", "d", false, "Enable debug mode. (may print sensitive Information, so please watch out!)")
2023-03-12 16:58:30 +01:00
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
}
func initConfig() {
// Find home directory.
homedir, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(homedir + "/.ipam/")
viper.SetConfigName("ipam.yml")
viper.SetDefault("EnablePowerDNS", false)
viper.SetDefault("DataPath", homedir+"/.ipam/data/")
println(homedir + "/.ipam/")
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)
//}
//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)
}
2023-03-10 17:52:20 +01:00
}