Skip to content

Commit

Permalink
feature: remove dangling image
Browse files Browse the repository at this point in the history
  • Loading branch information
somnek committed Oct 29, 2024
1 parent 10d6846 commit 1da4364
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mock
bin/

.DS_Store
Expand Down
5 changes: 4 additions & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func removeImage(c *docker.Client, id string) {
Force: true,
}
// just tell em to remove the container that use this image first
_ = c.RemoveImageExtended(id, opts)
err := c.RemoveImageExtended(id, opts)
if err != nil {
log.Fatal(err)
}
}

func listImages(c *docker.Client, showAll bool) []docker.APIImages {
Expand Down
6 changes: 6 additions & 0 deletions keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type keyMap struct {
Toggle key.Binding

Remove key.Binding
Clean key.Binding
Restart key.Binding
Kill key.Binding
Stop key.Binding
Expand Down Expand Up @@ -57,6 +58,7 @@ func (k keyMap) FullHelp() [][]key.Binding {
k.Kill,
k.Stop,
k.Start,
k.Clean,
k.Page1,
k.Page2,
k.Page3,
Expand Down Expand Up @@ -114,6 +116,10 @@ var keys = keyMap{
key.WithKeys("X"),
key.WithHelp("shift+x", "remove"),
),
Clean: key.NewBinding(
key.WithKeys("C"),
key.WithHelp("shift+c", "clean"),
),
Restart: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "restart"),
Expand Down
47 changes: 47 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}

// findDangling return a list of Image has name "<none>"
func findDangling(images []Image) []Image {
danglingImages := []Image{}
for _, img := range images {
if img.name == "<none>" {
danglingImages = append(danglingImages, img)
}
}
return danglingImages
}

// getContainers return a list of Container that are created using
// this image
func (img Image) findAssociatedContainersInUse(m model) []Container {
Expand Down Expand Up @@ -147,6 +158,42 @@ func handleImageKeys(m model, msg tea.KeyMsg) (tea.Model, tea.Cmd) {
}

switch {
case key.Matches(msg, m.keys.Clean): // clean
client, err := docker.NewClientFromEnv()
if err != nil {
m.logs += "Failed to create Docker client"
}

res := actionResultImages{}

// filter dangling images
danglingImages := findDangling(m.images)
for _, img := range danglingImages {
go removeImage(client, img.id)
desiredState := "x"
addProcess(&m, img.id, desiredState)
res.success = append(res.success, img)
}

var logs string
successCount, failedCount := len(res.success), len(res.failed)

if successCount > 0 {
logs += fmt.Sprintf(
"🗑️ Remove %v image(s)\n",
itemCountStyle.Render(fmt.Sprintf("%d", successCount)))
}

if failedCount > 0 {
logs += fmt.Sprintf(
"🚧 Unable to remove %v image(s)\n",
itemCountStyle.Render(fmt.Sprintf("%d", failedCount)))
}

m.logs = logs
m.cursor = -1
return m, cmd

case key.Matches(msg, m.keys.Remove): // remove
client, err := docker.NewClientFromEnv()
if err != nil {
Expand Down

0 comments on commit 1da4364

Please sign in to comment.