root.go 2.0 KB

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