123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- // Copyright (c) 2017 Arista Networks, Inc.
- // Use of this source code is governed by the Apache License 2.0
- // that can be found in the COPYING file.
- package main
- import (
- "context"
- "flag"
- "fmt"
- "os"
- "notabug.org/themusicgod1/goarista/cmd/gnmi/gnmi.go"
- "github.com/golang/glog"
- // pb "github.com/openconfig/gnmi/proto/gnmi"
- // ????
- )
- // TODO: Make this more clear
- var help = `Usage of gnmi:
- gnmi -addr ADDRESS:PORT [options...]
- capabilities
- get PATH+
- subscribe PATH+
- ((update|replace PATH JSON)|(delete PATH))+
- `
- func usageAndExit(s string) {
- flag.Usage()
- if s != "" {
- fmt.Fprintln(os.Stderr, s)
- }
- os.Exit(1)
- }
- func main() {
- cfg := &gnmi.Config{}
- flag.StringVar(&cfg.Addr, "addr", "", "Address of gNMI gRPC server")
- flag.StringVar(&cfg.CAFile, "cafile", "", "Path to server TLS certificate file")
- flag.StringVar(&cfg.CertFile, "certfile", "", "Path to client TLS certificate file")
- flag.StringVar(&cfg.KeyFile, "keyfile", "", "Path to client TLS private key file")
- flag.StringVar(&cfg.Password, "password", "", "Password to authenticate with")
- flag.StringVar(&cfg.Username, "username", "", "Username to authenticate with")
- flag.BoolVar(&cfg.TLS, "tls", false, "Enable TLS")
- flag.Usage = func() {
- fmt.Fprintln(os.Stderr, help)
- flag.PrintDefaults()
- }
- flag.Parse()
- if cfg.Addr == "" {
- usageAndExit("error: address not specified")
- }
- args := flag.Args()
- ctx := gnmi.NewContext(context.Background(), cfg)
- client, err := gnmi.Dial(cfg)
- if err != nil {
- glog.Fatal(err)
- }
- var setOps []*gnmi.Operation
- for i := 0; i < len(args); i++ {
- switch args[i] {
- case "capabilities":
- if len(setOps) != 0 {
- usageAndExit("error: 'capabilities' not allowed after 'merge|replace|delete'")
- }
- err := gnmi.Capabilities(ctx, client)
- if err != nil {
- glog.Fatal(err)
- }
- return
- case "get":
- if len(setOps) != 0 {
- usageAndExit("error: 'get' not allowed after 'merge|replace|delete'")
- }
- err := gnmi.Get(ctx, client, gnmi.SplitPaths(args[i+1:]))
- if err != nil {
- glog.Fatal(err)
- }
- return
- case "subscribe":
- if len(setOps) != 0 {
- usageAndExit("error: 'subscribe' not allowed after 'merge|replace|delete'")
- }
- respChan := make(chan *pb.SubscribeResponse)
- errChan := make(chan error)
- defer close(respChan)
- defer close(errChan)
- go gnmi.Subscribe(ctx, client, gnmi.SplitPaths(args[i+1:]), respChan, errChan)
- for {
- select {
- case resp := <-respChan:
- if err := gnmi.LogSubscribeResponse(resp); err != nil {
- glog.Fatal(err)
- }
- case err := <-errChan:
- glog.Fatal(err)
- }
- }
- case "update", "replace", "delete":
- if len(args) == i+1 {
- usageAndExit("error: missing path")
- }
- op := &gnmi.Operation{
- Type: args[i],
- }
- i++
- op.Path = gnmi.SplitPath(args[i])
- if op.Type != "delete" {
- if len(args) == i+1 {
- usageAndExit("error: missing JSON")
- }
- i++
- op.Val = args[i]
- }
- setOps = append(setOps, op)
- default:
- usageAndExit(fmt.Sprintf("error: unknown operation %q", args[i]))
- }
- }
- if len(setOps) == 0 {
- usageAndExit("")
- }
- err = gnmi.Set(ctx, client, setOps)
- if err != nil {
- glog.Fatal(err)
- }
- }
|