From 6160d34233994e3cbff2d790921e8e6147771076 Mon Sep 17 00:00:00 2001 From: Devaansh-Kumar Date: Thu, 15 Aug 2024 15:44:20 +0530 Subject: [PATCH 1/2] created new flag --notifications-output-file to specify file to write notifications to --- cmd/print.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/cmd/print.go b/cmd/print.go index cf7d2f44..8a7075d0 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -71,6 +71,9 @@ type PrintRunner struct { // Provider specific flags ---. providerSpecificFlags map[string]*string + + // notificationsOutputFile contains the path to the output file for notifications. + notificationsOutputFile string } // PrintGatewayAPIObjects performs necessary steps to digest and print @@ -92,8 +95,16 @@ func (pr *PrintRunner) PrintGatewayAPIObjects(cmd *cobra.Command, _ []string) er return err } - for _, table := range notificationTablesMap { - fmt.Println(table) + if pr.notificationsOutputFile != "" { + err = pr.writeNotificationsToFile(notificationTablesMap) + if err != nil { + return fmt.Errorf("failed to write notifications to file: %w", err) + } + } else { + // If no file is specified, print notifications to stdout + for _, table := range notificationTablesMap { + fmt.Println(table) + } } pr.outputResult(gatewayResources) @@ -307,6 +318,9 @@ if specified with --namespace.`) cmd.Flags().StringSliceVar(&pr.providers, "providers", []string{}, fmt.Sprintf("If present, the tool will try to convert only resources related to the specified providers, supported values are %v.", i2gw.GetSupportedProviders())) + cmd.Flags().StringVar(&pr.notificationsOutputFile, "notifications-output-file", "", + "Path to the file where notifications will be written. If not specified, notifications will be printed to stdout.") + pr.providerSpecificFlags = make(map[string]*string) for provider, flags := range i2gw.GetProviderSpecificFlagDefinitions() { for _, flag := range flags { @@ -365,3 +379,20 @@ func PrintUnstructuredAsYaml(obj *unstructured.Unstructured) error { return nil } + +// writeNotificationsToFile writes the notification tables to the specified file +func (pr *PrintRunner) writeNotificationsToFile(notificationTablesMap map[string]string) error { + file, err := os.Create(pr.notificationsOutputFile) + if err != nil { + return err + } + defer file.Close() + + for _, table := range notificationTablesMap { + _, err := file.WriteString(table + "\n") + if err != nil { + return err + } + } + return nil +} From eaac08daed2c9ba85a84d33f87f5abaf52962420 Mon Sep 17 00:00:00 2001 From: Devaansh-Kumar Date: Sat, 17 Aug 2024 11:13:50 +0530 Subject: [PATCH 2/2] added documentation for --notifications-output-file flag --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6b25eb45..279a995f 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ The above command will: | output | yaml | No | The output format, either yaml or json. | | providers | all supported providers | Yes | Comma-separated list of providers. | | kubeconfig | | No | The kubeconfig file to use when talking to the cluster. If the flag is not set, a set of standard locations can be searched for an existing kubeconfig file. | +| notifications-output-file | | No | The path to the file where notifications will be written to. If the flag is not present, notifications will be printed to stdout. | ## Conversion of Ingress resources to Gateway API