root.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/spf13/cobra"
  6. homedir "github.com/mitchellh/go-homedir"
  7. "github.com/spf13/viper"
  8. "github.com/certusone/wormhole/bridge/cmd/guardiand"
  9. )
  10. var cfgFile string
  11. // rootCmd represents the base command when called without any subcommands
  12. var rootCmd = &cobra.Command{
  13. Use: "guardiand",
  14. Short: "Wormhole bridge server",
  15. }
  16. // Execute adds all child commands to the root command and sets flags appropriately.
  17. // This is called by main.main(). It only needs to happen once to the rootCmd.
  18. func Execute() {
  19. if err := rootCmd.Execute(); err != nil {
  20. fmt.Println(err)
  21. os.Exit(1)
  22. }
  23. }
  24. func init() {
  25. cobra.OnInitialize(initConfig)
  26. rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.guardiand.yaml)")
  27. rootCmd.AddCommand(guardiand.BridgeCmd)
  28. rootCmd.AddCommand(guardiand.KeygenCmd)
  29. rootCmd.AddCommand(guardiand.AdminCmd)
  30. rootCmd.AddCommand(guardiand.TemplateCmd)
  31. }
  32. // initConfig reads in config file and ENV variables if set.
  33. func initConfig() {
  34. if cfgFile != "" {
  35. // Use config file from the flag.
  36. viper.SetConfigFile(cfgFile)
  37. } else {
  38. // Find home directory.
  39. home, err := homedir.Dir()
  40. if err != nil {
  41. fmt.Println(err)
  42. os.Exit(1)
  43. }
  44. // Search config in home directory with name ".guardiand" (without extension).
  45. viper.AddConfigPath(home)
  46. viper.SetConfigName(".guardiand.yaml")
  47. }
  48. viper.AutomaticEnv() // read in environment variables that match
  49. // If a config file is found, read it in.
  50. if err := viper.ReadInConfig(); err == nil {
  51. fmt.Println("Using config file:", viper.ConfigFileUsed())
  52. }
  53. }