root.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright © 2021 NAME HERE <EMAIL ADDRESS>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "os"
  17. "github.com/spf13/cobra"
  18. "context"
  19. homedir "github.com/mitchellh/go-homedir"
  20. "github.com/spf13/viper"
  21. "time"
  22. "log"
  23. nodepb "github.com/gravitl/netmaker/grpc"
  24. "google.golang.org/grpc"
  25. )
  26. var cfgFile string
  27. var client nodepb.NodeServiceClient
  28. var requestCtx context.Context
  29. var requestOpts grpc.DialOption
  30. // rootCmd represents the base command when called without any subcommands
  31. var rootCmd = &cobra.Command{
  32. Use: "grpc-test",
  33. Short: "A test cli which may or may not turn into a real one",
  34. Long: `huh. this isn't very long at all, is it?`,
  35. // Uncomment the following line if your bare application
  36. // has an action associated with it:
  37. // Run: func(cmd *cobra.Command, args []string) { },
  38. }
  39. // Execute adds all child commands to the root command and sets flags appropriately.
  40. // This is called by main.main(). It only needs to happen once to the rootCmd.
  41. func Execute() {
  42. if err := rootCmd.Execute(); err != nil {
  43. fmt.Println(err)
  44. os.Exit(1)
  45. }
  46. }
  47. func init() {
  48. cobra.OnInitialize(initConfig)
  49. // Here you will define your flags and configuration settings.
  50. // Cobra supports persistent flags, which, if defined here,
  51. // will be global for your application.
  52. rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.blogclient.yaml)")
  53. // Cobra also supports local flags, which will only run
  54. // when this action is called directly.
  55. rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
  56. fmt.Println("Starting Node Service Client")
  57. // Establish context to timeout after 10 seconds if server does not respond
  58. requestCtx, _ = context.WithTimeout(context.Background(), 10*time.Second)
  59. // Establish insecure grpc options (no TLS)
  60. requestOpts = grpc.WithInsecure()
  61. // Dial the server, returns a client connection
  62. conn, err := grpc.Dial("localhost:50051", requestOpts)
  63. if err != nil {
  64. log.Fatalf("Unable to establish client connection to localhost:50051: %v", err)
  65. }
  66. // Instantiate the BlogServiceClient with our client connection to the server
  67. client = nodepb.NewNodeServiceClient(conn)
  68. }
  69. // initConfig reads in config file and ENV variables if set.
  70. func initConfig() {
  71. if cfgFile != "" {
  72. // Use config file from the flag.
  73. viper.SetConfigFile(cfgFile)
  74. } else {
  75. // Find home directory.
  76. home, err := homedir.Dir()
  77. if err != nil {
  78. fmt.Println(err)
  79. os.Exit(1)
  80. }
  81. // Search config in home directory with name ".grpc-test" (without extension).
  82. viper.AddConfigPath(home)
  83. viper.SetConfigName(".grpc-test")
  84. }
  85. viper.AutomaticEnv() // read in environment variables that match
  86. // If a config file is found, read it in.
  87. if err := viper.ReadInConfig(); err == nil {
  88. fmt.Println("Using config file:", viper.ConfigFileUsed())
  89. }
  90. }