From c38cccc672b0d31cf6d482438e8b83030c1cbe20 Mon Sep 17 00:00:00 2001 From: Aneesh Rao Date: Fri, 12 Jul 2024 14:34:00 +0530 Subject: [PATCH] Added print-request and dry-run support for subscribe --- pkg/app/gnmi_client_subscribe.go | 21 +++++++++++++++++++++ pkg/app/subscribe.go | 1 + pkg/config/config.go | 1 + 3 files changed, 23 insertions(+) diff --git a/pkg/app/gnmi_client_subscribe.go b/pkg/app/gnmi_client_subscribe.go index e15b787f..381320bf 100644 --- a/pkg/app/gnmi_client_subscribe.go +++ b/pkg/app/gnmi_client_subscribe.go @@ -211,10 +211,22 @@ CRCLIENT: a.Logger.Printf("target %q gNMI client created", t.Config.Name) for _, sreq := range subRequests { + if a.Config.GlobalFlags.PrintRequest || a.Config.LocalFlags.SubscribeDryRun { + err := a.PrintMsg(tc.Name, "Subscribe Request:", sreq.req) + if err != nil { + a.logError(fmt.Errorf("target %q Subscribe Request printing failed: %v", tc.Name, err)) + } + } + if a.Config.LocalFlags.SubscribeDryRun { + continue + } a.Logger.Printf("sending gNMI SubscribeRequest: subscribe='%+v', mode='%+v', encoding='%+v', to %s", sreq.req, sreq.req.GetSubscribe().GetMode(), sreq.req.GetSubscribe().GetEncoding(), t.Config.Name) go t.Subscribe(gnmiCtx, sreq.req, sreq.name) } + if a.Config.LocalFlags.SubscribeDryRun { + os.Exit(0) + } return nil } @@ -272,6 +284,15 @@ CRCLIENT: a.Logger.Printf("target %q gNMI client created", t.Config.Name) OUTER: for _, sreq := range subRequests { + if a.Config.GlobalFlags.PrintRequest || a.Config.LocalFlags.SubscribeDryRun { + err := a.PrintMsg(tc.Name, "Subscribe Request:", sreq.req) + if err != nil { + a.logError(fmt.Errorf("target %q Subscribe Request printing failed: %v", tc.Name, err)) + } + } + if a.Config.LocalFlags.SubscribeDryRun { + continue + } a.Logger.Printf("sending gNMI SubscribeRequest: subscribe='%+v', mode='%+v', encoding='%+v', to %s", sreq.req, sreq.req.GetSubscribe().GetMode(), sreq.req.GetSubscribe().GetEncoding(), t.Config.Name) rspCh, errCh := t.SubscribeOnceChan(gnmiCtx, sreq.req) diff --git a/pkg/app/subscribe.go b/pkg/app/subscribe.go index 7dd5fa14..2414f628 100644 --- a/pkg/app/subscribe.go +++ b/pkg/app/subscribe.go @@ -183,6 +183,7 @@ func (a *App) InitSubscribeFlags(cmd *cobra.Command) { cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeHistoryStart, "history-start", "", "", "sets the start time in a historical range subscription, nanoseconds since Unix epoch or RFC3339 format") cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeHistoryEnd, "history-end", "", "", "sets the end time in a historical range subscription, nanoseconds since Unix epoch or RFC3339 format") cmd.Flags().Uint32VarP(&a.Config.LocalFlags.SubscribeDepth, "depth", "", 0, "depth extension value") + cmd.Flags().BoolVarP(&a.Config.LocalFlags.SubscribeDryRun, "dry-run", "", false, "dry-run mode, only print the subscribe request") // cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) { a.Config.FileConfig.BindPFlag(fmt.Sprintf("%s-%s", cmd.Name(), flag.Name), flag) diff --git a/pkg/config/config.go b/pkg/config/config.go index f2f0d359..b517d055 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -188,6 +188,7 @@ type LocalFlags struct { SubscribeHistoryStart string `mapstructure:"subscribe-history-start,omitempty" json:"subscribe-history-start,omitempty" yaml:"subscribe-history-start,omitempty"` SubscribeHistoryEnd string `mapstructure:"subscribe-history-end,omitempty" json:"subscribe-history-end,omitempty" yaml:"subscribe-history-end,omitempty"` SubscribeDepth uint32 `mapstructure:"subscribe-depth,omitempty" yaml:"subscribe-depth,omitempty" json:"subscribe-depth,omitempty"` + SubscribeDryRun bool `mapstructure:"subscribe-dry-run,omitempty" json:"subscribe-dry-run,omitempty" yaml:"subscribe-dry-run,omitempty"` // Path PathPathType string `mapstructure:"path-path-type,omitempty" json:"path-path-type,omitempty" yaml:"path-path-type,omitempty"` PathWithDescr bool `mapstructure:"path-descr,omitempty" json:"path-descr,omitempty" yaml:"path-descr,omitempty"`