forked from toolshed/abra
		
	refactor: create functions in client for removing and listing volumes
This commit is contained in:
		| @ -3,17 +3,31 @@ package app | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" |  | ||||||
|  |  | ||||||
|  | 	abraFormatter "coopcloud.tech/abra/cli/formatter" | ||||||
| 	"coopcloud.tech/abra/cli/internal" | 	"coopcloud.tech/abra/cli/internal" | ||||||
| 	"coopcloud.tech/abra/client" | 	"coopcloud.tech/abra/client" | ||||||
| 	"coopcloud.tech/abra/config" | 	"coopcloud.tech/abra/config" | ||||||
| 	"github.com/AlecAivazis/survey/v2" | 	"github.com/AlecAivazis/survey/v2" | ||||||
| 	"github.com/docker/docker/api/types/filters" |  | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| 	"github.com/urfave/cli/v2" | 	"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{ | var appVolumeListCommand = &cli.Command{ | ||||||
| 	Name:    "list", | 	Name:    "list", | ||||||
| 	Usage:   "list volumes associated with an app", | 	Usage:   "list volumes associated with an app", | ||||||
| @ -21,31 +35,26 @@ var appVolumeListCommand = &cli.Command{ | |||||||
| 	Action: func(c *cli.Context) error { | 	Action: func(c *cli.Context) error { | ||||||
| 		appName := c.Args().First() | 		appName := c.Args().First() | ||||||
| 		if appName == "" { | 		if appName == "" { | ||||||
| 			internal.ShowSubcommandHelpAndError(c, errors.New("No app name provided!")) | 			internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!")) | ||||||
| 		} | 		} | ||||||
| 		appFiles, err := config.LoadAppFiles("") | 		host := getAppsHost(appName) | ||||||
| 		if err != nil { |  | ||||||
| 			logrus.Fatal(err) |  | ||||||
| 		} |  | ||||||
| 		host := appFiles[appName].Server |  | ||||||
| 		ctx := context.Background() | 		ctx := context.Background() | ||||||
| 		cl, err := client.NewClientWithContext(host) | 		volumeList, err := client.GetVolumes(ctx, host, appName) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			logrus.Fatal(err) | 			logrus.Fatal(err) | ||||||
| 		} | 		} | ||||||
|  | 		table := abraFormatter.CreateTable([]string{"DRIVER", "VOLUME NAME"}) | ||||||
| 		fs := filters.NewArgs() | 		var volTable [][]string | ||||||
| 		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") |  | ||||||
| 		for _, volume := range volumeList { | 		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 | 		return nil | ||||||
| 	}, | 	}, | ||||||
| } | } | ||||||
| @ -60,31 +69,16 @@ var appVolumeRemoveCommand = &cli.Command{ | |||||||
| 	Action: func(c *cli.Context) error { | 	Action: func(c *cli.Context) error { | ||||||
| 		appName := c.Args().First() | 		appName := c.Args().First() | ||||||
| 		if appName == "" { | 		if appName == "" { | ||||||
| 			internal.ShowSubcommandHelpAndError(c, errors.New("No app name provided!")) | 			internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!")) | ||||||
| 		} | 		} | ||||||
| 		appFiles, err := config.LoadAppFiles("") | 		host := getAppsHost(appName) | ||||||
| 		if err != nil { |  | ||||||
| 			logrus.Fatal(err) |  | ||||||
| 		} |  | ||||||
| 		host := appFiles[appName].Server |  | ||||||
| 		ctx := context.Background() | 		ctx := context.Background() | ||||||
| 		cl, err := client.NewClientWithContext(host) | 		volumeList, err := client.GetVolumes(ctx, host, appName) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			logrus.Fatal(err) | 			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 | 		var volumesToRemove []string | ||||||
| 		if !internal.Force { | 		if !internal.Force { | ||||||
| 			volumesPrompt := &survey.MultiSelect{ | 			volumesPrompt := &survey.MultiSelect{ | ||||||
| @ -98,13 +92,12 @@ var appVolumeRemoveCommand = &cli.Command{ | |||||||
| 		} else { | 		} else { | ||||||
| 			volumesToRemove = volumeNames | 			volumesToRemove = volumeNames | ||||||
| 		} | 		} | ||||||
| 		for _, vol := range volumesToRemove { |  | ||||||
| 			err := cl.VolumeRemove(ctx, vol, internal.Force) | 		err = client.RemoveVolumes(ctx, host, volumesToRemove, internal.Force) | ||||||
| 			if err != nil { | 		if err != nil { | ||||||
| 				logrus.Fatal(err) | 			logrus.Fatal(err) | ||||||
| 			} |  | ||||||
| 			logrus.Info(fmt.Sprintf("Volume %s removed.", vol)) |  | ||||||
| 		} | 		} | ||||||
|  | 		logrus.Info("Volumes removed successfully.") | ||||||
| 		return nil | 		return nil | ||||||
| 	}, | 	}, | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										51
									
								
								client/volumes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								client/volumes.go
									
									
									
									
									
										Normal 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 | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user