feat: add volume arg to volume rm
All checks were successful
continuous-integration/drone/push Build is passing

See #574
This commit is contained in:
2025-08-13 13:41:38 +02:00
committed by decentral1se
parent 2cfc40dc28
commit 091611b984
2 changed files with 100 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package app
import (
"context"
"fmt"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/autocomplete"
@ -71,7 +72,7 @@ var AppVolumeListCommand = &cobra.Command{
}
var AppVolumeRemoveCommand = &cobra.Command{
Use: "remove <domain> [flags]",
Use: "remove <domain> [volume] [flags]",
Short: "Remove volume(s) associated with an app",
Long: `Remove volumes associated with an app.
@ -83,6 +84,11 @@ you to make a seclection. Use the "?" key to see more help on navigating this
interface.
Passing "--force/-f" will select all volumes for removal. Be careful.`,
Example: ` # delete volumes interactively
abra app volume rm 1312.net
# delete specific volume
abra app volume rm 1312.net my_volume`,
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: func(
@ -94,6 +100,11 @@ Passing "--force/-f" will select all volumes for removal. Be careful.`,
Run: func(cmd *cobra.Command, args []string) {
app := internal.ValidateApp(args)
var volumeToDelete string
if len(args) == 2 {
volumeToDelete = args[1]
}
cl, err := client.New(app.Server)
if err != nil {
log.Fatal(err)
@ -119,6 +130,30 @@ Passing "--force/-f" will select all volumes for removal. Be careful.`,
}
volumeNames := client.GetVolumeNames(volumeList)
if volumeToDelete != "" {
var exactMatch bool
fullVolumeToDeleteName := fmt.Sprintf("%s_%s", app.StackName(), volumeToDelete)
for _, volName := range volumeNames {
if volName == fullVolumeToDeleteName {
exactMatch = true
}
}
if !exactMatch {
log.Fatalf("unable to remove volume: no volume with name '%s'?", volumeToDelete)
}
err := client.RemoveVolumes(cl, context.Background(), []string{fullVolumeToDeleteName}, internal.Force, 5)
if err != nil {
log.Fatalf("removing volume %s failed: %s", volumeToDelete, err)
}
log.Infof("volume %s removed successfully", volumeToDelete)
return
}
var volumesToRemove []string
if !internal.Force && !internal.NoInput {
volumesPrompt := &survey.MultiSelect{

View File

@ -63,20 +63,24 @@ teardown(){
# bats test_tags=slow
@test "remove volumes" {
sleep 3 # NOTE(d1): hack to avoid "network not found"
_deploy_app
_undeploy_app
run $ABRA app volume ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'test-volume'
run $ABRA app volume rm "$TEST_APP_DOMAIN" --force
assert_success
assert_output --partial 'volumes removed successfully'
run $ABRA app volume ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'no volumes created'
}
# bats test_tags=slow
@test "remove no volumes" {
sleep 3 # NOTE(d1): hack to avoid "network not found"
_deploy_app
_undeploy_app
@ -88,3 +92,59 @@ teardown(){
assert_success
assert_output --partial 'no volumes removed'
}
# bats test_tags=slow
@test "remove single volume" {
_deploy_app
_undeploy_app
run $ABRA app volume ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'test-volume'
assert_output --partial 'test-volume-two'
run $ABRA app volume rm "$TEST_APP_DOMAIN" test-volume-two --force
assert_success
assert_output --partial 'test-volume-two removed successfully'
run $ABRA app volume ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'test-volume'
refute_output --partial 'test-volume-two'
run $ABRA app volume rm "$TEST_APP_DOMAIN" --force
assert_success
assert_output --partial 'volumes removed successfully'
run $ABRA app volume ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'no volumes created'
}
# bats test_tags=slow
@test "remove single volume incorrect name" {
_deploy_app
_undeploy_app
run $ABRA app volume rm "$TEST_APP_DOMAIN" DOESNTEXIST --force
assert_failure
assert_output --partial 'no volume with name'
}
# bats test_tags=slow
@test "remove single volume doesn't delete similar name" {
_deploy_app
_undeploy_app
run $ABRA app volume ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'test-volume-two'
run $ABRA app volume rm "$TEST_APP_DOMAIN" test-volume --force
assert_success
assert_output --partial 'test-volume removed successfully'
run $ABRA app volume ls "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'test-volume-two'
}