Add --force flag, use MultiSelect for volumes and secrets

This commit is contained in:
knoflook 2021-08-05 16:45:07 +02:00
parent f528cf4cfa
commit 7dad9895eb
Signed by: knoflook
GPG Key ID: D6A1D0E8FC4FEF1C
1 changed files with 67 additions and 35 deletions

View File

@ -3,8 +3,8 @@ package app
import (
"context"
"errors"
"fmt"
"os"
"strings"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/client"
@ -12,6 +12,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
@ -25,11 +26,20 @@ var VolumesFlag = &cli.BoolFlag{
Destination: &Volumes,
}
var Force bool
var ForceFlag = &cli.BoolFlag{
Name: "force",
Value: false,
Destination: &Force,
}
var appRemoveCommand = &cli.Command{
Name: "remove",
Aliases: []string{"rm", "delete"},
Flags: []cli.Flag{
VolumesFlag,
ForceFlag,
},
Action: func(c *cli.Context) error {
// Check if app name was provided by user
@ -38,15 +48,17 @@ var appRemoveCommand = &cli.Command{
internal.ShowSubcommandHelpAndError(c, errors.New("No app name provided!"))
}
// Make sure that the user really wants to delete the app
response := false
prompt := &survey.Confirm{
Message: "About to delete " + AppName + ", are you sure?",
if !Force {
response := false
prompt := &survey.Confirm{
Message: "About to delete " + AppName + ", are you sure?",
}
survey.AskOne(prompt, &response)
if response == false {
return errors.New("User aborted app removal")
}
}
survey.AskOne(prompt, &response)
if response == false {
return errors.New("User aborted app removal")
}
// Remove the file
// Get app list
AppFiles, err := config.LoadAppFiles("")
if err != nil {
return err
@ -59,68 +71,88 @@ var appRemoveCommand = &cli.Command{
if err != nil {
return err
}
// Remove the file
err = os.Remove(AppPath)
if err != nil {
return err
} else {
logrus.Info("File :" + AppPath + " removed.")
}
// Check if the app has secrets and remove them too
// Check if the app has secrets
fs := filters.NewArgs()
fs.Add("name", AppName)
SecretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: fs})
if err != nil {
return err
}
// TODO: Actually remove secrets
Secrets := make(map[string]string)
SecretNames := []string{}
for _, cont := range SecretList {
Secrets[cont.Spec.Annotations.Name] = cont.ID //we have to map the names to ID's
SecretNames = append(SecretNames, cont.Spec.Annotations.Name)
//cl.SecretRemove(ctx, cont.ID)
}
SecretNamesToRemove := []string{}
SecretsPrompt := &survey.MultiSelect{
Message: "Which secrets do you want to remove?",
Options: SecretNames,
Default: SecretNames,
// Ask if the user really wants to remove the apps
if Force {
SecretNamesToRemove = SecretNames
} else {
SecretsPrompt := &survey.MultiSelect{
Message: "Which secrets do you want to remove?",
Options: SecretNames,
Default: SecretNames,
}
survey.AskOne(SecretsPrompt, &SecretNamesToRemove)
}
survey.AskOne(SecretsPrompt, &SecretNamesToRemove)
// Actually remove the secrets
for _, name := range SecretNamesToRemove {
// DEBUG: SecretIDsToRemove = append(SecretIDsToRemove, Secrets[name])
err := cl.SecretRemove(ctx, Secrets[name])
if err != nil {
return err
} else {
logrus.Info("Secret: " + name + " removed")
}
// SecretIDsToRemove = append(SecretIDsToRemove, Secrets[name])
}
// Get volumes associated with the app
VolumeListOKBody, err := cl.VolumeList(ctx, fs)
VolumeList := VolumeListOKBody.Volumes
if err != nil {
return err
}
Vols := []string{}
for _, vol := range VolumeList {
Vols = append(Vols, vol.Name)
}
// Remove the volumes if desired
if Volumes == true {
VolumeListOKBody, err := cl.VolumeList(ctx, fs)
VolumeList := VolumeListOKBody.Volumes
if err != nil {
return err
}
Vols := []string{}
for _, vol := range VolumeList {
Vols = append(Vols, vol.Name)
}
RemoveVols := []string{}
VolumesPrompt := &survey.MultiSelect{
Message: "Which volumes do you want to remove?",
Options: Vols,
Default: Vols,
// If there's no --force, ask if the user wants to remove
if Force {
RemoveVols = Vols
} else {
VolumesPrompt := &survey.MultiSelect{
Message: "Which volumes do you want to remove?",
Options: Vols,
Default: Vols,
}
survey.AskOne(VolumesPrompt, &RemoveVols)
}
survey.AskOne(VolumesPrompt, &RemoveVols)
fmt.Println("Volumes to remove: ", RemoveVols) //TODO: Replace with a remove method
// Remove the volumes
for _, vol := range RemoveVols {
err := cl.VolumeRemove(ctx, vol, false) // false is for force removing
err := cl.VolumeRemove(ctx, vol, Force) // false is for force removing
if err != nil {
return err
} else {
logrus.Info("Volume " + vol + " removed")
}
}
} else {
fmt.Println("Volumes will not be removed")
logrus.Info("No volumes were removed. Volumes left: " + strings.Join(Vols, ", "))
}
return nil