Code cleanup for app remove
This commit is contained in:
parent
7dad9895eb
commit
09a06997e1
@ -3,6 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -26,133 +27,113 @@ var VolumesFlag = &cli.BoolFlag{
|
|||||||
Destination: &Volumes,
|
Destination: &Volumes,
|
||||||
}
|
}
|
||||||
|
|
||||||
var Force bool
|
|
||||||
|
|
||||||
var ForceFlag = &cli.BoolFlag{
|
|
||||||
Name: "force",
|
|
||||||
Value: false,
|
|
||||||
Destination: &Force,
|
|
||||||
}
|
|
||||||
|
|
||||||
var appRemoveCommand = &cli.Command{
|
var appRemoveCommand = &cli.Command{
|
||||||
Name: "remove",
|
Name: "remove",
|
||||||
Aliases: []string{"rm", "delete"},
|
Aliases: []string{"rm", "delete"},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
VolumesFlag,
|
VolumesFlag,
|
||||||
ForceFlag,
|
internal.ForceFlag,
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
// Check if app name was provided by user
|
appName := c.Args().First()
|
||||||
AppName := c.Args().First()
|
if appName == "" {
|
||||||
if AppName == "" {
|
|
||||||
internal.ShowSubcommandHelpAndError(c, errors.New("No app name provided!"))
|
internal.ShowSubcommandHelpAndError(c, errors.New("No app name provided!"))
|
||||||
}
|
}
|
||||||
// Make sure that the user really wants to delete the app
|
if !internal.Force {
|
||||||
if !Force {
|
|
||||||
response := false
|
response := false
|
||||||
prompt := &survey.Confirm{
|
prompt := &survey.Confirm{
|
||||||
Message: "About to delete " + AppName + ", are you sure?",
|
Message: fmt.Sprintf("About to delete %s, are you sure", appName),
|
||||||
}
|
}
|
||||||
survey.AskOne(prompt, &response)
|
survey.AskOne(prompt, &response)
|
||||||
if response == false {
|
if !response {
|
||||||
return errors.New("User aborted app removal")
|
return errors.New("User aborted app removal")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Get app list
|
appFiles, err := config.LoadAppFiles("")
|
||||||
AppFiles, err := config.LoadAppFiles("")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
AppPath := AppFiles[AppName].Path
|
appPath := appFiles[appName].Path
|
||||||
host := AppFiles[AppName].Server
|
fmt.Println(appFiles)
|
||||||
// Initialize docker client
|
host := appFiles[appName].Server
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
cl, err := client.NewClientWithContext(host)
|
cl, err := client.NewClientWithContext(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
// Remove the file
|
err = os.Remove(appPath)
|
||||||
err = os.Remove(AppPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
} else {
|
} else {
|
||||||
logrus.Info("File :" + AppPath + " removed.")
|
logrus.Info(fmt.Sprintf("File: %s removed", appPath))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the app has secrets
|
|
||||||
fs := filters.NewArgs()
|
fs := filters.NewArgs()
|
||||||
fs.Add("name", AppName)
|
fs.Add("name", appName)
|
||||||
SecretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: fs})
|
secretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: fs})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
Secrets := make(map[string]string)
|
secrets := make(map[string]string)
|
||||||
SecretNames := []string{}
|
secretNames := []string{}
|
||||||
for _, cont := range SecretList {
|
for _, cont := range secretList {
|
||||||
Secrets[cont.Spec.Annotations.Name] = cont.ID //we have to map the names to ID's
|
secrets[cont.Spec.Annotations.Name] = cont.ID //we have to map the names to ID's
|
||||||
SecretNames = append(SecretNames, cont.Spec.Annotations.Name)
|
secretNames = append(secretNames, cont.Spec.Annotations.Name)
|
||||||
}
|
}
|
||||||
SecretNamesToRemove := []string{}
|
secretNamesToRemove := []string{}
|
||||||
|
|
||||||
// Ask if the user really wants to remove the apps
|
if internal.Force {
|
||||||
if Force {
|
secretNamesToRemove = secretNames
|
||||||
SecretNamesToRemove = SecretNames
|
|
||||||
} else {
|
} else {
|
||||||
SecretsPrompt := &survey.MultiSelect{
|
secretsPrompt := &survey.MultiSelect{
|
||||||
Message: "Which secrets do you want to remove?",
|
Message: "Which secrets do you want to remove?",
|
||||||
Options: SecretNames,
|
Options: secretNames,
|
||||||
Default: SecretNames,
|
Default: secretNames,
|
||||||
}
|
}
|
||||||
survey.AskOne(SecretsPrompt, &SecretNamesToRemove)
|
survey.AskOne(secretsPrompt, &secretNamesToRemove)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actually remove the secrets
|
for _, name := range secretNamesToRemove {
|
||||||
for _, name := range SecretNamesToRemove {
|
err := cl.SecretRemove(ctx, secrets[name])
|
||||||
// DEBUG: SecretIDsToRemove = append(SecretIDsToRemove, Secrets[name])
|
|
||||||
err := cl.SecretRemove(ctx, Secrets[name])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
} else {
|
} else {
|
||||||
logrus.Info("Secret: " + name + " removed")
|
logrus.Info(fmt.Sprintf("Secret: %s removed", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get volumes associated with the app
|
volumeListOKBody, err := cl.VolumeList(ctx, fs)
|
||||||
VolumeListOKBody, err := cl.VolumeList(ctx, fs)
|
volumeList := volumeListOKBody.Volumes
|
||||||
VolumeList := VolumeListOKBody.Volumes
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
Vols := []string{}
|
vols := []string{}
|
||||||
for _, vol := range VolumeList {
|
for _, vol := range volumeList {
|
||||||
Vols = append(Vols, vol.Name)
|
vols = append(vols, vol.Name)
|
||||||
}
|
}
|
||||||
// Remove the volumes if desired
|
|
||||||
if Volumes == true {
|
|
||||||
|
|
||||||
RemoveVols := []string{}
|
if Volumes {
|
||||||
// If there's no --force, ask if the user wants to remove
|
removeVols := []string{}
|
||||||
if Force {
|
if internal.Force {
|
||||||
RemoveVols = Vols
|
removeVols = vols
|
||||||
} else {
|
} else {
|
||||||
VolumesPrompt := &survey.MultiSelect{
|
volumesPrompt := &survey.MultiSelect{
|
||||||
Message: "Which volumes do you want to remove?",
|
Message: "Which volumes do you want to remove?",
|
||||||
Options: Vols,
|
Options: vols,
|
||||||
Default: Vols,
|
Default: vols,
|
||||||
}
|
}
|
||||||
survey.AskOne(VolumesPrompt, &RemoveVols)
|
survey.AskOne(volumesPrompt, &removeVols)
|
||||||
}
|
}
|
||||||
// Remove the volumes
|
for _, vol := range removeVols {
|
||||||
for _, vol := range RemoveVols {
|
err := cl.VolumeRemove(ctx, vol, internal.Force) // last argument is for force removing
|
||||||
err := cl.VolumeRemove(ctx, vol, Force) // false is for force removing
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
} else {
|
} else {
|
||||||
logrus.Info("Volume " + vol + " removed")
|
logrus.Info("Volume " + vol + " removed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logrus.Info("No volumes were removed. Volumes left: " + strings.Join(Vols, ", "))
|
logrus.Info("No volumes were removed. Volumes left: " + strings.Join(vols, ", "))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -42,3 +42,12 @@ var ContextFlag = &cli.StringFlag{
|
|||||||
Aliases: []string{"c"},
|
Aliases: []string{"c"},
|
||||||
Destination: &Context,
|
Destination: &Context,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var Force bool
|
||||||
|
|
||||||
|
var ForceFlag = &cli.BoolFlag{
|
||||||
|
Name: "force",
|
||||||
|
Value: false,
|
||||||
|
Aliases: []string{"f"},
|
||||||
|
Destination: &Force,
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user