root.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/certusone/wormhole/node/cmd/ccq"
  6. "github.com/certusone/wormhole/node/cmd/debug"
  7. "github.com/certusone/wormhole/node/cmd/spy"
  8. txverifier "github.com/certusone/wormhole/node/cmd/txverifier"
  9. "github.com/certusone/wormhole/node/pkg/version"
  10. "github.com/spf13/cobra"
  11. "github.com/spf13/viper"
  12. "github.com/certusone/wormhole/node/cmd/guardiand"
  13. )
  14. var cfgFile string
  15. // rootCmd represents the base command when called without any subcommands
  16. var rootCmd = &cobra.Command{
  17. Use: "guardiand",
  18. Short: "Wormhole guardian node",
  19. }
  20. // Top-level version subcommand
  21. var versionCmd = &cobra.Command{
  22. Use: "version",
  23. Short: "Display binary version information",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. fmt.Println(version.Version())
  26. },
  27. }
  28. // 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.
  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(txverifier.TransferVerifierCmd)
  41. rootCmd.AddCommand(ccq.QueryServerCmd)
  42. rootCmd.AddCommand(guardiand.KeygenCmd)
  43. rootCmd.AddCommand(guardiand.AdminCmd)
  44. rootCmd.AddCommand(guardiand.TemplateCmd)
  45. rootCmd.AddCommand(versionCmd)
  46. rootCmd.AddCommand(debug.DebugCmd)
  47. }
  48. // initConfig reads in config file and ENV variables if set.
  49. // NOTE: This set up is used for guardiand commands other than node.
  50. // for that, see `initConfig` in `node.go`.
  51. func initConfig() {
  52. if cfgFile != "" {
  53. // Use config file from the flag.
  54. viper.SetConfigFile(cfgFile)
  55. } else {
  56. // Find home directory.
  57. home, err := os.UserHomeDir()
  58. if err != nil {
  59. fmt.Println(err)
  60. os.Exit(1)
  61. }
  62. // Search config in home directory with name ".guardiand" (without extension).
  63. viper.AddConfigPath(home)
  64. viper.SetConfigName(".guardiand.yaml")
  65. }
  66. viper.AutomaticEnv() // read in environment variables that match
  67. // If a config file is found, read it in.
  68. if err := viper.ReadInConfig(); err == nil {
  69. fmt.Println("Using config file:", viper.ConfigFileUsed())
  70. }
  71. }