From 4b12affdb04536299fca82030f3d4cc5a6c2708e Mon Sep 17 00:00:00 2001 From: Tim Middleton Date: Wed, 6 Nov 2024 12:10:14 +0800 Subject: [PATCH] Reduce code duplication --- pkg/cmd/formatting.go | 56 +++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 34 deletions(-) diff --git a/pkg/cmd/formatting.go b/pkg/cmd/formatting.go index 18d38da..dfc9f01 100644 --- a/pkg/cmd/formatting.go +++ b/pkg/cmd/formatting.go @@ -2041,28 +2041,32 @@ func FormatProxyServers(services []config.ProxySummary, protocol string) string // common values table.AddRow(value.NodeID, value.HostIP, value.ServiceName) - if protocol == tcp { - table.AddColumnsToRow(formatLargeInteger(value.ConnectionCount), - formattingFunction(value.TotalBytesSent), formattingFunction(value.TotalBytesReceived)) - if OutputFormat == constants.WIDE { - table.AddColumnsToRow(formatLargeInteger(value.TotalMessagesSent), - formatLargeInteger(value.TotalMessagesReceived), formatLargeInteger(value.OutgoingByteBacklog), - formatLargeInteger(value.OutgoingMessageBacklog), formatLargeInteger(value.UnAuthConnectionAttempts)) - } - } else { - table.AddColumnsToRow(value.HTTPServerType, - formatLargeInteger(value.TotalRequestCount), formatLargeInteger(value.TotalErrorCount)) - if OutputFormat == constants.WIDE { - table.AddColumnsToRow(formatLargeInteger(value.ResponseCount1xx), - formatLargeInteger(value.ResponseCount2xx), formatLargeInteger(value.ResponseCount3xx), - formatLargeInteger(value.ResponseCount4xx), formatLargeInteger(value.ResponseCount5xx)) - } - } + addColumns(table, value, protocol, formattingFunction) } return table.String() } +func addColumns(table FormattedTable, value config.ProxySummary, protocol string, formattingFunction func(bytesValue int64) string) { + if protocol == tcp { + table.AddColumnsToRow(formatLargeInteger(value.ConnectionCount), + formattingFunction(value.TotalBytesSent), formattingFunction(value.TotalBytesReceived)) + if OutputFormat == constants.WIDE { + table.AddColumnsToRow(formatLargeInteger(value.TotalMessagesSent), + formatLargeInteger(value.TotalMessagesReceived), formatLargeInteger(value.OutgoingByteBacklog), + formatLargeInteger(value.OutgoingMessageBacklog), formatLargeInteger(value.UnAuthConnectionAttempts)) + } + } else { + table.AddColumnsToRow(value.HTTPServerType, + formatLargeInteger(value.TotalRequestCount), formatLargeInteger(value.TotalErrorCount)) + if OutputFormat == constants.WIDE { + table.AddColumnsToRow(formatLargeInteger(value.ResponseCount1xx), + formatLargeInteger(value.ResponseCount2xx), formatLargeInteger(value.ResponseCount3xx), + formatLargeInteger(value.ResponseCount4xx), formatLargeInteger(value.ResponseCount5xx)) + } + } +} + // FormatProxyServersSummary returns the proxy servers' summary information in a column formatted output // protocol is either tcp or http and will display a different format based upon this. func FormatProxyServersSummary(services []config.ProxySummary, protocol string) string { @@ -2114,23 +2118,7 @@ func FormatProxyServersSummary(services []config.ProxySummary, protocol string) } table.AddRow(value.ServiceName) - if protocol == tcp { - table.AddColumnsToRow(formatLargeInteger(value.ConnectionCount), - formattingFunction(value.TotalBytesSent), formattingFunction(value.TotalBytesReceived)) - if OutputFormat == constants.WIDE { - table.AddColumnsToRow(formatLargeInteger(value.TotalMessagesSent), - formatLargeInteger(value.TotalMessagesReceived), formatLargeInteger(value.OutgoingByteBacklog), - formatLargeInteger(value.OutgoingMessageBacklog), formatLargeInteger(value.UnAuthConnectionAttempts)) - } - } else { - table.AddColumnsToRow(value.HTTPServerType, - formatLargeInteger(value.TotalRequestCount), formatLargeInteger(value.TotalErrorCount)) - if OutputFormat == constants.WIDE { - table.AddColumnsToRow(formatLargeInteger(value.ResponseCount1xx), - formatLargeInteger(value.ResponseCount2xx), formatLargeInteger(value.ResponseCount3xx), - formatLargeInteger(value.ResponseCount4xx), formatLargeInteger(value.ResponseCount5xx)) - } - } + addColumns(table, value, protocol, formattingFunction) } return table.String()