cert400/cmd/root.go

62 lines
1.3 KiB
Go

/*
Copyright © 2024 raul <raul@bulgariu.xyz>
*/
package cmd
import (
"log"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var rootCmd = &cobra.Command{
Use: "cert400",
Short: "CA Web-Server capable of generating signed certificates",
Long: `CA Web-Server capable of generating signed certificates`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Config file (default is $HOME/.config/cert400/cert400.toml)")
}
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
viper.AddConfigPath(home + "/.config/cert400/")
viper.SetConfigFile(home + "/.config/cert400/cert400.toml")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
if os.IsNotExist(err) {
log.Fatalln("Missing config file at $HOME/.config/cert400/cert400.toml")
} else {
log.Fatal(err)
}
}
}