bottin-agenda/cmd/root.go

169 lines
5.5 KiB
Go
Raw Normal View History

package cmd
import (
2023-05-29 17:58:23 -04:00
"fmt"
"log"
"os"
2024-02-19 12:11:41 -05:00
"strings"
"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",
}
// 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
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)
}
// web.api.key
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)
}
// web.api.protocol
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)
}
// web.api.port
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)
}
// web.password
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)
}
// web.port
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)
}
// web.user
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)
}
// api.key
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)
}
// api.port
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)
}
// bottin.api.host
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)
}
// bottin.api.key
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)
}
// bottin.api.protocol
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)
}
// bottin.api.port
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)
}
// db.database
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)
}
// db.host
rootCmd.PersistentFlags().String("db-host", "db", "Postgres host")
if err := viper.BindPFlag("db.host", rootCmd.PersistentFlags().Lookup("db-host")); err != nil {
log.Fatal(err)
}
// db.password
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)
}
// db.port
rootCmd.PersistentFlags().Int("db-port", 5432, "Postgres port")
if err := viper.BindPFlag("db.port", rootCmd.PersistentFlags().Lookup("db-port")); err != nil {
log.Fatal(err)
}
// db.user
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)
}
}
// 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(".", "_"))
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())
}
}