forked from toolshed/abra
refactor!: cobra migrate
This commit is contained in:
@ -1,62 +1,41 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/pkg/autocomplete"
|
||||
"coopcloud.tech/abra/pkg/client"
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/urfave/cli/v3"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var allFilter bool
|
||||
var ServerPruneCommand = &cobra.Command{
|
||||
Use: "prune <server> [flags]",
|
||||
Aliases: []string{"p"},
|
||||
Short: "Prune resources on a server",
|
||||
Long: `Prunes unused containers, networks, and dangling images.
|
||||
|
||||
var allFilterFlag = &cli.BoolFlag{
|
||||
Name: "all",
|
||||
Aliases: []string{"a"},
|
||||
Usage: "Remove all unused images not just dangling ones",
|
||||
Destination: &allFilter,
|
||||
}
|
||||
|
||||
var volumesFilter bool
|
||||
|
||||
var volumesFilterFlag = &cli.BoolFlag{
|
||||
Name: "volumes",
|
||||
Aliases: []string{"v"},
|
||||
Usage: "Prune volumes. This will remove app data, Be Careful!",
|
||||
Destination: &volumesFilter,
|
||||
}
|
||||
|
||||
var serverPruneCommand = cli.Command{
|
||||
Name: "prune",
|
||||
Aliases: []string{"p"},
|
||||
Usage: "Prune resources on a server",
|
||||
UsageText: "abra server prune <server> [options]",
|
||||
Description: `Prunes unused containers, networks, and dangling images.
|
||||
|
||||
Use "-v/--volumes" to remove volumes that are not associated with a deployed
|
||||
Use "--volumes/-v" to remove volumes that are not associated with a deployed
|
||||
app. This can result in unwanted data loss if not used carefully.`,
|
||||
Flags: []cli.Flag{
|
||||
allFilterFlag,
|
||||
volumesFilterFlag,
|
||||
Args: cobra.ExactArgs(1),
|
||||
ValidArgsFunction: func(
|
||||
cmd *cobra.Command,
|
||||
args []string,
|
||||
toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
return autocomplete.ServerNameComplete()
|
||||
},
|
||||
Before: internal.SubCommandBefore,
|
||||
ShellComplete: autocomplete.ServerNameComplete,
|
||||
HideHelp: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
serverName := internal.ValidateServer(cmd)
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
serverName := internal.ValidateServer(args)
|
||||
|
||||
cl, err := client.New(serverName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var args filters.Args
|
||||
var filterArgs filters.Args
|
||||
|
||||
cr, err := cl.ContainersPrune(ctx, args)
|
||||
cr, err := cl.ContainersPrune(cmd.Context(), filterArgs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -64,7 +43,7 @@ app. This can result in unwanted data loss if not used carefully.`,
|
||||
cntSpaceReclaimed := formatter.ByteCountSI(cr.SpaceReclaimed)
|
||||
log.Infof("containers pruned: %d; space reclaimed: %s", len(cr.ContainersDeleted), cntSpaceReclaimed)
|
||||
|
||||
nr, err := cl.NetworksPrune(ctx, args)
|
||||
nr, err := cl.NetworksPrune(cmd.Context(), filterArgs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -77,7 +56,7 @@ app. This can result in unwanted data loss if not used carefully.`,
|
||||
pruneFilters.Add("dangling", "false")
|
||||
}
|
||||
|
||||
ir, err := cl.ImagesPrune(ctx, pruneFilters)
|
||||
ir, err := cl.ImagesPrune(cmd.Context(), pruneFilters)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -86,7 +65,7 @@ app. This can result in unwanted data loss if not used carefully.`,
|
||||
log.Infof("images pruned: %d; space reclaimed: %s", len(ir.ImagesDeleted), imgSpaceReclaimed)
|
||||
|
||||
if volumesFilter {
|
||||
vr, err := cl.VolumesPrune(ctx, args)
|
||||
vr, err := cl.VolumesPrune(cmd.Context(), filterArgs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -95,6 +74,29 @@ app. This can result in unwanted data loss if not used carefully.`,
|
||||
log.Infof("volumes pruned: %d; space reclaimed: %s", len(vr.VolumesDeleted), volSpaceReclaimed)
|
||||
}
|
||||
|
||||
return nil
|
||||
return
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
allFilter bool
|
||||
volumesFilter bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
ServerPruneCommand.Flags().BoolVarP(
|
||||
&allFilter,
|
||||
"all",
|
||||
"a",
|
||||
false,
|
||||
"remove all unused images",
|
||||
)
|
||||
|
||||
ServerPruneCommand.Flags().BoolVarP(
|
||||
&volumesFilter,
|
||||
"volumes",
|
||||
"v",
|
||||
false,
|
||||
"remove volumes",
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user