cert400/cmd/root.go

67 lines
1.5 KiB
Go
Raw Normal View History

2024-05-22 09:29:02 +02:00
/*
2024-05-23 10:43:16 +02:00
Copyright © 2024 raul <raul@bulgariu.xyz>
2024-05-22 09:29:02 +02:00
*/
2024-05-22 10:41:45 +02:00
2024-05-22 09:29:02 +02:00
package cmd
import (
2024-05-22 15:37:48 +02:00
"log"
2024-05-22 09:29:02 +02:00
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var rootCmd = &cobra.Command{
Use: "cert400",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. 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.`,
// 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)
2024-05-22 15:03:26 +02:00
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Config file (default is $HOME/.config/cert400/cert400.toml)")
2024-05-22 09:29:02 +02:00
}
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
2024-05-22 15:37:48 +02:00
viper.AddConfigPath(home + "/.config/cert400/")
viper.SetConfigFile(home + "/.config/cert400/cert400.toml")
2024-05-22 09:29:02 +02:00
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
2024-05-22 15:37:48 +02:00
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)
}
2024-05-22 09:29:02 +02:00
}
}