read.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "context"
  17. nodepb "github.com/gravitl/netmaker/grpc"
  18. "github.com/gravitl/netmaker/wcagent/functions"
  19. "github.com/spf13/cobra"
  20. "google.golang.org/grpc/metadata"
  21. "google.golang.org/grpc"
  22. )
  23. // readCmd represents the read command
  24. var readCmd = &cobra.Command{
  25. Use: "read",
  26. Short: "Find a Node by its Mac Address",
  27. Long: `Find a node by it's macaddress, stored in mongoDB.
  28. If no node is found with the corresponding MAC it will return a 'Not Found' error`,
  29. SilenceUsage: true,
  30. RunE: func(cmd *cobra.Command, args []string) error {
  31. fmt.Println("read called")
  32. macaddress, err := cmd.Flags().GetString("macaddress")
  33. group, err := cmd.Flags().GetString("group")
  34. if err != nil {
  35. return err
  36. }
  37. req := &nodepb.ReadNodeReq{
  38. Macaddress: macaddress,
  39. Group: group,
  40. }
  41. ctx := context.Background()
  42. ctx, err = functions.SetJWT(client)
  43. if err != nil {
  44. return err
  45. }
  46. var header metadata.MD
  47. res, err := client.ReadNode(ctx, req, grpc.Header(&header))
  48. if err != nil {
  49. return err
  50. }
  51. fmt.Println(res)
  52. return nil
  53. },
  54. }
  55. func init() {
  56. readCmd.Flags().StringP("macaddress", "m", "", "The macaddress of the node")
  57. readCmd.Flags().StringP("group", "g", "", "The group of the node")
  58. readCmd.MarkFlagRequired("macaddress")
  59. readCmd.MarkFlagRequired("group")
  60. rootCmd.AddCommand(readCmd)
  61. // Here you will define your flags and configuration settings.
  62. // Cobra supports Persistent Flags which will work for this command
  63. // and all subcommands, e.g.:
  64. // readCmd.PersistentFlags().String("foo", "", "A help for foo")
  65. // Cobra supports local flags which will only run when this command
  66. // is called directly, e.g.:
  67. // readCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
  68. }