Skip to content

Commit

Permalink
remove image & fix nil cursor on remove for image ciew
Browse files Browse the repository at this point in the history
  • Loading branch information
somnek committed Jan 18, 2024
1 parent a9f02ad commit 9e157f3
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 96 deletions.
39 changes: 25 additions & 14 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,32 @@ import (
docker "github.com/fsouza/go-dockerclient"
)

//
// TODO: handle errors return from docker client instead of assigning to _
//
////////////////////////////////////////////
// TODO: handle errors return from docker
// client instead of assigning to _
////////////////////////////////////////////

// ---------------- Image ----------------
func removeImage(c *docker.Client, id string) {
opts := docker.RemoveImageOptions{
Force: true,
}
// just tell em to remove the container that use this image first
_ = c.RemoveImageExtended(id, opts)
}

func listImages(c *docker.Client, showAll bool) []docker.APIImages {
opts := docker.ListImagesOptions{
All: showAll,
}
images, err := c.ListImages(opts)
if err != nil {
log.Fatal(err)
}
return images
}

// ---------------- Container ----------------
func removeContainer(c *docker.Client, id string) {
opts := docker.RemoveContainerOptions{
ID: id,
Expand Down Expand Up @@ -52,14 +74,3 @@ func listContainers(c *docker.Client, showAll bool) []docker.APIContainers {
}
return containers
}

func listImages(c *docker.Client, showAll bool) []docker.APIImages {
opts := docker.ListImagesOptions{
All: showAll,
}
images, err := c.ListImages(opts)
if err != nil {
log.Fatal(err)
}
return images
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/charmbracelet/lipgloss v0.9.1
github.com/fsouza/go-dockerclient v1.10.0
github.com/mattn/go-runewidth v0.0.15
github.com/muesli/reflow v0.3.0
)

require (
Expand All @@ -30,7 +31,6 @@ require (
github.com/morikuni/aec v1.0.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
Expand Down
24 changes: 16 additions & 8 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ import (
docker "github.com/fsouza/go-dockerclient"
)

type actionResult struct {
type actionResultContainers struct {
success []Container
failed []Container
}

type actionResultImages struct {
success []Image
failed []Image
associatedContainers []Container
}

const (
on int = iota
off
)

// checkProcess check if the container is in m.processes
// (a.k.a process is in progress)
func checkProcess(id string, processes map[string]string) bool {
if _, ok := processes[id]; ok {
return true
Expand Down Expand Up @@ -66,7 +74,7 @@ func unpauseAndWriteLog(m model) (tea.Model, tea.Cmd) {
}
}

res := actionResult{}
res := actionResultContainers{}
for _, c := range targets {
if c.state == "paused" {
go unpauseContainer(client, c.id)
Expand Down Expand Up @@ -113,7 +121,7 @@ func pauseAndWriteLog(m model) (tea.Model, tea.Cmd) {
}
}

res := actionResult{}
res := actionResultContainers{}
for _, c := range targets {
if c.state == "running" {
go pauseContainer(client, c.id)
Expand Down Expand Up @@ -160,7 +168,7 @@ func stopAndWriteLog(m model) (tea.Model, tea.Cmd) {
}
}

res := actionResult{}
res := actionResultContainers{}
for _, c := range targets {
if c.state == "running" || c.state == "restarting" {
go stopContainer(client, c.id)
Expand Down Expand Up @@ -207,7 +215,7 @@ func startAndWriteLog(m model) (tea.Model, tea.Cmd) {
}
}

res := actionResult{}
res := actionResultContainers{}
for _, c := range targets {
if c.state == "exited" || c.state == "created" {
go startContainer(client, c.id)
Expand Down Expand Up @@ -255,7 +263,7 @@ func removeAndWriteLog(m model) (tea.Model, tea.Cmd) {
}
}

res := actionResult{}
res := actionResultContainers{}
for _, c := range targets {
removeContainer(client, c.id)
desiredState := "x"
Expand Down Expand Up @@ -294,7 +302,7 @@ func restartAndWriteLog(m model) (tea.Model, tea.Cmd) {
}
}

res := actionResult{}
res := actionResultContainers{}
for _, c := range targets {
if c.state == "running" {
go restartContainer(client, c.id)
Expand Down Expand Up @@ -341,7 +349,7 @@ func killAndWriteLog(m model) (tea.Model, tea.Cmd) {
}
}

res := actionResult{}
res := actionResultContainers{}
for _, c := range targets {
if c.state == "running" {
go killContainer(client, c.id)
Expand Down
Loading

0 comments on commit 9e157f3

Please sign in to comment.