feat: add tag command

Signed-off-by: Jacob Lorenzen <jacob@lorenzen.me>
This commit is contained in:
Jacob Lorenzen 2024-06-23 14:06:35 +02:00
parent 06ab7b77b1
commit 34dc3e584d
No known key found for this signature in database
6 changed files with 48 additions and 3 deletions

View file

@ -2,6 +2,7 @@ package commands
import (
"context"
"fmt"
"strings"
"github.com/docker/docker/api/types/image"
@ -143,3 +144,8 @@ func (c *DockerCommand) PruneImages() error {
_, err := c.Client.ImagesPrune(context.Background(), filters.Args{})
return err
}
func (i *Image) String() string {
return fmt.Sprintf("%s:%s", i.Name, i.Tag)
}

View file

@ -62,13 +62,15 @@ func (gui *Gui) getConfirmationPanelDimensions(wrap bool, prompt string) (int, i
height/2 + panelHeight/2
}
func (gui *Gui) createPromptPanel(title string, handleConfirm func(*gocui.Gui, *gocui.View) error) error {
func (gui *Gui) createPromptPanel(title string, prompt string, handleConfirm func(*gocui.Gui, *gocui.View) error) error {
gui.onNewPopupPanel()
err := gui.prepareConfirmationPanel(title, "")
err := gui.prepareConfirmationPanel(title, prompt)
if err != nil {
return err
}
gui.Views.Confirmation.Editable = true
gui.Views.Confirmation.SetContent(prompt)
gui.Views.Confirmation.SetCursor(len(prompt), 0)
return gui.setKeyBindings(gui.g, handleConfirm, nil)
}

View file

@ -443,7 +443,7 @@ func (gui *Gui) openFile(filename string) error {
}
func (gui *Gui) handleCustomCommand(g *gocui.Gui, v *gocui.View) error {
return gui.createPromptPanel(gui.Tr.CustomCommandTitle, func(g *gocui.Gui, v *gocui.View) error {
return gui.createPromptPanel(gui.Tr.CustomCommandTitle, "", func(g *gocui.Gui, v *gocui.View) error {
command := gui.trimmedContent(v)
return gui.runSubprocess(gui.OSCommand.RunCustomCommand(command))
})

View file

@ -219,3 +219,29 @@ func (gui *Gui) handleImagesBulkCommand(g *gocui.Gui, v *gocui.View) error {
return gui.createBulkCommandMenu(bulkCommands, commandObject)
}
func (gui *Gui) handleImagesTagCommand(g *gocui.Gui, v *gocui.View) error {
image, err := gui.Panels.Images.GetSelectedItem()
if err != nil {
return nil
}
return gui.createPromptPanel(gui.Tr.NewTagCommand, image.String(), func(g *gocui.Gui, v *gocui.View) error {
newTag := gui.trimmedContent(v)
var cmd string
// if the user has provided a tag with a colon, we assume they've provided a full tag
if strings.Contains(newTag, ":") {
cmd = "docker tag " + image.String() + " " + newTag
} else {
cmd = "docker tag " + image.String() + " " + image.Name + ":" + newTag
}
err := gui.runSubprocess(gui.OSCommand.RunCustomCommand(cmd))
if err != nil {
return gui.createErrorPanel(err.Error())
}
return gui.reloadImages()
})
}

View file

@ -388,6 +388,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.handleImagesBulkCommand,
Description: gui.Tr.ViewBulkCommands,
},
{
ViewName: "images",
Key: 't',
Modifier: gocui.ModNone,
Handler: gui.handleImagesTagCommand,
Description: gui.Tr.ViewTagCommands,
},
{
ViewName: "volumes",
Key: 'c',

View file

@ -106,6 +106,8 @@ type TranslationSet struct {
ExecShell string
RunCustomCommand string
ViewBulkCommands string
ViewTagCommands string
NewTagCommand string
FilterList string
OpenInBrowser string
SortContainersByState string
@ -206,6 +208,8 @@ func englishSet() TranslationSet {
ExecShell: "exec shell",
RunCustomCommand: "run predefined custom command",
ViewBulkCommands: "view bulk commands",
ViewTagCommands: "view tag commands",
NewTagCommand: "new tag",
FilterList: "filter list",
OpenInBrowser: "open in browser (first port is http)",
SortContainersByState: "sort containers by state",