Skip to content

Commit

Permalink
add network option remove
Browse files Browse the repository at this point in the history
  • Loading branch information
ernesto27 committed Jul 2, 2023
1 parent b81acdc commit 029e6ff
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ func (d *Docker) NetworkList() ([]MyNetwork, error) {
return myNetwork, nil
}

func (d *Docker) NetworkRemove(networkID string) error {
return d.cli.NetworkRemove(d.ctx, networkID)
}

func (d *Docker) getContainerIP(c types.ContainerJSON) string {
networkSettings := c.NetworkSettings
networkMode := string(c.HostConfig.NetworkMode)
Expand Down
5 changes: 5 additions & 0 deletions models/container_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func NewContainerOptions(container string, image string) ContainerOptions {
}
}

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

func (o ContainerOptions) Update(msg tea.Msg, m *model) (ContainerOptions, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
Expand Down
5 changes: 5 additions & 0 deletions models/image_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func NewImageOptions(container string, image string) ImageOptions {
}
}

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

func (o ImageOptions) Update(msg tea.Msg, m *model) (ImageOptions, tea.Cmd) {
if m.currentModel != MImageOptions {
return o, nil
Expand Down
11 changes: 8 additions & 3 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
const commands = `
GENERAL ↑/↓: Navigate • ctrl/c: Exit • ctrl/r: refresh • esc: Back
CONTAINERS ctrl/f: Search • ctrl/l: Logs • ctrl/o: Options • ctrl/e: Attach cmd
IMAGES ctrl/b: List • ctrl/f: Search
NETWORKS ctrl/n: List
IMAGES ctrl/b: List • ctrl/f: Search • ctrl/o: Options
NETWORKS ctrl/n: List • ctrl/f: Search • ctrl/o: Options
`

var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#9999FF")).Render
Expand All @@ -36,6 +36,7 @@ const (
MNetworkList
MNetworkSearch
MNetworkDetail
MNetworkOptions
)

type model struct {
Expand All @@ -52,6 +53,7 @@ type model struct {
networkList NetworkList
networkSearch NetworkSearch
networkDetail viewport.Model
networkOptions NetworkOptions
ready bool
currentModel currentModel
ContainerID string
Expand Down Expand Up @@ -94,7 +96,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.ClearScreen
}

if m.currentModel == MNetworkDetail || m.currentModel == MNetworkSearch {
if m.currentModel == MNetworkDetail || m.currentModel == MNetworkSearch || m.currentModel == MNetworkOptions {
m.currentModel = MNetworkList
return m, tea.ClearScreen
}
Expand Down Expand Up @@ -158,6 +160,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.networkList.table, _ = m.networkList.Update(msg, &m)
m.networkSearch, _ = m.networkSearch.Update(msg, &m)
m.networkOptions, _ = m.networkOptions.Update(msg, &m)

cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -211,6 +214,8 @@ func (m model) View() string {
return m.networkSearch.View()
case MNetworkDetail:
return m.networkDetail.View()
case MNetworkOptions:
return m.networkOptions.View()

default:
return ""
Expand Down
3 changes: 3 additions & 0 deletions models/network_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func (cl NetworkList) Update(msg tea.Msg, m *model) (table.Model, tea.Cmd) {
case "ctrl+f":
m.networkSearch.textInput.SetValue("")
m.currentModel = MNetworkSearch
case "ctrl+o":
m.networkOptions = NewNetworkOptions(m.networkList.table.SelectedRow()[1])
m.currentModel = MNetworkOptions
}
}

Expand Down
65 changes: 65 additions & 0 deletions models/network_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 NetworkOptions struct {
Options
}

func NewNetworkOptions(name string) NetworkOptions {
choices := []string{Remove}

return NetworkOptions{
Options{
Cursor: 0,
Choice: "",
Choices: choices,
Network: name,
},
}
}

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

func (n NetworkOptions) Update(msg tea.Msg, m *model) (NetworkOptions, tea.Cmd) {
if m.currentModel != MNetworkOptions {
return n, nil
}

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

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

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

m.networkList = NewNetworkList(networks, "")
m.currentModel = MNetworkList
}

}
}

return n, nil
}
12 changes: 3 additions & 9 deletions models/options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models

import (
"fmt"
"strings"

"github.com/charmbracelet/lipgloss"
Expand All @@ -13,6 +12,7 @@ type Options struct {
Choices []string
Container string
Image string
Network string
}

const (
Expand All @@ -22,7 +22,7 @@ const (
Restart = "Restart"
)

func (o Options) View() string {
func (o Options) View(title string) string {
s := strings.Builder{}

var style = lipgloss.NewStyle().
Expand All @@ -45,11 +45,5 @@ func (o Options) View() string {
s.WriteString("\n")
}
s.WriteString("\n(press Esc to go back)\n")

options := fmt.Sprintf("Options container: %s - %s", o.Container, o.Image)
if o.Container == "" {
options = fmt.Sprintf("Options image: %s", o.Image)
}

return style.Render(options) + s.String()
return style.Render(title) + s.String()
}

0 comments on commit 029e6ff

Please sign in to comment.