Skip to content

Commit cca9beb

Browse files
authored
refactor: allow returning multiple actions on delete (#1393)
This PR allows returning multiple actions during delete.
1 parent 5504434 commit cca9beb

17 files changed

Lines changed: 25 additions & 27 deletions

File tree

internal/cmd/base/delete.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type DeleteCmd[T any] struct {
2424
NameSuggestions func(client hcapi2.Client) func() []string
2525
AdditionalFlags func(*cobra.Command)
2626
Fetch FetchFunc[T]
27-
Delete func(s state.State, cmd *cobra.Command, resource T) (*hcloud.Action, error)
27+
Delete func(s state.State, cmd *cobra.Command, resource T) ([]*hcloud.Action, error)
2828

2929
// FetchFunc is a factory function that produces [DeleteCmd.Fetch]. Should be set in case the resource has
3030
// more than a single identifier that is used in the positional arguments.
@@ -115,7 +115,7 @@ func (dc *DeleteCmd[T]) Run(s state.State, cmd *cobra.Command, args []string) er
115115

116116
for batch := range slices.Chunk(toDelete, deleteBatchSize) {
117117
results := make([]util.ResourceState, len(batch))
118-
actions := make([]*hcloud.Action, 0, len(batch))
118+
var actions []*hcloud.Action
119119

120120
for i, idOrName := range batch {
121121
results[i] = util.ResourceState{IDOrName: idOrName}
@@ -130,14 +130,12 @@ func (dc *DeleteCmd[T]) Run(s state.State, cmd *cobra.Command, args []string) er
130130
continue
131131
}
132132

133-
action, err := dc.Delete(s, cmd, resource)
133+
deleteActions, err := dc.Delete(s, cmd, resource)
134134
if err != nil {
135135
results[i].Error = err
136136
continue
137137
}
138-
if action != nil {
139-
actions = append(actions, action)
140-
}
138+
actions = append(actions, deleteActions...)
141139
}
142140

143141
for _, result := range results {

internal/cmd/base/delete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var mu = sync.Mutex{}
1919
var fakeDeleteCmd = &base.DeleteCmd[*fakeResource]{
2020
ResourceNameSingular: "Fake resource",
2121
ResourceNamePlural: "Fake resources",
22-
Delete: func(_ state.State, cmd *cobra.Command, _ *fakeResource) (*hcloud.Action, error) {
22+
Delete: func(_ state.State, cmd *cobra.Command, _ *fakeResource) ([]*hcloud.Action, error) {
2323
defer mu.Unlock()
2424
cmd.Println("Deleting fake resource")
2525
return nil, nil

internal/cmd/certificate/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var DeleteCmd = base.DeleteCmd[*hcloud.Certificate]{
1717
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.Certificate, *hcloud.Response, error) {
1818
return s.Client().Certificate().Get(s, idOrName)
1919
},
20-
Delete: func(s state.State, _ *cobra.Command, certificate *hcloud.Certificate) (*hcloud.Action, error) {
20+
Delete: func(s state.State, _ *cobra.Command, certificate *hcloud.Certificate) ([]*hcloud.Action, error) {
2121
_, err := s.Client().Certificate().Delete(s, certificate)
2222
return nil, err
2323
},

internal/cmd/firewall/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var DeleteCmd = base.DeleteCmd[*hcloud.Firewall]{
1717
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.Firewall, *hcloud.Response, error) {
1818
return s.Client().Firewall().Get(s, idOrName)
1919
},
20-
Delete: func(s state.State, _ *cobra.Command, firewall *hcloud.Firewall) (*hcloud.Action, error) {
20+
Delete: func(s state.State, _ *cobra.Command, firewall *hcloud.Firewall) ([]*hcloud.Action, error) {
2121
_, err := s.Client().Firewall().Delete(s, firewall)
2222
return nil, err
2323
},

internal/cmd/floatingip/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var DeleteCmd = base.DeleteCmd[*hcloud.FloatingIP]{
1717
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.FloatingIP, *hcloud.Response, error) {
1818
return s.Client().FloatingIP().Get(s, idOrName)
1919
},
20-
Delete: func(s state.State, _ *cobra.Command, floatingIP *hcloud.FloatingIP) (*hcloud.Action, error) {
20+
Delete: func(s state.State, _ *cobra.Command, floatingIP *hcloud.FloatingIP) ([]*hcloud.Action, error) {
2121
_, err := s.Client().FloatingIP().Delete(s, floatingIP)
2222
return nil, err
2323
},

internal/cmd/image/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var DeleteCmd = base.DeleteCmd[*hcloud.Image]{
2424
}
2525
return s.Client().Image().GetByID(s, id)
2626
},
27-
Delete: func(s state.State, _ *cobra.Command, image *hcloud.Image) (*hcloud.Action, error) {
27+
Delete: func(s state.State, _ *cobra.Command, image *hcloud.Image) ([]*hcloud.Action, error) {
2828
_, err := s.Client().Image().Delete(s, image)
2929
return nil, err
3030
},

internal/cmd/loadbalancer/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var DeleteCmd = base.DeleteCmd[*hcloud.LoadBalancer]{
1717
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.LoadBalancer, *hcloud.Response, error) {
1818
return s.Client().LoadBalancer().Get(s, idOrName)
1919
},
20-
Delete: func(s state.State, _ *cobra.Command, loadBalancer *hcloud.LoadBalancer) (*hcloud.Action, error) {
20+
Delete: func(s state.State, _ *cobra.Command, loadBalancer *hcloud.LoadBalancer) ([]*hcloud.Action, error) {
2121
_, err := s.Client().LoadBalancer().Delete(s, loadBalancer)
2222
return nil, err
2323
},

internal/cmd/network/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var DeleteCmd = base.DeleteCmd[*hcloud.Network]{
1717
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.Network, *hcloud.Response, error) {
1818
return s.Client().Network().Get(s, idOrName)
1919
},
20-
Delete: func(s state.State, _ *cobra.Command, network *hcloud.Network) (*hcloud.Action, error) {
20+
Delete: func(s state.State, _ *cobra.Command, network *hcloud.Network) ([]*hcloud.Action, error) {
2121
_, err := s.Client().Network().Delete(s, network)
2222
return nil, err
2323
},

internal/cmd/placementgroup/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var DeleteCmd = base.DeleteCmd[*hcloud.PlacementGroup]{
1717
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.PlacementGroup, *hcloud.Response, error) {
1818
return s.Client().PlacementGroup().Get(s, idOrName)
1919
},
20-
Delete: func(s state.State, _ *cobra.Command, placementGroup *hcloud.PlacementGroup) (*hcloud.Action, error) {
20+
Delete: func(s state.State, _ *cobra.Command, placementGroup *hcloud.PlacementGroup) ([]*hcloud.Action, error) {
2121
_, err := s.Client().PlacementGroup().Delete(s, placementGroup)
2222
return nil, err
2323
},

internal/cmd/primaryip/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var DeleteCmd = base.DeleteCmd[*hcloud.PrimaryIP]{
1717
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.PrimaryIP, *hcloud.Response, error) {
1818
return s.Client().PrimaryIP().Get(s, idOrName)
1919
},
20-
Delete: func(s state.State, _ *cobra.Command, primaryIP *hcloud.PrimaryIP) (*hcloud.Action, error) {
20+
Delete: func(s state.State, _ *cobra.Command, primaryIP *hcloud.PrimaryIP) ([]*hcloud.Action, error) {
2121
_, err := s.Client().PrimaryIP().Delete(s, primaryIP)
2222
return nil, err
2323
},

0 commit comments

Comments
 (0)