From bd47e3ba792362a314e8a6084aa35eb3a579eb49 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Mon, 17 Aug 2026 14:58:38 +0200 Subject: [PATCH 1/3] Generic enum completion, switch os in list view --- cmd/completion/audit.go | 2 +- cmd/completion/completion.go | 14 ++++++++++++++ cmd/completion/component.go | 8 +------- cmd/completion/machine.go | 8 +------- cmd/tableprinters/switch.go | 4 ++-- tests/e2e/admin/switch_test.go | 24 ++++++++++++------------ 6 files changed, 31 insertions(+), 29 deletions(-) diff --git a/cmd/completion/audit.go b/cmd/completion/audit.go index ff1f066..e225a22 100644 --- a/cmd/completion/audit.go +++ b/cmd/completion/audit.go @@ -9,7 +9,7 @@ import ( ) func (c *Completion) AuditPhase(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return []string{apiv2.AuditPhase_AUDIT_PHASE_REQUEST.String(), apiv2.AuditPhase_AUDIT_PHASE_RESPONSE.String()}, cobra.ShellCompDirectiveNoFileComp + return c.genericEnums(apiv2.AuditPhase_name) } func (c *Completion) AuditStatusCodes(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { diff --git a/cmd/completion/completion.go b/cmd/completion/completion.go index aca23c9..dc10e6c 100644 --- a/cmd/completion/completion.go +++ b/cmd/completion/completion.go @@ -13,3 +13,17 @@ type Completion struct { func OutputFormat(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return []string{"table", "wide", "markdown", "json", "yaml", "template"}, cobra.ShellCompDirectiveNoFileComp } + +func (c *Completion) genericEnums(enums map[int32]string) ([]string, cobra.ShellCompDirective) { + var names []string + + for id, name := range enums { + if id == 0 { + // skip UNSPECIFIED + continue + } + names = append(names, name) + } + + return names, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cmd/completion/component.go b/cmd/completion/component.go index 9c6e686..710754f 100644 --- a/cmd/completion/component.go +++ b/cmd/completion/component.go @@ -6,11 +6,5 @@ import ( ) func (c *Completion) ComponentTypes(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - var names []string - - for _, name := range apiv2.ComponentType_name { - names = append(names, name) - } - - return names, cobra.ShellCompDirectiveNoFileComp + return c.genericEnums(apiv2.ComponentType_name) } diff --git a/cmd/completion/machine.go b/cmd/completion/machine.go index d964e59..5cb2735 100644 --- a/cmd/completion/machine.go +++ b/cmd/completion/machine.go @@ -69,11 +69,5 @@ func (c *Completion) Firewall(cmd *cobra.Command, args []string, toComplete stri } func (c *Completion) BMCCommands(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - var names []string - - for _, name := range apiv2.MachineBMCCommand_name { - names = append(names, name) - } - - return names, cobra.ShellCompDirectiveNoFileComp + return c.genericEnums(apiv2.MachineBMCCommand_name) } diff --git a/cmd/tableprinters/switch.go b/cmd/tableprinters/switch.go index 2a0cc47..032ff37 100644 --- a/cmd/tableprinters/switch.go +++ b/cmd/tableprinters/switch.go @@ -21,7 +21,7 @@ func (t *TablePrinter) SwitchTable(switches []*apiv2.Switch, wide bool) ([]strin rows [][]string ) - header := []string{"ID", "Partition", "Rack", "OS", "Status", "Last Sync"} + header := []string{"ID", "Partition", "Rack", "OS", "Metalcore", "Status", "Last Sync"} if wide { header = []string{"ID", "Partition", "Rack", "OS", "Metalcore", "IP", "Mode", "Last Sync", "Sync Duration", "Last Error"} t.t.DisableAutoWrap(true) @@ -171,7 +171,7 @@ func (t *TablePrinter) SwitchTable(switches []*apiv2.Switch, wide bool) ([]strin if wide { rows = append(rows, []string{id, partition, rack, os, metalCore, s.ManagementIp, mode, syncLast, syncDurStr, lastError}) } else { - rows = append(rows, []string{id, partition, rack, osIcon, shortStatus, syncLast}) + rows = append(rows, []string{id, partition, rack, osIcon, metalCore, shortStatus, syncLast}) } } diff --git a/tests/e2e/admin/switch_test.go b/tests/e2e/admin/switch_test.go index 300b8d3..11946d3 100644 --- a/tests/e2e/admin/switch_test.go +++ b/tests/e2e/admin/switch_test.go @@ -84,9 +84,9 @@ func Test_AdminSwitchCmd_List(t *testing.T) { }, }), WantTable: new(` - ID PARTITION RACK OS STATUS LAST SYNC - leaf01 fra-equ01 rack-1 🦔 ● - leaf02 fra-equ01 rack-1 🦔 ● + ID PARTITION RACK OS METALCORE STATUS LAST SYNC + leaf01 fra-equ01 rack-1 🦔 v0.9.1 (abc1234) ● + leaf02 fra-equ01 rack-1 🦔 v0.9.1 (abc1234) ● `), WantWideTable: new(` ID PARTITION RACK OS METALCORE IP MODE LAST SYNC SYNC DURATION LAST ERROR @@ -99,10 +99,10 @@ leaf01 fra-equ01 leaf02 fra-equ01 `), WantMarkdown: new(` - | ID | PARTITION | RACK | OS | STATUS | LAST SYNC | - |--------|-----------|--------|----|--------|-----------| - | leaf01 | fra-equ01 | rack-1 | 🦔 | ● | | - | leaf02 | fra-equ01 | rack-1 | 🦔 | ● | | + | ID | PARTITION | RACK | OS | METALCORE | STATUS | LAST SYNC | + |--------|-----------|--------|----|------------------|--------|-----------| + | leaf01 | fra-equ01 | rack-1 | 🦔 | v0.9.1 (abc1234) | ● | | + | leaf02 | fra-equ01 | rack-1 | 🦔 | v0.9.1 (abc1234) | ● | | `), }, } @@ -167,8 +167,8 @@ func Test_AdminSwitchCmd_Update(t *testing.T) { }, }), WantTable: new(` - ID PARTITION RACK OS STATUS LAST SYNC - leaf02 fra-equ01 rack-1 🦔 ● + ID PARTITION RACK OS METALCORE STATUS LAST SYNC + leaf02 fra-equ01 rack-1 🦔 v0.9.1 (abc1234) ● `), WantWideTable: new(` ID PARTITION RACK OS METALCORE IP MODE LAST SYNC SYNC DURATION LAST ERROR @@ -177,9 +177,9 @@ func Test_AdminSwitchCmd_Update(t *testing.T) { Template: new("{{ .id }} {{ .os.metal_core_version }}"), WantTemplate: new(`leaf02 v0.9.1 (abc1234), tags/v0.9.1`), WantMarkdown: new(` - | ID | PARTITION | RACK | OS | STATUS | LAST SYNC | - |--------|-----------|--------|----|--------|-----------| - | leaf02 | fra-equ01 | rack-1 | 🦔 | ● | | + | ID | PARTITION | RACK | OS | METALCORE | STATUS | LAST SYNC | + |--------|-----------|--------|----|------------------|--------|-----------| + | leaf02 | fra-equ01 | rack-1 | 🦔 | v0.9.1 (abc1234) | ● | | `), }, } From 148c209ebf9618d5b13742dcc2adae6ce715db07 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Mon, 17 Aug 2026 15:05:26 +0200 Subject: [PATCH 2/3] Fix --- cmd/tableprinters/switch_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/tableprinters/switch_test.go b/cmd/tableprinters/switch_test.go index 138b782..ea2848f 100644 --- a/cmd/tableprinters/switch_test.go +++ b/cmd/tableprinters/switch_test.go @@ -168,7 +168,7 @@ func TestTablePrinter_SwitchTable(t *testing.T) { name: "switches empty", switches: []*apiv2.Switch{}, wide: false, - wantHeader: []string{"ID", "Partition", "Rack", "OS", "Status", "Last Sync"}, + wantHeader: []string{"ID", "Partition", "Rack", "OS", "Metalcore", "Status", "Last Sync"}, wantRows: nil, }, { @@ -266,15 +266,15 @@ func TestTablePrinter_SwitchTable(t *testing.T) { }, }, wide: false, - wantHeader: []string{"ID", "Partition", "Rack", "OS", "Status", "Last Sync"}, + wantHeader: []string{"ID", "Partition", "Rack", "OS", "Metalcore", "Status", "Last Sync"}, wantRows: [][]string{ // FIXME: color of the dots is ignored; how to test for correct colors? - {"r01leaf01", "partition-a", "rack01", "🦔", color.GreenString(dot), "0s ago"}, // status green but error because one port is not in its desired state - {"r01leaf02", "partition-a", "rack01", "🐢", nbr + color.RedString(dot), "0s ago"}, // status red because in replace mode - {"r02leaf01", "partition-a", "rack02", apiv2.SwitchOSVendor_SWITCH_OS_VENDOR_UNSPECIFIED.String(), color.RedString(dot), "1h ago"}, // status red because last error came later than last sync - {"r02leaf02", "partition-a", "rack02", "", color.RedString(dot), "10m ago"}, // status red because last sync is too long ago - {"r03leaf01", "partition-a", "rack03", "", color.YellowString(dot), "0s ago"}, // status yellow because last sync duration was too long - {"r03leaf02", "partition-a", "rack03", "", color.YellowString(dot), ""}, // status yellow because not all connceted ports are up + {"r01leaf01", "partition-a", "rack01", "🦔", "",color.GreenString(dot), "0s ago"}, // status green but error because one port is not in its desired state + {"r01leaf02", "partition-a", "rack01", "🐢","", nbr + color.RedString(dot), "0s ago"}, // status red because in replace mode + {"r02leaf01", "partition-a", "rack02", apiv2.SwitchOSVendor_SWITCH_OS_VENDOR_UNSPECIFIED.String(), "",color.RedString(dot), "1h ago"}, // status red because last error came later than last sync + {"r02leaf02", "partition-a", "rack02", "", "",color.RedString(dot), "10m ago"}, // status red because last sync is too long ago + {"r03leaf01", "partition-a", "rack03", "", "",color.YellowString(dot), "0s ago"}, // status yellow because last sync duration was too long + {"r03leaf02", "partition-a", "rack03", "", "",color.YellowString(dot), ""}, // status yellow because not all connceted ports are up }, }, { From 9b40dbbd3143f4d6e8246d48812688b9b90afc20 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Mon, 17 Aug 2026 15:07:50 +0200 Subject: [PATCH 3/3] Fix --- cmd/tableprinters/switch_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/tableprinters/switch_test.go b/cmd/tableprinters/switch_test.go index ea2848f..8fdd53c 100644 --- a/cmd/tableprinters/switch_test.go +++ b/cmd/tableprinters/switch_test.go @@ -269,12 +269,12 @@ func TestTablePrinter_SwitchTable(t *testing.T) { wantHeader: []string{"ID", "Partition", "Rack", "OS", "Metalcore", "Status", "Last Sync"}, wantRows: [][]string{ // FIXME: color of the dots is ignored; how to test for correct colors? - {"r01leaf01", "partition-a", "rack01", "🦔", "",color.GreenString(dot), "0s ago"}, // status green but error because one port is not in its desired state - {"r01leaf02", "partition-a", "rack01", "🐢","", nbr + color.RedString(dot), "0s ago"}, // status red because in replace mode - {"r02leaf01", "partition-a", "rack02", apiv2.SwitchOSVendor_SWITCH_OS_VENDOR_UNSPECIFIED.String(), "",color.RedString(dot), "1h ago"}, // status red because last error came later than last sync - {"r02leaf02", "partition-a", "rack02", "", "",color.RedString(dot), "10m ago"}, // status red because last sync is too long ago - {"r03leaf01", "partition-a", "rack03", "", "",color.YellowString(dot), "0s ago"}, // status yellow because last sync duration was too long - {"r03leaf02", "partition-a", "rack03", "", "",color.YellowString(dot), ""}, // status yellow because not all connceted ports are up + {"r01leaf01", "partition-a", "rack01", "🦔", "", color.GreenString(dot), "0s ago"}, // status green but error because one port is not in its desired state + {"r01leaf02", "partition-a", "rack01", "🐢", "", nbr + color.RedString(dot), "0s ago"}, // status red because in replace mode + {"r02leaf01", "partition-a", "rack02", apiv2.SwitchOSVendor_SWITCH_OS_VENDOR_UNSPECIFIED.String(), "", color.RedString(dot), "1h ago"}, // status red because last error came later than last sync + {"r02leaf02", "partition-a", "rack02", "", "", color.RedString(dot), "10m ago"}, // status red because last sync is too long ago + {"r03leaf01", "partition-a", "rack03", "", "", color.YellowString(dot), "0s ago"}, // status yellow because last sync duration was too long + {"r03leaf02", "partition-a", "rack03", "", "", color.YellowString(dot), ""}, // status yellow because not all connceted ports are up }, }, {