Skip to content

Commit

Permalink
add volume option remove
Browse files Browse the repository at this point in the history
  • Loading branch information
ernesto27 committed Jul 4, 2023
1 parent 10ff7df commit 811e0c1
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 23 deletions.
4 changes: 4 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ func (d *Docker) GetVolumeByName(name string) (MyVolume, error) {
return MyVolume{}, fmt.Errorf("volume %s not found", name)
}

func (d *Docker) VolumeRemove(volumeID string) error {
return d.cli.VolumeRemove(d.ctx, volumeID, false)
}

func (d *Docker) Events() {
go func() {
eventStream, err := d.cli.Events(d.ctx, types.EventsOptions{})
Expand Down
12 changes: 6 additions & 6 deletions models/container_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ func NewContainerOptions(container string, image string) ContainerOptions {

return ContainerOptions{
Options{
Cursor: 0,
Choice: "",
Choices: choices,
Container: container,
Image: image,
Cursor: 0,
Choice: "",
Choices: choices,
Text1: container,
Text2: image,
},
}
}

func (o ContainerOptions) View() string {
title := fmt.Sprintf("Options container: %s - %s", o.Container, o.Image)
title := fmt.Sprintf("Options container: %s - %s", o.Text1, o.Text2)
return o.Options.View(title)
}

Expand Down
2 changes: 1 addition & 1 deletion models/image_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (il ImageList) Update(msg tea.Msg, m *model) (table.Model, tea.Cmd) {
m.imageSearch.textInput.SetValue("")
m.currentModel = MImageSearch
case "ctrl+o":
ov := NewImageOptions("", m.imageList.table.SelectedRow()[1])
ov := NewImageOptions(m.imageList.table.SelectedRow()[1])
m.imageOptions = ov
m.currentModel = MImageOptions

Expand Down
13 changes: 6 additions & 7 deletions models/image_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ type ImageOptions struct {
Options
}

func NewImageOptions(container string, image string) ImageOptions {
func NewImageOptions(image string) ImageOptions {
choices := []string{Remove}

return ImageOptions{
Options{
Cursor: 0,
Choice: "",
Choices: choices,
Container: container,
Image: image,
Cursor: 0,
Choice: "",
Choices: choices,
Text1: image,
},
}
}

func (o ImageOptions) View() string {
title := fmt.Sprintf("Options image: %s", o.Image)
title := fmt.Sprintf("Options image: %s", o.Text1)
return o.Options.View(title)
}

Expand Down
7 changes: 6 additions & 1 deletion models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
MVolumeList
MVolumeDetail
MVolumeSearch
MVolumeOptions
)

type model struct {
Expand All @@ -61,6 +62,7 @@ type model struct {
volumeList VolumeList
volumeDetail viewport.Model
volumeSearch VolumeSearch
volumeOptions VolumeOptions
ready bool
currentModel currentModel
ContainerID string
Expand Down Expand Up @@ -109,7 +111,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.ClearScreen
}

if m.currentModel == MVolumeDetail || m.currentModel == MVolumeSearch {
if m.currentModel == MVolumeDetail || m.currentModel == MVolumeSearch || m.currentModel == MVolumeOptions {
m.currentModel = MVolumeList
return m, tea.ClearScreen
}
Expand Down Expand Up @@ -186,6 +188,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.volumeList.table, _ = m.volumeList.Update(msg, &m)
m.volumeDetail, _ = m.volumeDetail.Update(msg)
m.volumeSearch, _ = m.volumeSearch.Update(msg, &m)
m.volumeOptions, _ = m.volumeOptions.Update(msg, &m)

cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -248,6 +251,8 @@ func (m model) View() string {
return m.volumeDetail.View()
case MVolumeSearch:
return m.volumeSearch.View()
case MVolumeOptions:
return m.volumeOptions.View()

default:
return ""
Expand Down
4 changes: 2 additions & 2 deletions models/network_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func NewNetworkOptions(name string) NetworkOptions {
Cursor: 0,
Choice: "",
Choices: choices,
Network: name,
Text1: name,
},
}
}

func (n NetworkOptions) View() string {
title := fmt.Sprintf("Options network: %s", n.Network)
title := fmt.Sprintf("Options network: %s", n.Text1)
return n.Options.View(title)
}

Expand Down
11 changes: 5 additions & 6 deletions models/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
)

type Options struct {
Cursor int
Choice string
Choices []string
Container string
Image string
Network string
Cursor int
Choice string
Choices []string
Text1 string
Text2 string
}

const (
Expand Down
3 changes: 3 additions & 0 deletions models/volume_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (vl VolumeList) Update(msg tea.Msg, m *model) (table.Model, tea.Cmd) {
case "ctrl+f":
m.volumeSearch.textInput.SetValue("")
m.currentModel = MVolumeSearch
case "ctrl+o":
m.volumeOptions = NewVolumeOptions(vl.table.SelectedRow()[0])
m.currentModel = MVolumeOptions
}
}

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

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
)

type VolumeOptions struct {
Options
}

func NewVolumeOptions(name string) VolumeOptions {
choices := []string{Remove}

return VolumeOptions{
Options{
Cursor: 0,
Choice: "",
Choices: choices,
Text1: name,
},
}
}

func (v VolumeOptions) View() string {
title := fmt.Sprintf("Options volume: %s", v.Text1)
return v.Options.View(title)
}

func (v VolumeOptions) Update(msg tea.Msg, m *model) (VolumeOptions, tea.Cmd) {
if m.currentModel != MVolumeOptions {
return v, nil
}

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
errAction := false
option := m.volumeOptions.Choices[m.volumeOptions.Cursor]

if option == Remove {
err := m.dockerClient.VolumeRemove(m.volumeList.table.SelectedRow()[0])
if err != nil {
fmt.Println(err)
errAction = true
}
}

if !errAction {
v, err := m.dockerClient.VolumeList()
if err != nil {
fmt.Println(err)
}

m.volumeList = NewVolumeList(v, "")
m.currentModel = MVolumeList
}

}
}

return v, nil
}

0 comments on commit 811e0c1

Please sign in to comment.