package cmd import ( "fmt" "os" "strings" "codeberg.org/vlbeaudoin/serpents" "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 gestion de distribution d'agendas", } // 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) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottin-agenda.yaml)") // web.api.host serpents.String(rootCmd.PersistentFlags(), "web.api.host", "web-api-host", "api", "Remote API server host") // web.api.key serpents.String(rootCmd.PersistentFlags(), "web.api.key", "web-api-key", "bottin-agenda", "Remote API server key") // web.api.protocol serpents.String(rootCmd.PersistentFlags(), "web.api.protocol", "web-api-protocol", "http", "Remote API server protocol") // web.api.port serpents.Int(rootCmd.PersistentFlags(), "web.api.port", "web-api-port", 1313, "Remote API server port") // web.password serpents.String(rootCmd.PersistentFlags(), "web.password", "web-password", "bottin-agenda", "Web client password") // web.port serpents.Int(rootCmd.PersistentFlags(), "web.port", "web-port", 2313, "Web client port") // web.user serpents.String(rootCmd.PersistentFlags(), "web.user", "web-user", "bottin-agenda", "Web client user") // api.key serpents.String(rootCmd.PersistentFlags(), "api.key", "api-key", "bottin-agenda", "API server key. Leave empty for no key auth.") // api.port serpents.Int(rootCmd.PersistentFlags(), "api.port", "api-port", 1313, "API server port") // bottin.api.host serpents.String(rootCmd.PersistentFlags(), "bottin.api.host", "bottin-api-host", "api", "Remote bottin API server host") // bottin.api.key serpents.String(rootCmd.PersistentFlags(), "bottin.api.key", "bottin-api-key", "bottin", "Remote bottin API server key") // bottin.api.protocol serpents.String(rootCmd.PersistentFlags(), "bottin.api.protocol", "bottin-api-protocol", "http", "Remote bottin API server protocol") // bottin.api.port serpents.Int(rootCmd.PersistentFlags(), "bottin.api.port", "bottin-api-port", 1312, "Remote bottin API server port") // db.database serpents.String(rootCmd.PersistentFlags(), "db.database", "db-database", "bottin-agenda", "Postgres database") // db.host serpents.String(rootCmd.PersistentFlags(), "db.host", "db-host", "db", "Postgres host") // db.password serpents.String(rootCmd.PersistentFlags(), "db.password", "db-password", "bottin-agenda", "Postgres password") // db.port serpents.Int(rootCmd.PersistentFlags(), "db.port", "db-port", 5432, "Postgres port") // db.user serpents.String(rootCmd.PersistentFlags(), "db.user", "db-user", "bottin-agenda", "Postgres user") } // 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.SetEnvPrefix("BOTTINAGENDA") viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) } }