abra/cli/app/volume.go

114 lines
2.6 KiB
Go

package app
import (
"context"
"errors"
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/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
} else {
logrus.Fatalf(`app "%s" does not exist`, appName)
}
return host
}
var appVolumeListCommand = &cli.Command{
Name: "list",
Usage: "list volumes associated with an app",
Aliases: []string{"ls"},
Action: func(c *cli.Context) error {
appName := c.Args().First()
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!"))
}
host := getAppsHost(appName)
ctx := context.Background()
volumeList, err := client.GetVolumes(ctx, host, appName)
if err != nil {
logrus.Fatal(err)
}
table := abraFormatter.CreateTable([]string{"DRIVER", "VOLUME NAME"})
var volTable [][]string
for _, volume := range volumeList {
volRow := []string{
volume.Driver,
volume.Name,
}
volTable = append(volTable, volRow)
}
table.AppendBulk(volTable)
table.Render()
return nil
},
}
var appVolumeRemoveCommand = &cli.Command{
Name: "remove",
Usage: "remove volume(s) associated with an app",
Aliases: []string{"rm", "delete"},
Flags: []cli.Flag{
internal.ForceFlag,
},
Action: func(c *cli.Context) error {
appName := c.Args().First()
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!"))
}
host := getAppsHost(appName)
ctx := context.Background()
volumeList, err := client.GetVolumes(ctx, host, appName)
if err != nil {
logrus.Fatal(err)
}
volumeNames := client.GetVolumeNames(volumeList)
var volumesToRemove []string
if !internal.Force {
volumesPrompt := &survey.MultiSelect{
Message: "Which volumes do you want to remove?",
Options: volumeNames,
Default: volumeNames,
}
if err := survey.AskOne(volumesPrompt, &volumesToRemove); err != nil {
logrus.Fatal(err)
}
} else {
volumesToRemove = volumeNames
}
err = client.RemoveVolumes(ctx, host, volumesToRemove, internal.Force)
if err != nil {
logrus.Fatal(err)
}
logrus.Info("Volumes removed successfully.")
return nil
},
}
var appVolumeCommand = &cli.Command{
Name: "volume",
Usage: "List or remove volumes associated with app",
ArgsUsage: "<command>",
Subcommands: []*cli.Command{
appVolumeListCommand,
appVolumeRemoveCommand,
},
}