Skip to content

Commit

Permalink
make base.LabelCmds generic
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Jan 30, 2025
1 parent 4ea8a26 commit 6d4faef
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 97 deletions.
18 changes: 9 additions & 9 deletions internal/cmd/base/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +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
Fetch func(s state.State, idOrName string) (any, error)
SetLabels func(s state.State, resource any, labels map[string]string) error
GetLabels func(resource any) map[string]string
GetIDOrName func(resource any) string
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> <label>...", util.ToKebabCase(lc.ResourceNameSingular)),
Short: lc.ShortDescriptionAdd,
Expand All @@ -45,7 +45,7 @@ func (lc *LabelCmds) AddCobraCommand(s state.State) *cobra.Command {
}

// RunAdd executes an add label command
func (lc *LabelCmds) RunAdd(s state.State, cmd *cobra.Command, args []string) error {
func (lc *LabelCmds[T]) RunAdd(s state.State, cmd *cobra.Command, args []string) error {
overwrite, _ := cmd.Flags().GetBool("overwrite")
idOrName := args[0]

Expand Down Expand Up @@ -91,7 +91,7 @@ func validateAddLabel(_ *cobra.Command, args []string) error {
}

// RemoveCobraCommand creates a command that can be registered with cobra.
func (lc *LabelCmds) RemoveCobraCommand(s state.State) *cobra.Command {
func (lc *LabelCmds[T]) RemoveCobraCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("remove-label <%s> (--all | <label>...)", util.ToKebabCase(lc.ResourceNameSingular)),
Short: lc.ShortDescriptionRemove,
Expand All @@ -117,7 +117,7 @@ func (lc *LabelCmds) RemoveCobraCommand(s state.State) *cobra.Command {
}

// RunRemove executes a remove label command
func (lc *LabelCmds) RunRemove(s state.State, cmd *cobra.Command, args []string) error {
func (lc *LabelCmds[T]) RunRemove(s state.State, cmd *cobra.Command, args []string) error {
all, _ := cmd.Flags().GetBool("all")
idOrName := args[0]

Expand Down
13 changes: 5 additions & 8 deletions internal/cmd/certificate/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var LabelCmds = base.LabelCmds{
var LabelCmds = base.LabelCmds[*hcloud.Certificate]{
ResourceNameSingular: "certificate",
ShortDescriptionAdd: "Add a label to an certificate",
ShortDescriptionRemove: "Remove a label from an certificate",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.Certificate().Names },
LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Certificate().LabelKeys },
Fetch: func(s state.State, idOrName string) (any, error) {
Fetch: func(s state.State, idOrName string) (*hcloud.Certificate, error) {
certificate, _, err := s.Client().Certificate().Get(s, idOrName)
if err != nil {
return nil, err
Expand All @@ -26,20 +26,17 @@ var LabelCmds = base.LabelCmds{
}
return certificate, nil
},
SetLabels: func(s state.State, resource any, labels map[string]string) error {
cert := resource.(*hcloud.Certificate)
SetLabels: func(s state.State, cert *hcloud.Certificate, labels map[string]string) error {
opts := hcloud.CertificateUpdateOpts{
Labels: labels,
}
_, _, err := s.Client().Certificate().Update(s, cert, opts)
return err
},
GetLabels: func(resource any) map[string]string {
cert := resource.(*hcloud.Certificate)
GetLabels: func(cert *hcloud.Certificate) map[string]string {
return cert.Labels
},
GetIDOrName: func(resource any) string {
cert := resource.(*hcloud.Certificate)
GetIDOrName: func(cert *hcloud.Certificate) string {
return strconv.FormatInt(cert.ID, 10)
},
}
13 changes: 5 additions & 8 deletions internal/cmd/firewall/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var LabelCmds = base.LabelCmds{
var LabelCmds = base.LabelCmds[*hcloud.Firewall]{
ResourceNameSingular: "firewall",
ShortDescriptionAdd: "Add a label to an firewall",
ShortDescriptionRemove: "Remove a label from an firewall",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names },
LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Firewall().LabelKeys },
Fetch: func(s state.State, idOrName string) (any, error) {
Fetch: func(s state.State, idOrName string) (*hcloud.Firewall, error) {
firewall, _, err := s.Client().Firewall().Get(s, idOrName)
if err != nil {
return nil, err
Expand All @@ -26,20 +26,17 @@ var LabelCmds = base.LabelCmds{
}
return firewall, nil
},
SetLabels: func(s state.State, resource any, labels map[string]string) error {
firewall := resource.(*hcloud.Firewall)
SetLabels: func(s state.State, firewall *hcloud.Firewall, labels map[string]string) error {
opts := hcloud.FirewallUpdateOpts{
Labels: labels,
}
_, _, err := s.Client().Firewall().Update(s, firewall, opts)
return err
},
GetLabels: func(resource any) map[string]string {
firewall := resource.(*hcloud.Firewall)
GetLabels: func(firewall *hcloud.Firewall) map[string]string {
return firewall.Labels
},
GetIDOrName: func(resource any) string {
firewall := resource.(*hcloud.Firewall)
GetIDOrName: func(firewall *hcloud.Firewall) string {
return strconv.FormatInt(firewall.ID, 10)
},
}
13 changes: 5 additions & 8 deletions internal/cmd/floatingip/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var LabelCmds = base.LabelCmds{
var LabelCmds = base.LabelCmds[*hcloud.FloatingIP]{
ResourceNameSingular: "Floating IP",
ShortDescriptionAdd: "Add a label to an Floating IP",
ShortDescriptionRemove: "Remove a label from an Floating IP",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names },
LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.FloatingIP().LabelKeys },
Fetch: func(s state.State, idOrName string) (any, error) {
Fetch: func(s state.State, idOrName string) (*hcloud.FloatingIP, error) {
floatingIP, _, err := s.Client().FloatingIP().Get(s, idOrName)
if err != nil {
return nil, err
Expand All @@ -26,20 +26,17 @@ var LabelCmds = base.LabelCmds{
}
return floatingIP, nil
},
SetLabels: func(s state.State, resource any, labels map[string]string) error {
floatingIP := resource.(*hcloud.FloatingIP)
SetLabels: func(s state.State, floatingIP *hcloud.FloatingIP, labels map[string]string) error {
opts := hcloud.FloatingIPUpdateOpts{
Labels: labels,
}
_, _, err := s.Client().FloatingIP().Update(s, floatingIP, opts)
return err
},
GetLabels: func(resource any) map[string]string {
floatingIP := resource.(*hcloud.FloatingIP)
GetLabels: func(floatingIP *hcloud.FloatingIP) map[string]string {
return floatingIP.Labels
},
GetIDOrName: func(resource any) string {
floatingIP := resource.(*hcloud.FloatingIP)
GetIDOrName: func(floatingIP *hcloud.FloatingIP) string {
return strconv.FormatInt(floatingIP.ID, 10)
},
}
13 changes: 5 additions & 8 deletions internal/cmd/image/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var LabelCmds = base.LabelCmds{
var LabelCmds = base.LabelCmds[*hcloud.Image]{
ResourceNameSingular: "image",
ShortDescriptionAdd: "Add a label to an image",
ShortDescriptionRemove: "Remove a label from an image",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names },
LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Image().LabelKeys },
Fetch: func(s state.State, idOrName string) (any, error) {
Fetch: func(s state.State, idOrName string) (*hcloud.Image, error) {
id, err := strconv.ParseInt(idOrName, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid snapshot or backup ID %q", idOrName)
Expand All @@ -30,20 +30,17 @@ var LabelCmds = base.LabelCmds{
}
return image, nil
},
SetLabels: func(s state.State, resource any, labels map[string]string) error {
image := resource.(*hcloud.Image)
SetLabels: func(s state.State, image *hcloud.Image, labels map[string]string) error {
opts := hcloud.ImageUpdateOpts{
Labels: labels,
}
_, _, err := s.Client().Image().Update(s, image, opts)
return err
},
GetLabels: func(resource any) map[string]string {
image := resource.(*hcloud.Image)
GetLabels: func(image *hcloud.Image) map[string]string {
return image.Labels
},
GetIDOrName: func(resource any) string {
image := resource.(*hcloud.Image)
GetIDOrName: func(image *hcloud.Image) string {
return strconv.FormatInt(image.ID, 10)
},
}
13 changes: 5 additions & 8 deletions internal/cmd/loadbalancer/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var LabelCmds = base.LabelCmds{
var LabelCmds = base.LabelCmds[*hcloud.LoadBalancer]{
ResourceNameSingular: "Load Balancer",
ShortDescriptionAdd: "Add a label to a Load Balancer",
ShortDescriptionRemove: "Remove a label from a Load Balancer",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names },
LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.LoadBalancer().LabelKeys },
Fetch: func(s state.State, idOrName string) (any, error) {
Fetch: func(s state.State, idOrName string) (*hcloud.LoadBalancer, error) {
loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName)
if err != nil {
return nil, err
Expand All @@ -26,20 +26,17 @@ var LabelCmds = base.LabelCmds{
}
return loadBalancer, nil
},
SetLabels: func(s state.State, resource any, labels map[string]string) error {
loadBalancer := resource.(*hcloud.LoadBalancer)
SetLabels: func(s state.State, loadBalancer *hcloud.LoadBalancer, labels map[string]string) error {
opts := hcloud.LoadBalancerUpdateOpts{
Labels: labels,
}
_, _, err := s.Client().LoadBalancer().Update(s, loadBalancer, opts)
return err
},
GetLabels: func(resource any) map[string]string {
loadBalancer := resource.(*hcloud.LoadBalancer)
GetLabels: func(loadBalancer *hcloud.LoadBalancer) map[string]string {
return loadBalancer.Labels
},
GetIDOrName: func(resource any) string {
loadBalancer := resource.(*hcloud.LoadBalancer)
GetIDOrName: func(loadBalancer *hcloud.LoadBalancer) string {
return strconv.FormatInt(loadBalancer.ID, 10)
},
}
13 changes: 5 additions & 8 deletions internal/cmd/network/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var LabelCmds = base.LabelCmds{
var LabelCmds = base.LabelCmds[*hcloud.Network]{
ResourceNameSingular: "Network",
ShortDescriptionAdd: "Add a label to a Network",
ShortDescriptionRemove: "Remove a label from a Network",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names },
LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Network().LabelKeys },
Fetch: func(s state.State, idOrName string) (any, error) {
Fetch: func(s state.State, idOrName string) (*hcloud.Network, error) {
network, _, err := s.Client().Network().Get(s, idOrName)
if err != nil {
return nil, err
Expand All @@ -26,20 +26,17 @@ var LabelCmds = base.LabelCmds{
}
return network, nil
},
SetLabels: func(s state.State, resource any, labels map[string]string) error {
network := resource.(*hcloud.Network)
SetLabels: func(s state.State, network *hcloud.Network, labels map[string]string) error {
opts := hcloud.NetworkUpdateOpts{
Labels: labels,
}
_, _, err := s.Client().Network().Update(s, network, opts)
return err
},
GetLabels: func(resource any) map[string]string {
network := resource.(*hcloud.Network)
GetLabels: func(network *hcloud.Network) map[string]string {
return network.Labels
},
GetIDOrName: func(resource any) string {
network := resource.(*hcloud.Network)
GetIDOrName: func(network *hcloud.Network) string {
return strconv.FormatInt(network.ID, 10)
},
}
13 changes: 5 additions & 8 deletions internal/cmd/placementgroup/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var LabelCmds = base.LabelCmds{
var LabelCmds = base.LabelCmds[*hcloud.PlacementGroup]{
ResourceNameSingular: "placement group",
ShortDescriptionAdd: "Add a label to a placement group",
ShortDescriptionRemove: "Remove a label from a placement group",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names },
LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.PlacementGroup().LabelKeys },
Fetch: func(s state.State, idOrName string) (any, error) {
Fetch: func(s state.State, idOrName string) (*hcloud.PlacementGroup, error) {
placementGroup, _, err := s.Client().PlacementGroup().Get(s, idOrName)
if err != nil {
return nil, err
Expand All @@ -26,20 +26,17 @@ var LabelCmds = base.LabelCmds{
}
return placementGroup, nil
},
SetLabels: func(s state.State, resource any, labels map[string]string) error {
placementGroup := resource.(*hcloud.PlacementGroup)
SetLabels: func(s state.State, placementGroup *hcloud.PlacementGroup, labels map[string]string) error {
opts := hcloud.PlacementGroupUpdateOpts{
Labels: labels,
}
_, _, err := s.Client().PlacementGroup().Update(s, placementGroup, opts)
return err
},
GetLabels: func(resource any) map[string]string {
placementGroup := resource.(*hcloud.PlacementGroup)
GetLabels: func(placementGroup *hcloud.PlacementGroup) map[string]string {
return placementGroup.Labels
},
GetIDOrName: func(resource any) string {
placementGroup := resource.(*hcloud.PlacementGroup)
GetIDOrName: func(placementGroup *hcloud.PlacementGroup) string {
return strconv.FormatInt(placementGroup.ID, 10)
},
}
13 changes: 5 additions & 8 deletions internal/cmd/primaryip/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var LabelCmds = base.LabelCmds{
var LabelCmds = base.LabelCmds[*hcloud.PrimaryIP]{
ResourceNameSingular: "primary-ip",
ShortDescriptionAdd: "Add a label to a Primary IP",
ShortDescriptionRemove: "Remove a label from a Primary IP",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names },
LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.PrimaryIP().LabelKeys },
Fetch: func(s state.State, idOrName string) (any, error) {
Fetch: func(s state.State, idOrName string) (*hcloud.PrimaryIP, error) {
primaryIP, _, err := s.Client().PrimaryIP().Get(s, idOrName)
if err != nil {
return nil, err
Expand All @@ -26,20 +26,17 @@ var LabelCmds = base.LabelCmds{
}
return primaryIP, nil
},
SetLabels: func(s state.State, resource any, labels map[string]string) error {
primaryIP := resource.(*hcloud.PrimaryIP)
SetLabels: func(s state.State, primaryIP *hcloud.PrimaryIP, labels map[string]string) error {
opts := hcloud.PrimaryIPUpdateOpts{
Labels: &labels,
}
_, _, err := s.Client().PrimaryIP().Update(s, primaryIP, opts)
return err
},
GetLabels: func(resource any) map[string]string {
primaryIP := resource.(*hcloud.PrimaryIP)
GetLabels: func(primaryIP *hcloud.PrimaryIP) map[string]string {
return primaryIP.Labels
},
GetIDOrName: func(resource any) string {
primaryIP := resource.(*hcloud.PrimaryIP)
GetIDOrName: func(primaryIP *hcloud.PrimaryIP) string {
return strconv.FormatInt(primaryIP.ID, 10)
},
}
Loading

0 comments on commit 6d4faef

Please sign in to comment.