11package project
22
33import (
4- "fmt"
5-
64 xfs "github.com/saitho/golang-extended-fs/v2"
75 "github.com/spf13/cobra"
86
@@ -12,6 +10,12 @@ import (
1210 "github.com/getstackhead/stackhead/system"
1311)
1412
13+ func reverse [S ~ []E , E any ](s S ) {
14+ for i , j := 0 , len (s )- 1 ; i < j ; i , j = i + 1 , j - 1 {
15+ s [i ], s [j ] = s [j ], s [i ]
16+ }
17+ }
18+
1519// DestroyApplication is a command object for Cobra that provides the destroy command
1620var DestroyApplication = & cobra.Command {
1721 Use : "destroy [path to project definition] [ipv4 address]" ,
@@ -26,22 +30,28 @@ var DestroyApplication = &cobra.Command{
2630 }
2731 commands .PrepareContext (args [1 ], system .ContextActionProjectDeploy , projectDefinition )
2832
29- modules := system .Context . GetModulesInOrder ( )
30- for i , j := 0 , len ( modules ) - 1 ; i < j ; i , j = i + 1 , j - 1 { // reverse module list
31- modules [ i ], modules [ j ] = modules [ j ], modules [ i ]
33+ latestDeployment , err := system .GetLatestDeployment ( projectDefinition )
34+ if err != nil {
35+ panic ( "unable to load latest deployment" + err . Error ())
3236 }
37+ system .Context .CurrentDeployment = * latestDeployment
3338
34- // Init modules
39+ modules := system .Context .GetModulesInOrder ()
40+ reverse (modules )
41+
42+ // Run modules destroy steps
3543 for _ , module := range modules {
3644 moduleSettings := system .GetModuleSettings (module .GetConfig ().Name )
3745 module .Init (moduleSettings )
3846 }
3947 taskRunner := routines.TaskRunner {}
4048
41- subTasks := []routines.Task {}
49+ subTasks := []routines.Task {
50+ // Remove resources from deployment
51+ routines .RemoveResources (latestDeployment ),
52+ }
4253
4354 if hasProjectDir , _ := xfs .HasFolder ("ssh://" + projectDefinition .GetDirectoryPath ()); hasProjectDir {
44-
4555 // Run destroy scripts from plugins
4656 for _ , module := range modules {
4757 moduleSettings := system .GetModuleSettings (module .GetConfig ().Name )
@@ -65,13 +75,10 @@ var DestroyApplication = &cobra.Command{
6575 })
6676 }
6777
68- _ = taskRunner .RunTask (routines.Task {
69- Name : fmt .Sprintf ("Destroying project \" %s\" on server with IP \" %s\" " , args [0 ], args [1 ]),
70- Run : func (r * routines.Task ) error {
71- return nil
72- },
73- SubTasks : subTasks ,
74- //RunAllSubTasksDespiteError: true,
75- })
78+ for _ , task := range subTasks {
79+ if err = taskRunner .RunTask (task ); err != nil {
80+ panic (err )
81+ }
82+ }
7683 },
7784}
0 commit comments