Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ability to delete multiple volumes #430

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 53 additions & 19 deletions cmd/volume/volume_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/alejandrojnm/go-pluralize"
"github.com/civo/civogo"
"github.com/civo/cli/common"
"github.com/civo/cli/config"
Expand All @@ -18,6 +19,7 @@ var volumeRemoveCmdExamples = []string{
"civo volume rm VOLUME_ID",
}

var volumesList []utility.ObjecteList
var volumeRemoveCmd = &cobra.Command{
Use: "remove",
Aliases: []string{"rm", "delete", "destroy"},
Expand All @@ -28,43 +30,75 @@ var volumeRemoveCmd = &cobra.Command{
utility.EnsureCurrentRegion()

client, err := config.CivoAPIClient()
if common.RegionSet != "" {
client.Region = common.RegionSet
}
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}
if common.RegionSet != "" {
client.Region = common.RegionSet
}

volume, err := client.FindVolume(args[0])
if err != nil {
if errors.Is(err, civogo.ZeroMatchesError) {
utility.Error("sorry there is no %s volume in your account", utility.Red(args[0]))
os.Exit(1)
if len(args) == 1 {
volume, err := client.FindVolume(args[0])
if err != nil {
if errors.Is(err, civogo.ZeroMatchesError) {
utility.Error("sorry there is no %s volume in your account", utility.Red(args[0]))
os.Exit(1)
}
if errors.Is(err, civogo.MultipleMatchesError) {
utility.Error("sorry we found more than one volume with that value in your account")
os.Exit(1)
}
}
if errors.Is(err, civogo.MultipleMatchesError) {
utility.Error("sorry we found more than one volume with that value in your account")
os.Exit(1)
volumesList = append(volumesList, utility.ObjecteList{ID: volume.ID, Name: volume.Name})
} else {
for _, v := range args {
volume, err := client.FindVolume(v)
if err == nil {
volumesList = append(volumesList, utility.ObjecteList{ID: volume.ID, Name: volume.Name})
}
}
}

if utility.UserConfirmedDeletion("volume", common.DefaultYes, volume.Name) {
volumeNameList := []string{}
for _, v := range volumesList {
volumeNameList = append(volumeNameList, v.Name)
}

_, err = client.DeleteVolume(volume.ID)
if err != nil {
utility.Error("error deleting the volume: %s", err)
os.Exit(1)
if utility.UserConfirmedDeletion(pluralize.Pluralize(len(volumesList), "Volume"), common.DefaultYes, strings.Join(volumeNameList, ", ")) {

for _, v := range volumesList {
vol, err := client.FindVolume(v.ID)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}
_, err = client.DeleteVolume(vol.ID)
if err != nil {
utility.Error("Error deleting the Volume: %s", err)
os.Exit(1)
}
}

ow := utility.NewOutputWriterWithMap(map[string]string{"id": volume.ID, "name": volume.Name})
ow := utility.NewOutputWriter()

for _, volume := range volumesList {
ow.StartLine()
ow.AppendDataWithLabel("id", volume.ID, "ID")
ow.AppendDataWithLabel("volume", volume.Name, "Volume")
}

switch common.OutputFormat {
case "json":
ow.WriteSingleObjectJSON(common.PrettySet)
if len(volumesList) == 1 {
ow.WriteSingleObjectJSON(common.PrettySet)
} else {
ow.WriteMultipleObjectsJSON(common.PrettySet)
}
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
fmt.Printf("The volume called %s with ID %s was deleted\n", utility.Green(volume.Name), utility.Green(volume.ID))
fmt.Printf("The %s (%s) have been deleted\n", pluralize.Pluralize(len(volumesList), "volume"), utility.Green(strings.Join(volumeNameList, ", ")))
}
} else {
fmt.Println("Operation aborted.")
Expand Down
Loading