-
Notifications
You must be signed in to change notification settings - Fork 24
/
data.go
107 lines (87 loc) · 2.46 KB
/
data.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
package containers
import (
"fmt"
"strings"
"time"
"github.com/containers/podman-tui/pdcs/containers"
putils "github.com/containers/podman-tui/pdcs/utils"
"github.com/containers/podman-tui/ui/style"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/docker/go-units"
"github.com/rivo/tview"
"github.com/rs/zerolog/log"
)
// UpdateData retrieves containers list data.
func (cnt *Containers) UpdateData() {
cntList, err := containers.List()
if err != nil {
log.Error().Msgf("view: containers update %v", err)
cnt.errorDialog.SetText(fmt.Sprintf("%v", err))
cnt.errorDialog.Display()
return
}
cnt.containersList.mu.Lock()
cnt.containersList.report = cntList
cnt.containersList.mu.Unlock()
}
func (cnt *Containers) getData() []entities.ListContainer {
cnt.containersList.mu.Lock()
data := cnt.containersList.report
cnt.containersList.mu.Unlock()
return data
}
// ClearData clears table data.
func (cnt *Containers) ClearData() {
cnt.containersList.mu.Lock()
cnt.containersList.report = nil
cnt.containersList.mu.Unlock()
cnt.table.Clear()
expand := 1
fgColor := style.PageHeaderFgColor
bgColor := style.PageHeaderBgColor
for i := 0; i < len(cnt.headers); i++ {
cnt.table.SetCell(0, i,
tview.NewTableCell(fmt.Sprintf("[::b]%s", strings.ToUpper(cnt.headers[i]))). //nolint:perfsprint
SetExpansion(expand).
SetBackgroundColor(bgColor).
SetTextColor(fgColor).
SetAlign(tview.AlignLeft).
SetSelectable(false))
}
cnt.table.SetTitle(fmt.Sprintf("[::b]%s[0]", strings.ToUpper(cnt.title)))
}
type conReporter struct {
entities.ListContainer
}
func (con conReporter) names() string {
return strings.Join(con.Names, ",")
}
func (con conReporter) state() string {
var state string
switch con.ListContainer.State {
case "running":
t := units.HumanDuration(time.Since(time.Unix(con.StartedAt, 0)))
state = "Up " + t + " ago"
case "configured":
state = "Created"
case "exited", "stopped":
t := units.HumanDuration(time.Since(time.Unix(con.ExitedAt, 0)))
state = fmt.Sprintf("Exited (%d) %s ago", con.ExitCode, t)
default:
state = con.ListContainer.State
}
return state
}
func (con conReporter) status() string {
hc := con.ListContainer.Status
if hc != "" {
return con.state() + " (" + hc + ")"
}
return con.state()
}
func (con conReporter) ports() string {
if len(con.ListContainer.Ports) < 1 {
return ""
}
return putils.PortsToString(con.ListContainer.Ports)
}