Skip to content

Commit

Permalink
volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
somnek committed Jan 26, 2024
1 parent 7f33718 commit ad16623
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
notUsingYet.go
data
Taskfile.yml
Expand Down
18 changes: 18 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ import (
// client instead of assigning to _
////////////////////////////////////////////

// ---------------- Volume ----------------
func removeVolume(c *docker.Client, id string) {
opts := docker.RemoveVolumeOptions{
Name: id,
Force: true,
}
_ = c.RemoveVolumeWithOptions(opts)
}

func listVolumes(c *docker.Client) []docker.Volume {
opts := docker.ListVolumesOptions{}
volumes, err := c.ListVolumes(opts)
if err != nil {
log.Fatal(err)
}
return volumes
}

// ---------------- Image ----------------
func removeImage(c *docker.Client, id string) {
opts := docker.RemoveImageOptions{
Expand Down
18 changes: 18 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,21 @@ func getImages() []Image {
}
return images
}

// ---------------- Volume ----------------
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) {
volume := Volume{
name: v.Name,
mountPoint: v.Mountpoint,
}
volumes = append(volumes, volume)
}
return volumes
}
15 changes: 15 additions & 0 deletions keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type keyMap struct {
Clear key.Binding
SelectAll key.Binding
Tab key.Binding
num1 key.Binding
num2 key.Binding
num3 key.Binding
Toggle key.Binding

Remove key.Binding
Expand Down Expand Up @@ -88,6 +91,18 @@ var keys = keyMap{
key.WithKeys("tab"),
key.WithHelp("tab", "switch page"),
),
num1: key.NewBinding(
key.WithKeys("1"),
key.WithHelp("1", "Containers Page"),
),
num2: key.NewBinding(
key.WithKeys("2"),
key.WithHelp("2", "Images Page"),
),
num3: key.NewBinding(
key.WithKeys("3"),
key.WithHelp("3", "Volumes Page"),
),
Toggle: key.NewBinding(
key.WithKeys(" ", "enter"),
key.WithHelp("space/enter", "toggle selection"),
Expand Down
21 changes: 11 additions & 10 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
)

Expand All @@ -16,23 +15,29 @@ type Container struct {
desc string
}

type Volume struct {
name string
mountPoint string
}

type Image struct {
name string
id string
name string
}

const (
pageContainer int = iota
pageImage
pageVolume
pageLog
)

type model struct {
containers []Container
images []Image
volumes []Volume
cursor int
selected map[int]struct{}
spinner spinner.Model
blinkSwitch int
// TODO: merge process into Container struct
processes map[string]string // map[containerID]desiredState
Expand Down Expand Up @@ -61,25 +66,21 @@ func doTick() tea.Cmd {
}

func (m model) Init() tea.Cmd {
return tea.Batch(doTick(), m.spinner.Tick)
return doTick()
}

func initialModel() model {
cursor := 0
// containers
containers := getContainers()
images := getImages()
volumes := getVolumes()

// 0 container scenario
if len(containers) > 0 {
containers[cursor].desc = buildContainerDescShort(containers[cursor].id)
}

// spinner
s := spinner.New()
s.Spinner = spinner.Jump
s.Style = spinnerStyle

// help
h := help.New()
h.Width = fullWidth
Expand All @@ -89,9 +90,9 @@ func initialModel() model {
return model{
containers: containers,
images: images,
volumes: volumes,
selected: make(map[int]struct{}),
processes: processes,
spinner: s,
page: pageContainer,
keys: keys,
help: h,
Expand Down
4 changes: 0 additions & 4 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ var (
Foreground(frenchBlue).
Bold(true)

spinnerStyle = lipgloss.NewStyle().
Foreground(celesBlue).
Bold(true)

helpStyle = lipgloss.NewStyle()

PortMapColStyle = lipgloss.NewStyle().Foreground(paletteA10)
Expand Down
55 changes: 50 additions & 5 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
)

func (m model) togglePageKey() keyMap {
m.keys = keys // keys is default (container)

switch m.page {
case pageImage:
m.keys.Restart.Unbind()
Expand All @@ -17,8 +19,14 @@ func (m model) togglePageKey() keyMap {
m.keys.Start.Unbind()
m.keys.Pause.Unbind()
m.keys.Unpause.Unbind()
case pageVolume:
m.keys.Restart.Unbind()
m.keys.Kill.Unbind()
m.keys.Stop.Unbind()
m.keys.Start.Unbind()
m.keys.Pause.Unbind()
m.keys.Unpause.Unbind()
case pageContainer:
m.keys = keys // keys is default (container)
}
return m.keys
}
Expand All @@ -38,10 +46,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {

// case spinner.TickMsg:
// m.spinner, cmd = m.spinner.Update(msg)
// return m, cmd

case TickMsg:
// containers
containers := getContainers()
Expand Down Expand Up @@ -85,6 +89,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return handleImageKeys(m, msg)
}
return handleCommonKeys(&m, msg)
case pageVolume:
if getCurrentViewItemCount(m) > 0 {
return handleVolumeKeys(m, msg)
}
return handleCommonKeys(&m, msg)
}

handleCommonKeys(&m, msg)
Expand All @@ -105,6 +114,24 @@ func (img Image) findAssociatedContainersInUse(m model) []Container {
return containers
}

func handleVolumeKeys(m model, msg tea.KeyMsg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

// handle 0 volumes
if getCurrentViewItemCount(m) == 0 {
return handleCommonKeys(&m, msg)
}

switch {
case key.Matches(msg, m.keys.Remove): // remove
// TODO: remove
return m, cmd

default:
return handleCommonKeys(&m, msg)
}
}

func handleImageKeys(m model, msg tea.KeyMsg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

Expand Down Expand Up @@ -270,6 +297,24 @@ func handleCommonKeys(m *model, msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.help.ShowAll = !m.help.ShowAll
return m, nil

case key.Matches(msg, m.keys.num1): // page 1: containers
if m.page != pageContainer {
m.page = pageContainer
}
m.keys = m.togglePageKey()

case key.Matches(msg, m.keys.num2): // page 2: images
if m.page != pageImage {
m.page = pageImage
}
m.keys = m.togglePageKey()

case key.Matches(msg, m.keys.num3): // page 3: volumes
if m.page != pageVolume {
m.page = pageVolume
}
m.keys = m.togglePageKey()

case key.Matches(msg, m.keys.Tab): // switch tab
if m.page == pageContainer {
m.page = pageImage
Expand Down
12 changes: 12 additions & 0 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ func buildLogView(m model) string {
return logStyle.Render(s)
}

// ----------------------------- volume view -----------------------------

func buildVolumeView(m model) (string, string) {
var bodyL string
for _, choice := range m.volumes {
bodyL += choice.name + "\n"
}
return bodyLStyle.Render(bodyL), "abc"
}

// ----------------------------- image view -----------------------------

func buildImageDescShort(id string) string {
Expand Down Expand Up @@ -267,6 +277,8 @@ func (m model) View() string {
bodyL, bodyR = buildContainerView(m)
case pageImage:
bodyL, bodyR = buildImageView(m)
case pageVolume:
bodyL, bodyR = buildVolumeView(m)
}

// title
Expand Down

0 comments on commit ad16623

Please sign in to comment.