parent
8554e68418
commit
6732edf8db
|
@ -1,7 +1,19 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/client"
|
||||
"coopcloud.tech/abra/config"
|
||||
"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"
|
||||
)
|
||||
|
||||
|
@ -16,9 +28,114 @@ var VolumesFlag = &cli.BoolFlag{
|
|||
}
|
||||
|
||||
var appRemoveCommand = &cli.Command{
|
||||
Name: "remove",
|
||||
Name: "remove",
|
||||
Aliases: []string{"rm", "delete"},
|
||||
Flags: []cli.Flag{
|
||||
VolumesFlag,
|
||||
internal.SecretsFlag,
|
||||
internal.ForceFlag,
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
appName := c.Args().First()
|
||||
if appName == "" {
|
||||
internal.ShowSubcommandHelpAndError(c, errors.New("No app name provided!"))
|
||||
}
|
||||
if !internal.Force {
|
||||
response := false
|
||||
prompt := &survey.Confirm{
|
||||
Message: fmt.Sprintf("About to delete %s, are you sure", appName),
|
||||
}
|
||||
survey.AskOne(prompt, &response)
|
||||
if !response {
|
||||
return errors.New("User aborted app removal")
|
||||
}
|
||||
}
|
||||
appFiles, err := config.LoadAppFiles("")
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
appPath := appFiles[appName].Path
|
||||
fmt.Println(appFiles)
|
||||
host := appFiles[appName].Server
|
||||
ctx := context.Background()
|
||||
cl, err := client.NewClientWithContext(host)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
err = os.Remove(appPath)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
} else {
|
||||
logrus.Info(fmt.Sprintf("File: %s removed", appPath))
|
||||
}
|
||||
|
||||
fs := filters.NewArgs()
|
||||
fs.Add("name", appName)
|
||||
secretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: fs})
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
secretNamesToRemove := []string{}
|
||||
|
||||
if internal.Force {
|
||||
secretNamesToRemove = secretNames
|
||||
} else {
|
||||
secretsPrompt := &survey.MultiSelect{
|
||||
Message: "Which secrets do you want to remove?",
|
||||
Options: secretNames,
|
||||
Default: secretNames,
|
||||
}
|
||||
survey.AskOne(secretsPrompt, &secretNamesToRemove)
|
||||
}
|
||||
|
||||
for _, name := range secretNamesToRemove {
|
||||
err := cl.SecretRemove(ctx, secrets[name])
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
} else {
|
||||
logrus.Info(fmt.Sprintf("Secret: %s removed", name))
|
||||
}
|
||||
}
|
||||
|
||||
volumeListOKBody, err := cl.VolumeList(ctx, fs)
|
||||
volumeList := volumeListOKBody.Volumes
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
vols := []string{}
|
||||
for _, vol := range volumeList {
|
||||
vols = append(vols, vol.Name)
|
||||
}
|
||||
|
||||
if Volumes {
|
||||
removeVols := []string{}
|
||||
if internal.Force {
|
||||
removeVols = vols
|
||||
} else {
|
||||
volumesPrompt := &survey.MultiSelect{
|
||||
Message: "Which volumes do you want to remove?",
|
||||
Options: vols,
|
||||
Default: vols,
|
||||
}
|
||||
survey.AskOne(volumesPrompt, &removeVols)
|
||||
}
|
||||
for _, vol := range removeVols {
|
||||
err := cl.VolumeRemove(ctx, vol, internal.Force) // last argument is for force removing
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
} else {
|
||||
logrus.Info("Volume " + vol + " removed")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logrus.Info("No volumes were removed. Volumes left: " + strings.Join(vols, ", "))
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
|
|
@ -42,3 +42,12 @@ var ContextFlag = &cli.StringFlag{
|
|||
Aliases: []string{"c"},
|
||||
Destination: &Context,
|
||||
}
|
||||
|
||||
var Force bool
|
||||
|
||||
var ForceFlag = &cli.BoolFlag{
|
||||
Name: "force",
|
||||
Value: false,
|
||||
Aliases: []string{"f"},
|
||||
Destination: &Force,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue