root.go 1.9 KB

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