-
Notifications
You must be signed in to change notification settings - Fork 24
/
volumes.go
195 lines (161 loc) · 4.51 KB
/
volumes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package volumes
import (
"fmt"
"strings"
"sync"
"github.com/containers/podman-tui/ui/dialogs"
"github.com/containers/podman-tui/ui/style"
"github.com/containers/podman-tui/ui/utils"
"github.com/containers/podman-tui/ui/volumes/voldialogs"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/rivo/tview"
)
// Volumes implemnents the volumes page primitive.
type Volumes struct {
*tview.Box
title string
headers []string
table *tview.Table
errorDialog *dialogs.ErrorDialog
progressDialog *dialogs.ProgressDialog
confirmDialog *dialogs.ConfirmDialog
cmdDialog *dialogs.CommandDialog
messageDialog *dialogs.MessageDialog
createDialog *voldialogs.VolumeCreateDialog
volumeList volListReport
confirmData string
}
type volListReport struct {
mu sync.Mutex
report []*entities.VolumeListReport
}
// NewVolumes returns new vols page view.
func NewVolumes() *Volumes {
vols := &Volumes{
Box: tview.NewBox(),
title: "volumes",
headers: []string{"driver", "volume name", "created at", "mount point"},
errorDialog: dialogs.NewErrorDialog(),
progressDialog: dialogs.NewProgressDialog(),
confirmDialog: dialogs.NewConfirmDialog(),
messageDialog: dialogs.NewMessageDialog(""),
createDialog: voldialogs.NewVolumeCreateDialog(),
}
vols.initUI()
return vols
}
func (vols *Volumes) initUI() {
vols.cmdDialog = dialogs.NewCommandDialog([][]string{
{"create", "create a new volume"},
{"inspect", "display detailed volume's information"},
{"prune", "remove all unused volumes"},
{"rm", "remove the selected volume"},
})
vols.table = tview.NewTable()
vols.table.SetTitle(fmt.Sprintf("[::b]%s[0]", strings.ToUpper(vols.title)))
vols.table.SetBorderColor(style.BorderColor)
vols.table.SetBackgroundColor(style.BgColor)
vols.table.SetTitleColor(style.FgColor)
vols.table.SetBorder(true)
for i := 0; i < len(vols.headers); i++ {
vols.table.SetCell(0, i,
tview.NewTableCell(fmt.Sprintf("[black::b]%s", strings.ToUpper(vols.headers[i]))). //nolint:perfsprint
SetExpansion(1).
SetBackgroundColor(style.PageHeaderBgColor).
SetTextColor(style.PageHeaderFgColor).
SetAlign(tview.AlignLeft).
SetSelectable(false))
}
vols.table.SetFixed(1, 1)
vols.table.SetSelectable(true, false)
// set command dialog functions
vols.cmdDialog.SetSelectedFunc(func() {
vols.cmdDialog.Hide()
vols.runCommand(vols.cmdDialog.GetSelectedItem())
})
vols.cmdDialog.SetCancelFunc(func() {
vols.cmdDialog.Hide()
})
// set message dialog functions
vols.messageDialog.SetCancelFunc(func() {
vols.messageDialog.Hide()
})
// set confirm dialogs functions
vols.confirmDialog.SetSelectedFunc(func() {
vols.confirmDialog.Hide()
switch vols.confirmData {
case "prune":
vols.prune()
case "rm":
vols.remove()
}
})
vols.confirmDialog.SetCancelFunc(func() {
vols.confirmDialog.Hide()
})
// set create dialog functions
vols.createDialog.SetCancelFunc(func() {
vols.createDialog.Hide()
})
vols.createDialog.SetCreateFunc(func() {
vols.createDialog.Hide()
vols.create()
})
}
// GetTitle returns primitive title.
func (vols *Volumes) GetTitle() string {
return vols.title
}
// HasFocus returns whether or not this primitive has focus.
func (vols *Volumes) HasFocus() bool {
if vols.SubDialogHasFocus() || vols.table.HasFocus() {
return true
}
return vols.Box.HasFocus()
}
// SubDialogHasFocus returns whether or not sub dialog primitive has focus.
func (vols *Volumes) SubDialogHasFocus() bool {
for _, dialog := range vols.getInnerDialogs() {
if dialog.HasFocus() {
return true
}
}
return false
}
// Focus is called when this primitive receives focus.
func (vols *Volumes) Focus(delegate func(p tview.Primitive)) {
for _, dialog := range vols.getInnerDialogs() {
if dialog.IsDisplay() {
delegate(dialog)
return
}
}
delegate(vols.table)
}
// HideAllDialogs hides all sub dialogs.
func (vols *Volumes) HideAllDialogs() {
for _, dialog := range vols.getInnerDialogs() {
if dialog.IsDisplay() {
dialog.Hide()
}
}
}
func (vols *Volumes) getInnerDialogs() []utils.UIDialog {
dialogs := []utils.UIDialog{
vols.errorDialog,
vols.progressDialog,
vols.confirmDialog,
vols.cmdDialog,
vols.messageDialog,
vols.createDialog,
}
return dialogs
}
func (vols *Volumes) getSelectedItem() string {
if vols.table.GetRowCount() <= 1 {
return ""
}
row, _ := vols.table.GetSelection()
volID := vols.table.GetCell(row, 1).Text
return volID
}