-
-
Notifications
You must be signed in to change notification settings - Fork 165
feat: uc image rm #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
miekg
wants to merge
4
commits into
psviderski:main
Choose a base branch
from
miekg:miek/26/mei06wo/prune
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: uc image rm #357
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package image | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/psviderski/uncloud/internal/cli" | ||
| "github.com/psviderski/uncloud/internal/cli/completion" | ||
| "github.com/psviderski/uncloud/internal/cli/tui" | ||
| "github.com/psviderski/uncloud/pkg/api" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type removeOptions struct { | ||
| force bool | ||
| machines []string | ||
| yes bool | ||
| } | ||
|
|
||
| func NewRemoveCommand() *cobra.Command { | ||
| opts := removeOptions{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "rm IMAGE [IMAGE...]", | ||
| Aliases: []string{"remove", "delete"}, | ||
| Short: "Remove one or more images.", | ||
| Long: "Remove one or more images. You cannot remove an image that is in use by a container.", | ||
| Args: cobra.MinimumNArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| uncli := cmd.Context().Value("cli").(*cli.CLI) | ||
| return remove(cmd.Context(), uncli, args, opts) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVarP(&opts.force, "force", "f", false, | ||
| "Force the removal of one or more images.") | ||
| cmd.Flags().StringSliceVarP(&opts.machines, "machine", "m", nil, | ||
| "Name or ID of the machine to remove one or more images from. "+ | ||
| "Can be specified multiple times or as a comma-separated list.\n"+ | ||
| "If not specified, the found images(s) will be removed from all machines.") | ||
| cmd.Flags().BoolVarP(&opts.yes, "yes", "y", false, | ||
| "Do not prompt for confirmation before removing the image(s).") | ||
|
|
||
| completion.MachinesFlag(cmd) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func remove(ctx context.Context, uncli *cli.CLI, names []string, opts removeOptions) error { | ||
| client, err := uncli.ConnectCluster(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("connect to cluster: %w", err) | ||
| } | ||
| defer client.Close() | ||
|
|
||
| filter := api.ImageFilter{} | ||
|
|
||
| if len(opts.machines) > 0 { | ||
| machines := cli.ExpandCommaSeparatedValues(opts.machines) | ||
| filter.Machines = machines | ||
| } | ||
|
|
||
| images := []api.MachineImages{} | ||
| for _, name := range names { | ||
| filter.Name = name | ||
| clusterImages, err := client.ListImages(ctx, filter) | ||
| if err != nil { | ||
| return fmt.Errorf("list images: %w", err) | ||
| } | ||
| for i := range clusterImages { | ||
| if len(clusterImages[i].Images) > 0 { | ||
| images = append(images, clusterImages[i]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(images) == 0 { | ||
| return fmt.Errorf("no images found matching the specified names") | ||
| } | ||
|
|
||
| allMachines, err := client.ListMachines(ctx, nil) | ||
| if err != nil { | ||
| return fmt.Errorf("list machines: %w", err) | ||
| } | ||
|
|
||
| // Confirm removal if not using --yes flag. | ||
| if !opts.yes { | ||
| fmt.Println("The following images will be removed:") | ||
| for _, machineImage := range images { | ||
| // Get machine name for better readability. | ||
| machineName := machineImage.Metadata.Machine | ||
| if m := allMachines.FindByNameOrID(machineName); m != nil { | ||
| machineName = m.Machine.Name | ||
| } | ||
|
|
||
| for _, img := range machineImage.Images { | ||
| id := strings.TrimPrefix(img.ID, "sha256:")[:12] | ||
| name := "<none>" | ||
| if len(img.RepoTags) > 0 && img.RepoTags[0] != "<none>:<none>" { | ||
| name = img.RepoTags[0] | ||
| } | ||
|
|
||
| fmt.Printf(" • '%s' ('%s') on machine '%s'\n", name, id, machineName) | ||
| } | ||
| } | ||
|
|
||
| fmt.Println() | ||
| confirmed, err := tui.Confirm("") | ||
| if err != nil { | ||
| return fmt.Errorf("confirm removal: %w", err) | ||
| } | ||
| if !confirmed { | ||
| return cli.Cancelled("Cancelled. No images were removed.") | ||
| } | ||
| } | ||
|
|
||
| var removeErr error | ||
| for _, machineImage := range images { | ||
| machineName := machineImage.Metadata.Machine | ||
| if m := allMachines.FindByNameOrID(machineName); m != nil { | ||
| machineName = m.Machine.Name | ||
| } | ||
|
|
||
| for _, img := range machineImage.Images { | ||
| id := strings.TrimPrefix(img.ID, "sha256:")[:12] | ||
| name := "<none>" | ||
| if len(img.RepoTags) > 0 && img.RepoTags[0] != "<none>:<none>" { | ||
| name = img.RepoTags[0] | ||
| } | ||
|
|
||
| if err := client.RemoveImage(ctx, machineImage.Metadata.Machine, img.ID, opts.force); err != nil { | ||
| if !errors.Is(err, api.ErrNotFound) { | ||
| removeErr = errors.Join(removeErr, fmt.Errorf("failed to remove image '%s' ('%s') on machine '%s': %w", | ||
| name, id, machineName, err)) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| fmt.Printf("Image '%s' ('%s') removed from machine '%s'.\n", name, id, machineName) | ||
| } | ||
| } | ||
|
|
||
| return removeErr | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.