Skip to content

Commit

Permalink
Show contianer using by volume on detail
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestopigma committed Jul 4, 2023
1 parent aa3428b commit 8ed18b2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 45 deletions.
56 changes: 47 additions & 9 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Docker struct {
Containers []MyContainer
Images []MyImage
Networks []MyNetwork
Volumes []*volume.Volume
Volumes []MyVolume
}

type MyNetwork struct {
Expand All @@ -47,7 +47,10 @@ type MyContainer struct {
Size string
Command string
Env []string
ReadOnly bool
MountedAt string
Network MyNetwork
Mounts []types.MountPoint
}

type MyImage struct {
Expand All @@ -56,6 +59,11 @@ type MyImage struct {
History []image.HistoryResponseItem
}

type MyVolume struct {
Volume *volume.Volume
Containers []MyContainer
}

func (i *MyImage) GetFormatTimestamp() string {
if i.Summary.Created == 0 {
return ""
Expand Down Expand Up @@ -159,6 +167,14 @@ func (d *Docker) ContainerList() ([]MyContainer, error) {
gateway = networkSettings.Networks[networkMode].Gateway
}

readOnly := false
mountedAt := ""

if len(c.Mounts) > 0 {
readOnly = c.Mounts[0].RW == false
mountedAt = c.Mounts[0].Destination
}

mc = append(mc, MyContainer{
ID: c.ID,
IDShort: utils.TrimValue(c.ID, 10),
Expand All @@ -172,11 +188,14 @@ func (d *Docker) ContainerList() ([]MyContainer, error) {
Size: formatSize(*cJSON.SizeRootFs),
Env: cJSON.Config.Env,
Command: strings.Join(cJSON.Config.Entrypoint, " ") + " " + strings.Join(cJSON.Config.Cmd, " "),
ReadOnly: readOnly,
MountedAt: mountedAt,
Network: MyNetwork{
Name: networkMode,
IPAddress: ipAddress,
Gateway: gateway,
},
Mounts: c.Mounts,
})

d.Containers = mc
Expand Down Expand Up @@ -394,25 +413,44 @@ func (d *Docker) GetNetworkByName(name string) (MyNetwork, error) {
return MyNetwork{}, fmt.Errorf("network %s not found", name)
}

func (d *Docker) VolumeList() ([]*volume.Volume, error) {
func (d *Docker) VolumeList() ([]MyVolume, error) {
options := volume.ListOptions{}
volumes, err := d.cli.VolumeList(d.ctx, options)
vl, err := d.cli.VolumeList(d.ctx, options)
if err != nil {
return []*volume.Volume{}, err
return []MyVolume{}, err
}

mvol := []MyVolume{}
for _, v := range vl.Volumes {
name := v.Name

containers := []MyContainer{}
for _, c := range d.Containers {
for _, mount := range c.Mounts {
if mount.Type == "volume" && mount.Name == name {
containers = append(containers, c)
}
}
}

mvol = append(mvol, MyVolume{
Volume: v,
Containers: containers,
})
}

d.Volumes = volumes.Volumes
return volumes.Volumes, nil
d.Volumes = mvol
return mvol, nil
}

func (d *Docker) GetVolumeByName(name string) (*volume.Volume, error) {
func (d *Docker) GetVolumeByName(name string) (MyVolume, error) {
for _, v := range d.Volumes {
if v.Name == name {
if v.Volume.Name == name {
return v, nil
}
}

return &volume.Volume{}, fmt.Errorf("volume %s not found", name)
return MyVolume{}, fmt.Errorf("volume %s not found", name)
}

func (d *Docker) Events() {
Expand Down
5 changes: 5 additions & 0 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.ClearScreen
}

if m.currentModel == MVolumeDetail || m.currentModel == MVolumeSearch {
m.currentModel = MVolumeList
return m, tea.ClearScreen
}

case "ctrl+c":
return m, tea.Quit
case "down":
Expand Down
51 changes: 24 additions & 27 deletions models/volume_detail.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package models

import (
"dockerniceui/docker"
"dockerniceui/utils"
"fmt"

"github.com/charmbracelet/bubbles/viewport"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
"github.com/docker/docker/api/types/volume"
)

func NewVolumeDetail(volume *volume.Volume, createTable utils.CreateTableFunc) (viewport.Model, error) {
func NewVolumeDetail(volume docker.MyVolume, createTable utils.CreateTableFunc) (viewport.Model, error) {
content := getContentVolume(volume)
const width = 120

Expand Down Expand Up @@ -38,35 +38,32 @@ func NewVolumeDetail(volume *volume.Volume, createTable utils.CreateTableFunc) (
return vp, nil
}

func getContentVolume(volume *volume.Volume) string {
// content := `
// # Volume detail

// | Type | Value |
// | ---- | ----- |
// | ID | 1234567890 |
// | Created | my-network |
// | Mount path | bridge |
// | Driver | local |
// | Labels | false

// # Containers using this volume

// | Name | Mounted at | Read -only
// | -- | ---- | ------------ | ------------ |
// | 1234567890 | my-container |
// | 1234567890 | my-container |

//`
func getContentVolume(v docker.MyVolume) string {
response := ""
response += utils.CreateTable("# Volume detail", []string{"Type", "Value"},
[][]string{
{"ID", volume.Name},
{"Created", volume.CreatedAt},
{"Mount path", volume.Mountpoint},
{"Driver", volume.Driver},
{"Labels", fmt.Sprintf("%v", volume.Labels)},
{"ID", v.Volume.Name},
{"Created", v.Volume.CreatedAt},
{"Mount path", v.Volume.Mountpoint},
{"Driver", v.Volume.Driver},
{"Labels", fmt.Sprintf("%v", v.Volume.Labels)},
})

response += "\n\n"

if len(v.Containers) > 0 {
rows := [][]string{}
for _, c := range v.Containers {
ro := "false"
if c.ReadOnly {
ro = "true"
}
rows = append(rows, []string{c.Name, c.MountedAt, ro})
}

response += utils.CreateTable("# Containers using this volume", []string{"Name", "Mounted at", "Read -only"}, rows)

}

return response
}
18 changes: 9 additions & 9 deletions models/volume_list.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package models

import (
"dockerniceui/docker"
"dockerniceui/utils"
"fmt"
"strings"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/docker/docker/api/types/volume"
)

type VolumeList struct {
table table.Model
}

func NewVolumeList(volumeList []*volume.Volume, query string) VolumeList {
func NewVolumeList(volumeList []docker.MyVolume, query string) VolumeList {
columns := []table.Column{
{Title: "Name", Width: 20},
{Title: "Stack", Width: 10},
Expand Down Expand Up @@ -84,13 +84,13 @@ func (vl VolumeList) Update(msg tea.Msg, m *model) (table.Model, tea.Cmd) {
return vl.table, nil
}

func GetVolumeRows(volumeList []*volume.Volume, query string) []table.Row {
var filtered []*volume.Volume
func GetVolumeRows(volumeList []docker.MyVolume, query string) []table.Row {
var filtered []docker.MyVolume
if query == "" {
filtered = volumeList
} else {
for _, v := range volumeList {
if strings.Contains(strings.ToLower(v.Name), strings.ToLower(query)) {
if strings.Contains(strings.ToLower(v.Volume.Name), strings.ToLower(query)) {
filtered = append(filtered, v)
}
}
Expand All @@ -100,11 +100,11 @@ func GetVolumeRows(volumeList []*volume.Volume, query string) []table.Row {
for _, v := range filtered {

rows = append(rows, table.Row{
v.Name,
v.Volume.Name,
"",
v.Driver,
v.Mountpoint,
v.CreatedAt,
v.Volume.Driver,
v.Volume.Mountpoint,
v.Volume.CreatedAt,
})
}

Expand Down

0 comments on commit 8ed18b2

Please sign in to comment.