feat: initial commit for abra app volume ls/rm #51

Merged
roxxers merged 2 commits from knoflook/abra:secret-create into main 2021-08-18 18:29:14 +00:00
2 changed files with 89 additions and 45 deletions
Showing only changes of commit c3088a5158 - Show all commits

View File

@ -3,17 +3,31 @@ package app
import (
"context"
"errors"
"fmt"
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/client"
"coopcloud.tech/abra/config"
"github.com/AlecAivazis/survey/v2"
"github.com/docker/docker/api/types/filters"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
func getAppsHost(appName string) string {
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
var host string
if app, ok := appFiles[appName]; ok {
host = app.Server
roxxers marked this conversation as resolved Outdated

Style: Errors should not start with capital. https://staticcheck.io/docs/checks#ST1005

Style: Errors should not start with capital. https://staticcheck.io/docs/checks#ST1005

I am also unsure of using this error vs just printing the help page without the error. Since not giving the first arg seems to do so without error for other commands. This is more a thing for future clean up tbf and me rambling.

I am also unsure of using this error vs just printing the help page without the error. Since not giving the first arg seems to do so without error for other commands. This is more a thing for future clean up tbf and me rambling.
} else {
logrus.Fatalf(`app "%s" does not exist`, appName)
}
return host
}
knoflook marked this conversation as resolved Outdated

As discussed with my panic hour, this just isn't checking if the app itself exists leading to a weird error that is very confusing. Like "Context '' does not exist."

var host string
if app, ok := appFiles[appName]; ok {
	host = app.Server
}
if host == "" {
	logrus.Fatalf(`app "%s" does not exist`, appName)
}

Just tested this and it works great with this addition.

As discussed with my panic hour, this just isn't checking if the app itself exists leading to a weird error that is very confusing. Like "Context '' does not exist." ```go var host string if app, ok := appFiles[appName]; ok { host = app.Server } if host == "" { logrus.Fatalf(`app "%s" does not exist`, appName) } ``` Just tested this and it works great with this addition.

Just realised this should be simplified to

var host string
if app, ok := appFiles[appName]; ok {
	host = app.Server
} else {
	logrus.Fatalf(`app "%s" does not exist`, appName)
}
Just realised this should be simplified to ```go var host string if app, ok := appFiles[appName]; ok { host = app.Server } else { logrus.Fatalf(`app "%s" does not exist`, appName) } ```
var appVolumeListCommand = &cli.Command{
Name: "list",
Usage: "list volumes associated with an app",
@ -21,31 +35,26 @@ var appVolumeListCommand = &cli.Command{
Action: func(c *cli.Context) error {
appName := c.Args().First()
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("No app name provided!"))
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
host := appFiles[appName].Server
host := getAppsHost(appName)
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
volumeList, err := client.GetVolumes(ctx, host, appName)
if err != nil {
logrus.Fatal(err)
knoflook marked this conversation as resolved Outdated

Minimal: We use table creation in the formatter for the abra/cli package

Minimal: We use table creation in the formatter for the abra/cli package
}
fs := filters.NewArgs()
fs.Add("name", appName)
volumeListOKBody, err := cl.VolumeList(ctx, fs)
volumeList := volumeListOKBody.Volumes
if err != nil {
logrus.Fatal(err)
}
fmt.Println("DRIVER\t\t\tVOLUME NAME")
table := abraFormatter.CreateTable([]string{"DRIVER", "VOLUME NAME"})
var volTable [][]string
for _, volume := range volumeList {
fmt.Printf("%s\t\t\t%s\n", volume.Driver, volume.Name)
volRow := []string{
volume.Driver,
volume.Name,
}
volTable = append(volTable, volRow)
}
table.AppendBulk(volTable)
table.Render()
return nil
},
}
@ -60,31 +69,16 @@ var appVolumeRemoveCommand = &cli.Command{
Action: func(c *cli.Context) error {
knoflook marked this conversation as resolved Outdated

Same issue as before. Since you are doing this twice you could refactor some of the calls here into their own functions so both commands can use them?

Same issue as before. Since you are doing this twice you could refactor some of the calls here into their own functions so both commands can use them?
appName := c.Args().First()
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("No app name provided!"))
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
host := appFiles[appName].Server
host := getAppsHost(appName)
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
volumeList, err := client.GetVolumes(ctx, host, appName)
if err != nil {
logrus.Fatal(err)
}
volumeNames := client.GetVolumeNames(volumeList)
fs := filters.NewArgs()
fs.Add("name", appName)
volumeListOKBody, err := cl.VolumeList(ctx, fs)
volumeList := volumeListOKBody.Volumes
if err != nil {
logrus.Fatal(err)
}
var volumeNames []string
for _, volume := range volumeList {
volumeNames = append(volumeNames, volume.Name)
}
var volumesToRemove []string
if !internal.Force {
volumesPrompt := &survey.MultiSelect{
@ -98,13 +92,12 @@ var appVolumeRemoveCommand = &cli.Command{
} else {
volumesToRemove = volumeNames
}
for _, vol := range volumesToRemove {
err := cl.VolumeRemove(ctx, vol, internal.Force)
if err != nil {
logrus.Fatal(err)
}
logrus.Info(fmt.Sprintf("Volume %s removed.", vol))
err = client.RemoveVolumes(ctx, host, volumesToRemove, internal.Force)
if err != nil {
logrus.Fatal(err)
}
logrus.Info("Volumes removed successfully.")
return nil
},
}

51
client/volumes.go Normal file
View File

@ -0,0 +1,51 @@
package client
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/sirupsen/logrus"
)
func GetVolumes(ctx context.Context, server string, appName string) ([]*types.Volume, error) {
cl, err := NewClientWithContext(server)
if err != nil {
return nil, err
}
fs := filters.NewArgs()
fs.Add("name", appName)
volumeListOKBody, err := cl.VolumeList(ctx, fs)
volumeList := volumeListOKBody.Volumes
if err != nil {
logrus.Fatal(err)
}
return volumeList, nil
}
func GetVolumeNames(volumes []*types.Volume) []string {
var volumeNames []string
for _, vol := range volumes {
volumeNames = append(volumeNames, vol.Name)
}
return volumeNames
}
func RemoveVolumes(ctx context.Context, server string, volumeNames []string, force bool) error {
cl, err := NewClientWithContext(server)
if err != nil {
return err
}
for _, volName := range volumeNames {
err := cl.VolumeRemove(ctx, volName, force)
if err != nil {
return err
}
}
return nil
}