Skip to content

Commit

Permalink
edited tableString to use maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Devaansh-Kumar committed Jul 12, 2024
1 parent b961ec2 commit d579fa7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ func (pr *PrintRunner) PrintGatewayAPIObjects(cmd *cobra.Command, _ []string) er
return fmt.Errorf("failed to initialize namespace filter: %w", err)
}

gatewayResources, tableString, err := i2gw.ToGatewayAPIResources(cmd.Context(), pr.namespaceFilter, pr.inputFile, pr.providers, pr.getProviderSpecificFlags())
gatewayResources, notificationTablesMap, err := i2gw.ToGatewayAPIResources(cmd.Context(), pr.namespaceFilter, pr.inputFile, pr.providers, pr.getProviderSpecificFlags())
if err != nil {
return err
}

if tableString != nil {
fmt.Println(tableString)
for _, table := range notificationTablesMap {
fmt.Println(table)
}

pr.outputResult(gatewayResources)
Expand Down
9 changes: 4 additions & 5 deletions pkg/i2gw/ingress2gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"maps"
"strings"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -32,7 +31,7 @@ import (
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile string, providers []string, providerSpecificFlags map[string]map[string]string) ([]GatewayResources, *strings.Builder, error) {
func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile string, providers []string, providerSpecificFlags map[string]map[string]string) ([]GatewayResources, map[string]string, error) {
var clusterClient client.Client

if inputFile == "" {
Expand Down Expand Up @@ -76,12 +75,12 @@ func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile stri
errs = append(errs, conversionErrs...)
gatewayResources = append(gatewayResources, providerGatewayResources)
}
tableString := notifications.NotificationAggr.CreateNotificationTables()
notificationTablesMap := notifications.NotificationAggr.CreateNotificationTables()
if len(errs) > 0 {
return nil, tableString, aggregatedErrs(errs)
return nil, notificationTablesMap, aggregatedErrs(errs)
}

return gatewayResources, tableString, nil
return gatewayResources, notificationTablesMap, nil
}

func readProviderResourcesFromFile(ctx context.Context, providerByName map[ProviderName]Provider, inputFile string) error {
Expand Down
17 changes: 11 additions & 6 deletions pkg/i2gw/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ func (na *NotificationAggregator) DispatchNotification(notification Notification
na.mutex.Unlock()
}

// CreateNotificationTables takes all generated notifications and returns a string.Builder
// CreateNotificationTables takes all generated notifications and returns a map[string]string
// that displays the notifications in a tabular format based on provider
func (na *NotificationAggregator) CreateNotificationTables() *strings.Builder {
tableString := &strings.Builder{}
func (na *NotificationAggregator) CreateNotificationTables() map[string]string {
notificationTablesMap := make(map[string]string)

for provider, msgs := range na.Notifications {
t := tablewriter.NewWriter(tableString)
providerTable := strings.Builder{}

t := tablewriter.NewWriter(&providerTable)
t.SetHeader([]string{"Message Type", "Notification", "Calling Object"})
t.SetColWidth(200)
t.SetRowLine(true)
Expand All @@ -73,10 +76,12 @@ func (na *NotificationAggregator) CreateNotificationTables() *strings.Builder {
t.Append(row)
}

tableString.WriteString(fmt.Sprintf("Notifications from %v\n", provider))
providerTable.WriteString(fmt.Sprintf("Notifications from %v:\n", strings.ToUpper(provider)))
t.Render()
notificationTablesMap[provider] = providerTable.String()
}
return tableString

return notificationTablesMap
}

func convertObjectsToStr(ob []client.Object) string {
Expand Down

0 comments on commit d579fa7

Please sign in to comment.