Skip to content

Commit

Permalink
lxc: Add --force to cmdProjectDelete
Browse files Browse the repository at this point in the history
Signed-off-by: Long Tran <[email protected]>
(cherry picked from commit eaed1d4fc35c4deab260ae4388fbdde8d2fdb95f)
Signed-off-by: Mark Bolton <[email protected]>
License: Apache-2.0
  • Loading branch information
trannlong authored and boltmark committed Dec 9, 2024
1 parent 2ca9fa8 commit bc303ce
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions lxc/project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit bc303ce

Please sign in to comment.