add --name to subscribeCmd | filter subscriptionConfig if name is provided
@@ -202,6 +202,7 @@ func init() {
subscribeCmd.Flags().StringSliceP("model", "", []string{""}, "subscribe request used model(s)")
subscribeCmd.Flags().Bool("quiet", false, "suppress stdout printing")
subscribeCmd.Flags().StringP("target", "", "", "subscribe request target")
+ subscribeCmd.Flags().StringSliceP("name", "n", []string{}, "reference subscriptions by name, must be defined in gnmic config file")
//
viper.BindPFlag("subscribe-prefix", subscribeCmd.LocalFlags().Lookup("prefix"))
viper.BindPFlag("subscribe-path", subscribeCmd.LocalFlags().Lookup("path"))
@@ -215,6 +216,7 @@ func init() {
viper.BindPFlag("subscribe-sub-model", subscribeCmd.LocalFlags().Lookup("model"))
viper.BindPFlag("subscribe-quiet", subscribeCmd.LocalFlags().Lookup("quiet"))
viper.BindPFlag("subscribe-target", subscribeCmd.LocalFlags().Lookup("target"))
+ viper.BindPFlag("subscribe-name", subscribeCmd.LocalFlags().Lookup("name"))
}
func formatSubscribeResponse(meta map[string]interface{}, subResp *gnmi.SubscribeResponse) ([]byte, error) {
@@ -381,5 +383,21 @@ func getSubscriptions() (map[string]*collector.SubscriptionConfig, error) {
}
subscriptions[sn] = sub
}
+ subNames := viper.GetStringSlice("subscribe-name")
+ if len(subNames) == 0 {
return subscriptions, nil
}
+ filteredSubscriptions := make(map[string]*collector.SubscriptionConfig)
+ notFound := make([]string, 0)
+ for _, name := range subNames {
+ if s, ok := subscriptions[name]; ok {
+ filteredSubscriptions[name] = s
+ } else {
+ notFound = append(notFound, name)
+ }
+ }
+ if len(notFound) > 0 {
+ return nil, fmt.Errorf("named subscription(s) not found in config file: %v", notFound)
+ }
+ return filteredSubscriptions, nil
+}
