root.go 2.0 KB

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