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 ( import (
"context" "context"
"fmt"
"strings" "strings"
"github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/image"
@ -143,3 +144,8 @@ func (c *DockerCommand) PruneImages() error {
_, err := c.Client.ImagesPrune(context.Background(), filters.Args{}) _, err := c.Client.ImagesPrune(context.Background(), filters.Args{})
return err 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 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() gui.onNewPopupPanel()
err := gui.prepareConfirmationPanel(title, "") err := gui.prepareConfirmationPanel(title, prompt)
if err != nil { if err != nil {
return err return err
} }
gui.Views.Confirmation.Editable = true gui.Views.Confirmation.Editable = true
gui.Views.Confirmation.SetContent(prompt)
gui.Views.Confirmation.SetCursor(len(prompt), 0)
return gui.setKeyBindings(gui.g, handleConfirm, nil) 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 { 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) command := gui.trimmedContent(v)
return gui.runSubprocess(gui.OSCommand.RunCustomCommand(command)) 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) 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, Handler: gui.handleImagesBulkCommand,
Description: gui.Tr.ViewBulkCommands, Description: gui.Tr.ViewBulkCommands,
}, },
{
ViewName: "images",
Key: 't',
Modifier: gocui.ModNone,
Handler: gui.handleImagesTagCommand,
Description: gui.Tr.ViewTagCommands,
},
{ {
ViewName: "volumes", ViewName: "volumes",
Key: 'c', Key: 'c',

View file

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