From bc303ce5e23744c66c3d3d121716e952d4675e40 Mon Sep 17 00:00:00 2001 From: Long Tran Date: Tue, 28 May 2024 18:47:58 -0400 Subject: [PATCH] lxc: Add --force to cmdProjectDelete Signed-off-by: Long Tran (cherry picked from commit eaed1d4fc35c4deab260ae4388fbdde8d2fdb95f) Signed-off-by: Mark Bolton License: Apache-2.0 --- lxc/project.go | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/lxc/project.go b/lxc/project.go index 5c8252e1628e..5dcf97915e16 100644 --- a/lxc/project.go +++ b/lxc/project.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "errors" "fmt" "io" @@ -181,6 +182,8 @@ func (c *cmdProjectCreate) run(cmd *cobra.Command, args []string) error { type cmdProjectDelete struct { global *cmdGlobal project *cmdProject + + flagForce bool } func (c *cmdProjectDelete) command() *cobra.Command { @@ -191,6 +194,7 @@ func (c *cmdProjectDelete) command() *cobra.Command { cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G( `Delete projects`)) + cmd.Flags().BoolVarP(&c.flagForce, "force", "f", false, i18n.G("Force delete the project and everything it contains.")) cmd.RunE = c.run cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { @@ -204,6 +208,16 @@ func (c *cmdProjectDelete) command() *cobra.Command { return cmd } +func (c *cmdProjectDelete) promptConfirmation(name string) bool { + reader := bufio.NewReader(os.Stdin) + + fmt.Printf(i18n.G("Remove %s and everything it contains (instances, images, volumes, networks, ...) (yes/no): "), name) + input, _ := reader.ReadString('\n') + input = strings.TrimSuffix(input, "\n") + + return slices.Contains([]string{i18n.G("yes")}, strings.ToLower(input)) +} + func (c *cmdProjectDelete) run(cmd *cobra.Command, args []string) error { // Quick checks. exit, err := c.global.CheckArgs(cmd, args, 1, 1) @@ -228,10 +242,22 @@ func (c *cmdProjectDelete) run(cmd *cobra.Command, args []string) error { return errors.New(i18n.G("Missing project name")) } - // Delete the project - err = resource.server.DeleteProject(resource.name) - if err != nil { - return err + // Delete the project, server is unable to find the project here. + if c.flagForce { + if !c.promptConfirmation(resource.name) { + fmt.Println(i18n.G("Project deletion aborted")) + return nil + } + + err := resource.server.DeleteProjectForce(resource.name) + if err != nil { + return err + } + } else { + err = resource.server.DeleteProject(resource.name) + if err != nil { + return err + } } if !c.global.flagQuiet {