add promtui to select target for PollRequest
@@ -26,6 +26,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/google/gnxi/utils/xpath"
+ "github.com/manifoldco/promptui"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -80,8 +81,16 @@ var subscribeCmd = &cobra.Command{
polledSubsChan := make(map[string]chan struct{})
waitChan := make(chan struct{})
if subscReq.GetSubscribe().Mode == gnmi.SubscriptionList_POLL {
- for _, addr := range addresses {
- polledSubsChan[addr] = make(chan struct{})
+ for i := range addresses {
+ _, _, err := net.SplitHostPort(addresses[i])
+ if err != nil {
+ if strings.Contains(err.Error(), "missing port in address") {
+ addresses[i] = net.JoinHostPort(addresses[i], defaultGrpcPort)
+ } else {
+ continue
+ }
+ }
+ polledSubsChan[addresses[i]] = make(chan struct{})
}
}
wg := new(sync.WaitGroup)
@@ -146,7 +155,8 @@ var subscribeCmd = &cobra.Command{
}
case gnmi.SubscriptionList_POLL:
for {
- <-polledSubsChan[address]
+ select {
+ case <-polledSubsChan[address]:
err = subscribeClient.Send(&gnmi.SubscribeRequest{
Request: &gnmi.SubscribeRequest_Poll{
Poll: &gnmi.Poll{},
@@ -172,23 +182,27 @@ var subscribeCmd = &cobra.Command{
waitChan <- struct{}{}
}
}
+ }
}(addr)
}
if subscReq.GetSubscribe().Mode == gnmi.SubscriptionList_POLL {
- var address string
for {
- fmt.Print("target to poll(ip:port): ")
- _, err := fmt.Scan(&address)
+ s := promptui.Select{
+ Label: "select target to poll",
+ Items: addresses,
+ HideSelected: true,
+ }
+ _, addr, err := s.Run()
if err != nil {
- fmt.Printf("%v\n", err)
+ fmt.Printf("failed selecting target to poll: %v\n", err)
continue
}
- c, ok := polledSubsChan[address]
- if !ok {
- fmt.Printf("unknown target: %s\n", address)
+ if _, ok := polledSubsChan[addr]; !ok {
+ fmt.Printf("unknown target: %s\n", addr)
continue
}
- c <- struct{}{}
+ logger.Printf("polling address '%s'", addr)
+ polledSubsChan[addr] <- struct{}{}
<-waitChan
}
}
