Add abra app remove command #43

Closed
knoflook wants to merge 7 commits from knoflook:dev into main
Showing only changes of commit 7dad9895eb - Show all commits

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{
knoflook marked this conversation as resolved Outdated

This is a general flag we'll add to other sub-commands, it can live in cli/internal/common.go?

This is a general flag we'll add to other sub-commands, it can live in [cli/internal/common.go](https://git.coopcloud.tech/coop-cloud/go-abra/src/branch/main/cli/internal/common.go)?
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
knoflook marked this conversation as resolved Outdated

We haven't discussed this but the existing convention that I see is that we don't add comments to code to explain what it is doing unless it is doing something strange or difficult. Otherwise, we comment to explain why we're doing something (context, not what it is doing).

The issue is, that when the code changes, you might forget to change the comment and then the next person might be caught reading the comment which describes code that doesn'te exist anymore. I would say have a look around the rest of the codebase at comment usage and perhaps remove the obvious ones in this function.

For example the // we have to map the names to ID's comment is a good one, I'd keep that.

We haven't discussed this but the existing convention that I see is that we don't add comments to code to explain what it is doing unless it is doing something strange or difficult. Otherwise, we comment to explain why we're doing something (context, not what it is doing). The issue is, that when the code changes, you might forget to change the comment and then the next person might be caught reading the comment which describes code that doesn'te exist anymore. I would say have a look around the rest of the codebase at comment usage and perhaps remove the obvious ones in this function. For example the `// we have to map the names to ID's` comment is a good one, I'd keep that.
@ -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?",
knoflook marked this conversation as resolved Outdated

Message: fmt.Sprintf("About to delete %s, are you sure?", appName)

`Message: fmt.Sprintf("About to delete %s, are you sure?", appName)`
}
survey.AskOne(prompt, &response)
if response == false {
knoflook marked this conversation as resolved Outdated
if !response {
  logrus.Fatal(errors.New("User aborted app removal"))
}
```golang if !response { logrus.Fatal(errors.New("User aborted app removal")) } ```
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("")
decentral1se marked this conversation as resolved Outdated

appFiles, err := config.LoadAppFiles()

`appFiles, err := config.LoadAppFiles()`

Can't do it that way, if you remove "" LoadAppFiles returns an empty map[]

Can't do it that way, if you remove "" LoadAppFiles returns an empty map[]
if err != nil {
return err
knoflook marked this conversation as resolved Outdated

logrus.Fatal(err)

`logrus.Fatal(err)`
@ -59,68 +71,88 @@ var appRemoveCommand = &cli.Command{
if err != nil {
return err
}
// Remove the file
err = os.Remove(AppPath)
if err != nil {
knoflook marked this conversation as resolved
Review
if err != nil {
	logrus.Fatal(err)
}
logrus.Info(fmt.Sprintf("File: %s removed", appPath))
```golang if err != nil { logrus.Fatal(err) } logrus.Info(fmt.Sprintf("File: %s removed", appPath)) ```
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
knoflook marked this conversation as resolved Outdated

logrus.Fatal(err)

`logrus.Fatal(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])
knoflook marked this conversation as resolved Outdated

Let's remove that 🙃

Let's remove that 🙃
err := cl.SecretRemove(ctx, Secrets[name])
if err != nil {
knoflook marked this conversation as resolved Outdated
if err != nil {
	logrus.Fatal(err)
}
logrus.Info(fmt.Sprintf("Secret: %s removed", name))
```golang if err != nil { logrus.Fatal(err) } logrus.Info(fmt.Sprintf("Secret: %s removed", name)) ```
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
knoflook marked this conversation as resolved
Review

logrus.Fatal(err)

`logrus.Fatal(err)`
}
Vols := []string{}
for _, vol := range VolumeList {
Vols = append(Vols, vol.Name)
}
// Remove the volumes if desired
if Volumes == true {
knoflook marked this conversation as resolved Outdated

if Volumes {

`if Volumes {`
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