getpeers.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. nodepb "github.com/gravitl/netmaker/grpc"
  17. "context"
  18. "io"
  19. "github.com/spf13/cobra"
  20. "github.com/gravitl/netmaker/wcagent/functions"
  21. "google.golang.org/grpc/metadata"
  22. "google.golang.org/grpc"
  23. )
  24. // getpeersCmd represents the getpeers command
  25. var getpeersCmd = &cobra.Command{
  26. Use: "getpeers",
  27. Short: "A brief description of your command",
  28. Long: `A longer description that spans multiple lines and likely contains examples
  29. and usage of using your command. For example:
  30. Cobra is a CLI library for Go that empowers applications.
  31. This application is a tool to generate the needed files
  32. to quickly create a Cobra application.`,
  33. SilenceUsage: true,
  34. RunE: func(cmd *cobra.Command, args []string) error {
  35. fmt.Println("read called")
  36. group, err := cmd.Flags().GetString("group")
  37. if err != nil {
  38. return err
  39. }
  40. req := &nodepb.GetPeersReq{
  41. Group: group,
  42. }
  43. ctx := context.Background()
  44. ctx, err = functions.SetJWT(client)
  45. if err != nil {
  46. return err
  47. }
  48. var header metadata.MD
  49. stream, err := client.GetPeers(ctx, req, grpc.Header(&header))
  50. if err != nil {
  51. return err
  52. }
  53. //fmt.Println(res)
  54. for {
  55. // stream.Recv returns a pointer to a ListBlogRes at the current iteration
  56. res, err := stream.Recv()
  57. // If end of stream, break the loop
  58. if err == io.EOF {
  59. break
  60. }
  61. // if err, return an error
  62. if err != nil {
  63. return err
  64. }
  65. // If everything went well use the generated getter to print the blog message
  66. fmt.Println(res.Peers)
  67. }
  68. return nil
  69. },
  70. }
  71. func init() {
  72. getpeersCmd.Flags().StringP("group", "g", "", "The group of the node")
  73. getpeersCmd.MarkFlagRequired("group")
  74. rootCmd.AddCommand(getpeersCmd)
  75. // Here you will define your flags and configuration settings.
  76. // Cobra supports Persistent Flags which will work for this command
  77. // and all subcommands, e.g.:
  78. // getpeersCmd.PersistentFlags().String("foo", "", "A help for foo")
  79. // Cobra supports local flags which will only run when this command
  80. // is called directly, e.g.:
  81. // getpeersCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
  82. }