2023-02-17 15:37:36 -05:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-05-29 17:58:23 -04:00
|
|
|
"fmt"
|
2024-09-16 14:31:30 -04:00
|
|
|
"log"
|
2023-02-17 15:37:36 -05:00
|
|
|
"os"
|
2024-02-19 12:11:41 -05:00
|
|
|
"strings"
|
2023-02-17 15:37:36 -05:00
|
|
|
|
|
|
|
"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",
|
2023-05-29 17:58:23 -04:00
|
|
|
Short: "Application de gestion de distribution d'agendas",
|
2023-02-17 15:37:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)")
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// web.api.host
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("web-api-host", "api", "Remote API server host")
|
|
|
|
if err := viper.BindPFlag("web.api.host", rootCmd.PersistentFlags().Lookup("web-api-host")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// web.api.key
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("web-api-key", "bottin-agenda", "Remote API server key")
|
|
|
|
if err := viper.BindPFlag("web.api.key", rootCmd.PersistentFlags().Lookup("web-api-key")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// web.api.protocol
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("web-api-protocol", "http", "Remote API server protocol")
|
|
|
|
if err := viper.BindPFlag("web.api.protocol", rootCmd.PersistentFlags().Lookup("web-api-protocol")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// web.api.port
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().Int("web-api-port", 1313, "Remote API server port")
|
|
|
|
if err := viper.BindPFlag("web.api.port", rootCmd.PersistentFlags().Lookup("web-api-port")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// web.password
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("web-password", "bottin-agenda", "Web client password")
|
|
|
|
if err := viper.BindPFlag("web.password", rootCmd.PersistentFlags().Lookup("web-password")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// web.port
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().Int("web-port", 2313, "Web client port")
|
|
|
|
if err := viper.BindPFlag("web.port", rootCmd.PersistentFlags().Lookup("web-port")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// web.user
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("web-user", "bottin-agenda", "Web client user")
|
|
|
|
if err := viper.BindPFlag("web.user", rootCmd.PersistentFlags().Lookup("web-user")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// api.key
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("api-key", "bottin-agenda", "API server key. Leave empty for no key auth.")
|
|
|
|
if err := viper.BindPFlag("api.key", rootCmd.PersistentFlags().Lookup("api-key")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// api.port
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().Int("api-port", 1313, "API server port")
|
|
|
|
if err := viper.BindPFlag("api.port", rootCmd.PersistentFlags().Lookup("api-port")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// bottin.api.host
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("bottin-api-host", "api", "Remote bottin API server host")
|
|
|
|
if err := viper.BindPFlag("bottin.api.host", rootCmd.PersistentFlags().Lookup("bottin-api-host")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// bottin.api.key
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("bottin-api-key", "bottin", "Remote bottin API server key")
|
|
|
|
if err := viper.BindPFlag("bottin.api.key", rootCmd.PersistentFlags().Lookup("bottin-api-key")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// bottin.api.protocol
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("bottin-api-protocol", "http", "Remote bottin API server protocol")
|
|
|
|
if err := viper.BindPFlag("bottin.api.protocol", rootCmd.PersistentFlags().Lookup("bottin-api-protocol")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// bottin.api.port
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().Int("bottin-api-port", 1312, "Remote bottin API server port")
|
|
|
|
if err := viper.BindPFlag("bottin.api.port", rootCmd.PersistentFlags().Lookup("bottin-api-port")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// db.database
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("db-database", "bottin-agenda", "Postgres database")
|
|
|
|
if err := viper.BindPFlag("db.database", rootCmd.PersistentFlags().Lookup("db-database")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// db.host
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("db-host", "db", "Postgres host")
|
|
|
|
if err := viper.BindPFlag("db.host", rootCmd.PersistentFlags().Lookup("db-host")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// db.password
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("db-password", "bottin-agenda", "Postgres password")
|
|
|
|
if err := viper.BindPFlag("db.password", rootCmd.PersistentFlags().Lookup("db-password")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// db.port
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().Int("db-port", 5432, "Postgres port")
|
|
|
|
if err := viper.BindPFlag("db.port", rootCmd.PersistentFlags().Lookup("db-port")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-09 01:09:02 -04:00
|
|
|
|
|
|
|
// db.user
|
2024-09-16 14:31:30 -04:00
|
|
|
rootCmd.PersistentFlags().String("db-user", "bottin-agenda", "Postgres user")
|
|
|
|
if err := viper.BindPFlag("db.user", rootCmd.PersistentFlags().Lookup("db-user")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-02-17 15:37:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2024-02-19 12:11:41 -05:00
|
|
|
viper.SetEnvPrefix("BOTTINAGENDA")
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2023-02-17 15:37:36 -05:00
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
2023-05-29 17:58:23 -04:00
|
|
|
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
2023-02-17 15:37:36 -05:00
|
|
|
}
|
|
|
|
}
|