2021-08-12 11:11:03 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2021-08-17 18:36:46 +00:00
|
|
|
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
2021-08-12 11:11:03 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2021-09-04 22:41:31 +00:00
|
|
|
// getAppServer retrieves the server of an app.
|
|
|
|
func getAppServer(appName string) string {
|
2021-08-17 18:36:46 +00:00
|
|
|
appFiles, err := config.LoadAppFiles("")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-04 22:41:31 +00:00
|
|
|
var server string
|
2021-08-17 18:36:46 +00:00
|
|
|
if app, ok := appFiles[appName]; ok {
|
2021-09-04 22:41:31 +00:00
|
|
|
server = app.Server
|
2021-08-17 18:36:46 +00:00
|
|
|
} else {
|
|
|
|
logrus.Fatalf(`app "%s" does not exist`, appName)
|
|
|
|
}
|
2021-09-04 22:41:31 +00:00
|
|
|
return server
|
2021-08-17 18:36:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 11:11:03 +00:00
|
|
|
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 == "" {
|
2021-08-17 18:36:46 +00:00
|
|
|
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!"))
|
2021-08-12 11:11:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 18:36:46 +00:00
|
|
|
ctx := context.Background()
|
2021-09-04 22:41:31 +00:00
|
|
|
server := getAppServer(appName)
|
|
|
|
volumeList, err := client.GetVolumes(ctx, server, appName)
|
2021-08-12 11:11:03 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-08-17 18:36:46 +00:00
|
|
|
table := abraFormatter.CreateTable([]string{"DRIVER", "VOLUME NAME"})
|
|
|
|
var volTable [][]string
|
2021-08-12 11:11:03 +00:00
|
|
|
for _, volume := range volumeList {
|
2021-08-17 18:36:46 +00:00
|
|
|
volRow := []string{
|
|
|
|
volume.Driver,
|
|
|
|
volume.Name,
|
|
|
|
}
|
|
|
|
volTable = append(volTable, volRow)
|
2021-08-12 11:11:03 +00:00
|
|
|
}
|
2021-08-17 18:36:46 +00:00
|
|
|
table.AppendBulk(volTable)
|
|
|
|
table.Render()
|
2021-08-12 11:11:03 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var appVolumeRemoveCommand = &cli.Command{
|
|
|
|
Name: "remove",
|
|
|
|
Usage: "remove volume(s) associated with an app",
|
2021-09-04 23:34:56 +00:00
|
|
|
Aliases: []string{"rm"},
|
2021-08-12 11:11:03 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
internal.ForceFlag,
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
appName := c.Args().First()
|
|
|
|
if appName == "" {
|
2021-08-17 18:36:46 +00:00
|
|
|
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!"))
|
2021-08-12 11:11:03 +00:00
|
|
|
}
|
2021-09-04 22:41:31 +00:00
|
|
|
server := getAppServer(appName)
|
2021-08-12 11:11:03 +00:00
|
|
|
ctx := context.Background()
|
2021-09-04 22:41:31 +00:00
|
|
|
volumeList, err := client.GetVolumes(ctx, server, appName)
|
2021-08-12 11:11:03 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-08-17 18:36:46 +00:00
|
|
|
volumeNames := client.GetVolumeNames(volumeList)
|
2021-08-12 11:11:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-08-17 18:36:46 +00:00
|
|
|
|
2021-09-04 22:41:31 +00:00
|
|
|
err = client.RemoveVolumes(ctx, server, volumesToRemove, internal.Force)
|
2021-08-17 18:36:46 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
2021-08-12 11:11:03 +00:00
|
|
|
}
|
2021-08-17 18:36:46 +00:00
|
|
|
logrus.Info("Volumes removed successfully.")
|
2021-08-12 11:11:03 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var appVolumeCommand = &cli.Command{
|
|
|
|
Name: "volume",
|
2021-09-04 23:34:56 +00:00
|
|
|
Aliases: []string{"v"},
|
|
|
|
Usage: "Manage app volumes",
|
2021-08-12 11:11:03 +00:00
|
|
|
ArgsUsage: "<command>",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
appVolumeListCommand,
|
|
|
|
appVolumeRemoveCommand,
|
|
|
|
},
|
|
|
|
}
|