Skip to content

Commit

Permalink
WIP - detail volume
Browse files Browse the repository at this point in the history
  • Loading branch information
ernesto27 committed Jul 3, 2023
1 parent 30ebc21 commit 3ef3ff1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 25 deletions.
11 changes: 11 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Docker struct {
Containers []MyContainer
Images []MyImage
Networks []MyNetwork
Volumes []*volume.Volume
}

type MyNetwork struct {
Expand Down Expand Up @@ -400,8 +401,18 @@ func (d *Docker) VolumeList() ([]*volume.Volume, error) {
return []*volume.Volume{}, err
}

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

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

return &volume.Volume{}, 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 @@ -39,6 +39,7 @@ const (
MNetworkOptions

MVolumeList
MVolumeDetail
)

type model struct {
Expand All @@ -57,6 +58,7 @@ type model struct {
networkDetail viewport.Model
networkOptions NetworkOptions
volumeList VolumeList
volumeDetail viewport.Model
ready bool
currentModel currentModel
ContainerID string
Expand Down Expand Up @@ -174,6 +176,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.networkOptions, _ = m.networkOptions.Update(msg, &m)

m.volumeList.table, _ = m.volumeList.Update(msg, &m)
m.volumeDetail, _ = m.volumeDetail.Update(msg)

cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -232,6 +235,8 @@ func (m model) View() string {

case MVolumeList:
return m.volumeList.View(commands, m.dockerVersion)
case MVolumeDetail:
return m.volumeDetail.View()

default:
return ""
Expand Down
21 changes: 0 additions & 21 deletions models/network_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,6 @@ import (
)

func NewNetworkDetail(network docker.MyNetwork, createTable utils.CreateTableFunc) (viewport.Model, error) {
// content := `
// # Network status

// | Type | Value |
// | ---- | ----- |
// | ID | 1234567890 |
// | Name | my-network |
// | Driver | bridge |
// | Scope | local |
// | Enable IPv6 | false |
// | Internal | false |
// | Attachable | false |

// # Containers

// | ID | Name | IPv4 Address | IPv6 Address |
// | -- | ---- | ------------ | ------------ |
// | 1234567890 | my-container |
// | 1234567890 | my-container |

//`
content := getContentNetwork(network)
const width = 120

Expand Down
72 changes: 72 additions & 0 deletions models/volume_detail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package models

import (
"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) {
content := getContentVolume(volume)
const width = 120

vp := viewport.New(width, 30)
vp.Style = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
PaddingRight(2)

renderer, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(width),
)
if err != nil {
return viewport.Model{}, err
}

str, err := renderer.Render(content)
if err != nil {
return viewport.Model{}, err
}

vp.SetContent(str)

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 |

//`
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)},
})

return response
}
23 changes: 19 additions & 4 deletions models/volume_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

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

"github.com/charmbracelet/bubbles/table"
Expand All @@ -15,10 +17,11 @@ type VolumeList struct {

func NewVolumeList(volumeList []*volume.Volume, query string) VolumeList {
columns := []table.Column{
{Title: "Name", Width: 30},
{Title: "Stack", Width: 20},
{Title: "Driver", Width: 20},
{Title: "Created", Width: 30},
{Title: "Name", Width: 20},
{Title: "Stack", Width: 10},
{Title: "Driver", Width: 10},
{Title: "Mount point", Width: 40},
{Title: "Created", Width: 25},
}

rows := GetVolumeRows(volumeList, query)
Expand Down Expand Up @@ -61,6 +64,17 @@ func (vl VolumeList) Update(msg tea.Msg, m *model) (table.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
v, err := m.dockerClient.GetVolumeByName(vl.table.SelectedRow()[0])
if err != nil {
fmt.Println(err)
}

vd, err := NewVolumeDetail(v, utils.CreateTable)
if err != nil {
fmt.Println(err)
}
m.volumeDetail = vd
m.currentModel = MVolumeDetail
}
}

Expand All @@ -86,6 +100,7 @@ func GetVolumeRows(volumeList []*volume.Volume, query string) []table.Row {
v.Name,
"",
v.Driver,
v.Mountpoint,
v.CreatedAt,
})
}
Expand Down

0 comments on commit 3ef3ff1

Please sign in to comment.