Add abra app remove command #43

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

View File

@ -1,7 +1,16 @@
package app
import (
"context"
"errors"
"fmt"
"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/urfave/cli/v2"
)
@ -16,9 +25,56 @@ var VolumesFlag = &cli.BoolFlag{
}
var appRemoveCommand = &cli.Command{
Name: "remove",
Name: "remove",
Aliases: []string{"rm", "delete"},
Flags: []cli.Flag{
VolumesFlag,
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)?
internal.SecretsFlag,
},
Action: func(c *cli.Context) error {
// Check if app name was provided by user
AppName := c.Args().First()
if AppName == "" {
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?",
}
survey.AskOne(prompt, &response)
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.
if response == false {
knoflook marked this conversation as resolved Outdated

appName (e.g. host)

`appName` (e.g. [host](https://git.coopcloud.tech/coop-cloud/go-abra/src/branch/main/cli/recipe/recipe.go#L189))
return errors.New("User aborted app removal")
}
// Remove the file
AppFiles, err := config.LoadAppFiles("")
if err != nil {
return err
}
AppPath := AppFiles[AppName].Path
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)`
host := AppFiles[AppName].Server
// Initialize docker client
ctx := context.Background()
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")) } ```
cl, err := client.NewClientWithContext(host)
if err != nil {
return err
}
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[]
fmt.Println(AppPath) //to be replaced by a remove method
knoflook marked this conversation as resolved Outdated

logrus.Fatal(err)

`logrus.Fatal(err)`
// Check if the app has secrets and remove them too
if internal.Secrets {
knoflook marked this conversation as resolved Outdated

appPath := AppFiles[appName].Path

`appPath := AppFiles[appName].Path`
fs := filters.NewArgs()
fs.Add("name", AppName)
SecretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: fs})
if err != nil {
return err
}
fmt.Println(SecretList)
}
// Remove the volumes if desired
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 nil
},
}