bottin-agenda/cmd/root.go

109 lines
3.1 KiB
Go
Raw Normal View History

package cmd
import (
"log"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "bottin-agenda",
Short: "Application de distribution d'agendas",
// 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() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottin-agenda.yaml)")
// bottin.host
rootCmd.PersistentFlags().String(
"bottin-host", "",
"The host of the bottin instance (config: 'bottin.host')")
viper.BindPFlag(
"bottin.host",
rootCmd.PersistentFlags().Lookup("bottin-host"))
rootCmd.MarkPersistentFlagRequired("bottin.host")
// bottin.protocol
rootCmd.PersistentFlags().String(
"bottin-protocol", "http",
"The protocol of the bottin instance (config: 'bottin.protocol')")
viper.BindPFlag(
"bottin.protocol",
rootCmd.PersistentFlags().Lookup("bottin-protocol"))
rootCmd.MarkPersistentFlagRequired("bottin.protocol")
// bottin.port
rootCmd.PersistentFlags().Int(
"bottin-port", 1312,
"The port to the bottin instance (config: 'bottin.port')")
viper.BindPFlag(
"bottin.port",
rootCmd.PersistentFlags().Lookup("bottin-port"))
rootCmd.MarkPersistentFlagRequired("bottin.port")
// bottin.username
rootCmd.PersistentFlags().String(
"bottin-username", "",
"The username to the bottin instance (config: 'bottin.username')")
viper.BindPFlag(
"bottin.username",
rootCmd.PersistentFlags().Lookup("bottin-username"))
rootCmd.MarkPersistentFlagRequired("bottin.username")
// bottin.password
rootCmd.PersistentFlags().String(
"bottin-password", "",
"The password to the bottin instance (config: 'bottin.password')")
viper.BindPFlag(
"bottin.password",
rootCmd.PersistentFlags().Lookup("bottin-password"))
rootCmd.MarkPersistentFlagRequired("bottin.password")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory with name ".bottin-agenda" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".bottin-agenda")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Printf("Using config file: %s\n", viper.ConfigFileUsed())
}
}