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" ) // 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 := c.Args().First() if appName == "" { internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!")) } ctx := context.Background() server := getAppServer(appName) volumeList, err := client.GetVolumes(ctx, server, 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"}, 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!")) } server := getAppServer(appName) ctx := context.Background() volumeList, err := client.GetVolumes(ctx, server, 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, server, volumesToRemove, internal.Force) if err != nil { logrus.Fatal(err) } logrus.Info("Volumes removed successfully.") return nil }, } var appVolumeCommand = &cli.Command{ Name: "volume", Aliases: []string{"v"}, Usage: "Manage app volumes", ArgsUsage: "", Subcommands: []*cli.Command{ appVolumeListCommand, appVolumeRemoveCommand, }, }