main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2017 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package main
  5. import (
  6. "context"
  7. "flag"
  8. "fmt"
  9. "os"
  10. "notabug.org/themusicgod1/goarista/cmd/gnmi/gnmi.go"
  11. "github.com/golang/glog"
  12. // pb "github.com/openconfig/gnmi/proto/gnmi"
  13. // ????
  14. )
  15. // TODO: Make this more clear
  16. var help = `Usage of gnmi:
  17. gnmi -addr ADDRESS:PORT [options...]
  18. capabilities
  19. get PATH+
  20. subscribe PATH+
  21. ((update|replace PATH JSON)|(delete PATH))+
  22. `
  23. func usageAndExit(s string) {
  24. flag.Usage()
  25. if s != "" {
  26. fmt.Fprintln(os.Stderr, s)
  27. }
  28. os.Exit(1)
  29. }
  30. func main() {
  31. cfg := &gnmi.Config{}
  32. flag.StringVar(&cfg.Addr, "addr", "", "Address of gNMI gRPC server")
  33. flag.StringVar(&cfg.CAFile, "cafile", "", "Path to server TLS certificate file")
  34. flag.StringVar(&cfg.CertFile, "certfile", "", "Path to client TLS certificate file")
  35. flag.StringVar(&cfg.KeyFile, "keyfile", "", "Path to client TLS private key file")
  36. flag.StringVar(&cfg.Password, "password", "", "Password to authenticate with")
  37. flag.StringVar(&cfg.Username, "username", "", "Username to authenticate with")
  38. flag.BoolVar(&cfg.TLS, "tls", false, "Enable TLS")
  39. flag.Usage = func() {
  40. fmt.Fprintln(os.Stderr, help)
  41. flag.PrintDefaults()
  42. }
  43. flag.Parse()
  44. if cfg.Addr == "" {
  45. usageAndExit("error: address not specified")
  46. }
  47. args := flag.Args()
  48. ctx := gnmi.NewContext(context.Background(), cfg)
  49. client, err := gnmi.Dial(cfg)
  50. if err != nil {
  51. glog.Fatal(err)
  52. }
  53. var setOps []*gnmi.Operation
  54. for i := 0; i < len(args); i++ {
  55. switch args[i] {
  56. case "capabilities":
  57. if len(setOps) != 0 {
  58. usageAndExit("error: 'capabilities' not allowed after 'merge|replace|delete'")
  59. }
  60. err := gnmi.Capabilities(ctx, client)
  61. if err != nil {
  62. glog.Fatal(err)
  63. }
  64. return
  65. case "get":
  66. if len(setOps) != 0 {
  67. usageAndExit("error: 'get' not allowed after 'merge|replace|delete'")
  68. }
  69. err := gnmi.Get(ctx, client, gnmi.SplitPaths(args[i+1:]))
  70. if err != nil {
  71. glog.Fatal(err)
  72. }
  73. return
  74. case "subscribe":
  75. if len(setOps) != 0 {
  76. usageAndExit("error: 'subscribe' not allowed after 'merge|replace|delete'")
  77. }
  78. respChan := make(chan *pb.SubscribeResponse)
  79. errChan := make(chan error)
  80. defer close(respChan)
  81. defer close(errChan)
  82. go gnmi.Subscribe(ctx, client, gnmi.SplitPaths(args[i+1:]), respChan, errChan)
  83. for {
  84. select {
  85. case resp := <-respChan:
  86. if err := gnmi.LogSubscribeResponse(resp); err != nil {
  87. glog.Fatal(err)
  88. }
  89. case err := <-errChan:
  90. glog.Fatal(err)
  91. }
  92. }
  93. case "update", "replace", "delete":
  94. if len(args) == i+1 {
  95. usageAndExit("error: missing path")
  96. }
  97. op := &gnmi.Operation{
  98. Type: args[i],
  99. }
  100. i++
  101. op.Path = gnmi.SplitPath(args[i])
  102. if op.Type != "delete" {
  103. if len(args) == i+1 {
  104. usageAndExit("error: missing JSON")
  105. }
  106. i++
  107. op.Val = args[i]
  108. }
  109. setOps = append(setOps, op)
  110. default:
  111. usageAndExit(fmt.Sprintf("error: unknown operation %q", args[i]))
  112. }
  113. }
  114. if len(setOps) == 0 {
  115. usageAndExit("")
  116. }
  117. err = gnmi.Set(ctx, client, setOps)
  118. if err != nil {
  119. glog.Fatal(err)
  120. }
  121. }