(usage cmdUsage, vp *viper.Viper, ofilter *flowFilter)
| 289 | } |
| 290 | |
| 291 | func newFlowsCmdHelper(usage cmdUsage, vp *viper.Viper, ofilter *flowFilter) *cobra.Command { |
| 292 | |
| 293 | filterFlags := pflag.NewFlagSet("filters", pflag.ContinueOnError) |
| 294 | rawFilterFlags := pflag.NewFlagSet("raw-filters", pflag.ContinueOnError) |
| 295 | flowsFormattingFlags := pflag.NewFlagSet("Flow Format", pflag.ContinueOnError) |
| 296 | flagSets := []*pflag.FlagSet{selectorFlags, filterFlags, rawFilterFlags, formattingFlags, flowsFormattingFlags, config.ServerFlags, otherFlags} |
| 297 | |
| 298 | flowsCmd := &cobra.Command{ |
| 299 | Example: usage.example, |
| 300 | Use: usage.use, |
| 301 | Short: usage.short, |
| 302 | Long: usage.long, |
| 303 | PreRunE: func(cmd *cobra.Command, _ []string) error { |
| 304 | // bind these flags to viper so that they can be specified as environment variables. |
| 305 | // We bind these flags during PreRun so that only the running command binds them to the configuration. |
| 306 | return vp.BindPFlags(rawFilterFlags) |
| 307 | }, |
| 308 | RunE: func(cmd *cobra.Command, args []string) error { |
| 309 | debug := vp.GetBool(config.KeyDebug) |
| 310 | if err := handleFlowArgs(cmd.OutOrStdout(), ofilter, debug); err != nil { |
| 311 | return err |
| 312 | } |
| 313 | req, err := getFlowsRequest(ofilter, vp.GetStringSlice(allowlistFlag), vp.GetStringSlice(denylistFlag)) |
| 314 | if err != nil { |
| 315 | return err |
| 316 | } |
| 317 | if otherOpts.printRawFilters { |
| 318 | filterYAML, err := getFlowFiltersYAML(req) |
| 319 | if err != nil { |
| 320 | return err |
| 321 | } |
| 322 | fmt.Fprint(cmd.OutOrStdout(), filterYAML) |
| 323 | return nil |
| 324 | } |
| 325 | |
| 326 | ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) |
| 327 | defer cancel() |
| 328 | |
| 329 | client, cleanup, err := GetHubbleClientFunc(ctx, vp) |
| 330 | if err != nil { |
| 331 | return err |
| 332 | } |
| 333 | defer cleanup() |
| 334 | |
| 335 | logger.Logger.Debug("Sending GetFlows request", logfields.Request, req) |
| 336 | if err := getFlows(ctx, client, req); err != nil { |
| 337 | msg := err.Error() |
| 338 | // extract custom error message from failed grpc call |
| 339 | if s, ok := status.FromError(err); ok && s.Code() == codes.Unknown { |
| 340 | msg = s.Message() |
| 341 | } |
| 342 | return errors.New(msg) |
| 343 | } |
| 344 | return nil |
| 345 | }, |
| 346 | } |
| 347 | |
| 348 | // filter flags |
no test coverage detected
searching dependent graphs…