Skip to content

Commit

Permalink
sort volume
Browse files Browse the repository at this point in the history
  • Loading branch information
somnek committed Jan 29, 2024
1 parent 8ac3ccb commit 5298533
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
34 changes: 33 additions & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"sort"

tea "github.com/charmbracelet/bubbletea"
docker "github.com/fsouza/go-dockerclient"
Expand Down Expand Up @@ -420,19 +421,50 @@ func getImages() []Image {
}

// ---------------- Volume ----------------
func filterContainersByVolume(c *docker.Client, volName string) []docker.APIContainers {
opts := docker.ListContainersOptions{
All: true,
Filters: map[string][]string{
"volume": {volName},
},
}
// TODO: handle error
containers, _ := c.ListContainers(opts)
return containers
}

func getVolumes() []Volume {
client, err := docker.NewClientFromEnv()
if err != nil {
log.Fatalf("failed to create Docker clinet: %v", err)
}

volumes := []Volume{}

for _, v := range listVolumes(client) {
// find containers using the volume
containers := filterContainersByVolume(client, v.Name)

volume := Volume{
name: v.Name,
mountPoint: v.Mountpoint,
containers: containers,
createdAt: v.CreatedAt,
}
volumes = append(volumes, volume)

volumes = append([]Volume{volume}, volumes...)

}

// sort: show newest volume at the top
sort.Slice(volumes, func(i, j int) bool {
vi, vj := volumes[i], volumes[j]
// sort by volume name if vi & vj has the same createdAt
if vi.createdAt.Equal(vj.createdAt) {
return vi.name < vj.name
}
return volumes[i].createdAt.After(volumes[j].createdAt)
})

return volumes
}
5 changes: 2 additions & 3 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ type Container struct {
id string
ancestor string
desc string
cmd string
ip string
ports []docker.PortBinding
}

type Volume struct {
name string
mountPoint string
containers []docker.APIContainers
createdAt time.Time
}

type Image struct {
Expand Down
5 changes: 3 additions & 2 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var (

PortMapColStyle = lipgloss.NewStyle().Foreground(paletteA10)

inUseStyleTrue = lipgloss.NewStyle().Foreground(green)
inUseStyleFalse = lipgloss.NewStyle().Foreground(paletteA2)
inUseStyleTrue = lipgloss.NewStyle().Foreground(green)
inUseStyleFalse = lipgloss.NewStyle().Foreground(paletteA2)
volumeItemInUseStyle = lipgloss.NewStyle().Foreground(paletteA7)
)
4 changes: 4 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
images := getImages()
m.images = images

// volumes
volumes := getVolumes()
m.volumes = volumes

// cursor
if m.cursor == -1 {
m.cursor = 0
Expand Down
45 changes: 19 additions & 26 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,40 +116,28 @@ func formatVolumeName(name string) string {
return s
}

func buildVolumeDescShort(name string) string {
client, err := docker.NewClientFromEnv()
if err != nil {
log.Fatal(err)
}
volume, err := client.InspectVolume(name)
if err != nil {
log.Fatal(err)
}

// find containers that using this volume
containers, err := client.ListContainers(docker.ListContainersOptions{
All: true,
Filters: map[string][]string{"volume": {volume.Name}},
})

if err != nil {
log.Fatal(err)
}
func buildVolumeDescShort(volume Volume) string {
name := volume.name
createdAt := volume.createdAt
containers := volume.containers
mountPoint := volume.mountPoint

// get first element from result
container := "null"

// TODO: display list of containers, might have >1 container per volume
var inUse bool
containerName := "null"

if len(containers) > 0 {
inUse = true
container = containers[0].Names[0][1:]
containerName = containers[0].Names[0][1:]
}

var desc string
desc += fmt.Sprintf("Mount : %s\n", formatVolumeMountPoint(volume.Mountpoint))
desc += fmt.Sprintf("Name : %s\n", formatVolumeName(volume.Name))
desc += fmt.Sprintf("Created : %s\n", createdAt)
desc += fmt.Sprintf("Mount : %s\n", formatVolumeMountPoint(mountPoint))
desc += fmt.Sprintf("Name : %s\n", formatVolumeName(name))
desc += fmt.Sprintf("In Use : %v\n", formatVolumeInUse(inUse))
desc += fmt.Sprintf("Use by : %s\n", container)
desc += fmt.Sprintf("Use by : %s\n", containerName)

return desc
}
Expand All @@ -161,9 +149,14 @@ func buildVolumeView(m model) (string, string) {
check := " "
if m.cursor == i {
cursor = "❯"
bodyR = buildVolumeDescShort(choice.name)
bodyR = buildVolumeDescShort(choice)
}

name := runewidth.Truncate(choice.name, maxImageNameWidth, "")
if len(choice.containers) > 0 {
name = volumeItemInUseStyle.Render(name)
}

if _, ok := m.selected[i]; ok {
check = checkStyle.Render("✔")
}
Expand Down

0 comments on commit 5298533

Please sign in to comment.