From d846f4952a6b5888ad39b648b7f041f430041e95 Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Fri, 31 Jan 2025 16:59:54 +0100 Subject: [PATCH] refactor: split up label command into separate Fetch/GetLabels/GetIDOrName (#967) This change is intended for an upcoming resource that has labels but doesn't have numeric IDs. --- internal/cmd/base/labels.go | 38 +++++++++++++--------- internal/cmd/certificate/labels.go | 21 ++++++++---- internal/cmd/certificate/labels_test.go | 16 +++++---- internal/cmd/firewall/labels.go | 21 ++++++++---- internal/cmd/firewall/labels_test.go | 16 +++++---- internal/cmd/floatingip/labels.go | 21 ++++++++---- internal/cmd/floatingip/labels_test.go | 16 +++++---- internal/cmd/image/labels.go | 22 ++++++++----- internal/cmd/image/labels_test.go | 16 +++++---- internal/cmd/loadbalancer/labels.go | 21 ++++++++---- internal/cmd/loadbalancer/labels_test.go | 16 +++++---- internal/cmd/network/labels.go | 21 ++++++++---- internal/cmd/network/labels_test.go | 16 +++++---- internal/cmd/placementgroup/labels.go | 21 ++++++++---- internal/cmd/placementgroup/labels_test.go | 16 +++++---- internal/cmd/primaryip/labels.go | 21 ++++++++---- internal/cmd/primaryip/labels_test.go | 16 +++++---- internal/cmd/server/labels.go | 21 ++++++++---- internal/cmd/server/labels_test.go | 36 +++++++++++--------- internal/cmd/sshkey/labels.go | 21 ++++++++---- internal/cmd/sshkey/labels_test.go | 16 +++++---- internal/cmd/volume/labels.go | 21 ++++++++---- internal/cmd/volume/labels_test.go | 22 ++++++++----- 23 files changed, 290 insertions(+), 182 deletions(-) diff --git a/internal/cmd/base/labels.go b/internal/cmd/base/labels.go index 5feadeff..78094e54 100644 --- a/internal/cmd/base/labels.go +++ b/internal/cmd/base/labels.go @@ -14,18 +14,20 @@ import ( ) // LabelCmds allows defining commands for adding labels to resources. -type LabelCmds struct { +type LabelCmds[T any] struct { ResourceNameSingular string ShortDescriptionAdd string ShortDescriptionRemove string NameSuggestions func(client hcapi2.Client) func() []string LabelKeySuggestions func(client hcapi2.Client) func(idOrName string) []string - FetchLabels func(s state.State, idOrName string) (map[string]string, int64, error) - SetLabels func(s state.State, id int64, labels map[string]string) error + Fetch func(s state.State, idOrName string) (T, error) + SetLabels func(s state.State, resource T, labels map[string]string) error + GetLabels func(resource T) map[string]string + GetIDOrName func(resource T) string } // AddCobraCommand creates a command that can be registered with cobra. -func (lc *LabelCmds) AddCobraCommand(s state.State) *cobra.Command { +func (lc *LabelCmds[T]) AddCobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("add-label [--overwrite] <%s>