diff --git a/cmd/devstream/destroy.go b/cmd/devstream/destroy.go index 2f4a053df..3770137c0 100644 --- a/cmd/devstream/destroy.go +++ b/cmd/devstream/destroy.go @@ -10,6 +10,8 @@ import ( "github.com/devstream-io/devstream/pkg/util/log" ) +var isForceDestroy bool + var destroyCMD = &cobra.Command{ Use: "destroy", Short: "Destroy DevOps tools deployment according to DevStream configuration file & state file", @@ -19,7 +21,7 @@ var destroyCMD = &cobra.Command{ func destroyCMDFunc(cmd *cobra.Command, args []string) { log.Info("Destroy started.") - if err := pluginengine.Destroy(configFile, continueDirectly); err != nil { + if err := pluginengine.Destroy(configFile, continueDirectly, isForceDestroy); err != nil { log.Errorf("Destroy failed => %s.", err) os.Exit(1) } @@ -27,6 +29,7 @@ func destroyCMDFunc(cmd *cobra.Command, args []string) { } func init() { + destroyCMD.Flags().BoolVarP(&isForceDestroy, "force", "", false, "force destroy by config") destroyCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file") destroyCMD.Flags().BoolVarP(&continueDirectly, "yes", "y", false, "destroy directly without confirmation") diff --git a/internal/pkg/pluginengine/change_helper.go b/internal/pkg/pluginengine/change_helper.go index 89ee1286c..f2673a0dd 100644 --- a/internal/pkg/pluginengine/change_helper.go +++ b/internal/pkg/pluginengine/change_helper.go @@ -185,7 +185,7 @@ func changesForDelete(smgr statemanager.Manager, cfg *configmanager.Config, isFo return changes, nil } -func GetChangesForDestroy(smgr statemanager.Manager) ([]*Change, error) { +func GetChangesForDestroy(smgr statemanager.Manager, isForceDestroy bool) ([]*Change, error) { changes := make([]*Change, 0) // rebuilding tools from config @@ -210,6 +210,12 @@ func GetChangesForDestroy(smgr statemanager.Manager) ([]*Change, error) { for i := len(batchesOfTools) - 1; i >= 0; i-- { batch := batchesOfTools[i] for _, tool := range batch { + if !isForceDestroy { + state := smgr.GetState(statemanager.StateKeyGenerateFunc(&tool)) + if state == nil { + continue + } + } description := fmt.Sprintf("Tool (%s/%s) will be deleted.", tool.Name, tool.InstanceID) changes = append(changes, generateDeleteAction(&tool, description)) } diff --git a/internal/pkg/pluginengine/cmd_destroy.go b/internal/pkg/pluginengine/cmd_destroy.go index 90cfe508a..10aa1f705 100644 --- a/internal/pkg/pluginengine/cmd_destroy.go +++ b/internal/pkg/pluginengine/cmd_destroy.go @@ -11,7 +11,7 @@ import ( "github.com/devstream-io/devstream/pkg/util/log" ) -func Destroy(configFile string, continueDirectly bool) error { +func Destroy(configFile string, continueDirectly bool, isForceDestroy bool) error { cfg, err := configmanager.NewManager(configFile).LoadConfig() if err != nil { return err @@ -30,7 +30,7 @@ func Destroy(configFile string, continueDirectly bool) error { return err } - changes, err := GetChangesForDestroy(smgr) + changes, err := GetChangesForDestroy(smgr, isForceDestroy) if err != nil { log.Debugf("Get changes failed: %s.", err) return err