0
0
forked from toolshed/abra

refactor: use app getting instead of boilerplate

This commit is contained in:
2021-09-05 23:17:35 +02:00
parent 48bcc9cb36
commit d4333c2dc0
18 changed files with 111 additions and 266 deletions

View File

@ -6,41 +6,24 @@ import (
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/config"
"github.com/AlecAivazis/survey/v2"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
// getAppServer retrieves the server of an app.
func getAppServer(appName string) string {
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
var server string
if app, ok := appFiles[appName]; ok {
server = app.Server
} else {
logrus.Fatalf(`app "%s" does not exist`, appName)
}
return server
}
var appVolumeListCommand = &cli.Command{
Name: "list",
Usage: "list volumes associated with an app",
Aliases: []string{"ls"},
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
app := internal.ValidateApp(c)
ctx := context.Background()
server := getAppServer(appName)
volumeList, err := client.GetVolumes(ctx, server, appName)
volumeList, err := client.GetVolumes(ctx, app.Server, app.Name)
if err != nil {
logrus.Fatal(err)
}
table := abraFormatter.CreateTable([]string{"DRIVER", "VOLUME NAME"})
var volTable [][]string
for _, volume := range volumeList {
@ -50,8 +33,10 @@ var appVolumeListCommand = &cli.Command{
}
volTable = append(volTable, volRow)
}
table.AppendBulk(volTable)
table.Render()
return nil
},
}
@ -64,10 +49,10 @@ var appVolumeRemoveCommand = &cli.Command{
internal.ForceFlag,
},
Action: func(c *cli.Context) error {
appName := internal.ValidateAppNameArg(c)
server := getAppServer(appName)
app := internal.ValidateApp(c)
ctx := context.Background()
volumeList, err := client.GetVolumes(ctx, server, appName)
volumeList, err := client.GetVolumes(ctx, app.Server, app.Name)
if err != nil {
logrus.Fatal(err)
}
@ -87,11 +72,13 @@ var appVolumeRemoveCommand = &cli.Command{
volumesToRemove = volumeNames
}
err = client.RemoveVolumes(ctx, server, volumesToRemove, internal.Force)
err = client.RemoveVolumes(ctx, app.Server, volumesToRemove, internal.Force)
if err != nil {
logrus.Fatal(err)
}
logrus.Info("Volumes removed successfully.")
return nil
},
}