forked from Mikescher/better-docker-ps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
111 lines (98 loc) · 3.1 KB
/
schema.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
package docker
import (
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net"
)
type ContainerSchema struct {
ID string `json:"Id"`
Names []string `json:"Names"`
Image string `json:"Image"`
ImageID string `json:"ImageID"`
Command string `json:"Command"`
Created int64 `json:"Created"`
Ports []PortSchema `json:"Ports"`
Labels map[string]string `json:"Labels"`
State ContainerState `json:"State"`
Status string `json:"Status"`
HostConfig ContainerHostConfig `json:"HostConfig"`
NetworkSettings ContainerNetworkSettings `json:"NetworkSettings"`
Mounts []ContainerMount `json:"Mounts"`
SizeRw int64 `json:"SizeRw"`
SizeRootFs int64 `json:"SizeRootFs"`
}
func (s ContainerSchema) PortsSorted() []PortSchema {
ports := langext.ArrCopy(s.Ports)
langext.SortSliceStable(ports, func(p1, p2 PortSchema) bool {
if p1.PublicPort != p2.PublicPort {
return p1.PublicPort < p2.PublicPort
}
if p1.PrivatePort != p2.PrivatePort {
return p1.PrivatePort < p2.PrivatePort
}
return false
})
return ports
}
type ContainerHostConfig struct {
NetworkMode string `json:"NetworkMode"`
}
type ContainerNetworkSettings struct {
Networks map[string]ContainerSingleNetworkSettings `json:"Networks"`
}
type ContainerSingleNetworkSettings struct {
NetworkMode string `json:"NetworkID"`
EndpointID string `json:"EndpointID"`
Gateway string `json:"Gateway"`
IPAddress string `json:"IPAddress"`
IPPrefixLen int `json:"IPPrefixLen"`
IPv6Gateway string `json:"IPv6Gateway"`
GlobalIPv6Address string `json:"GlobalIPv6Address"`
GlobalIPv6PrefixLen int `json:"GlobalIPv6PrefixLen"`
MacAddress string `json:"MacAddress"`
}
type PortSchema struct {
IP string `json:"IP"`
PrivatePort int `json:"PrivatePort"`
PublicPort int `json:"PublicPort"`
Type string `json:"Type"`
}
func (s PortSchema) IsLoopback() bool {
ip := net.ParseIP(s.IP)
return ip != nil && ip.IsLoopback()
}
type ContainerMount struct {
Name string `json:"Name"`
Source string `json:"Source"`
Destination string `json:"Destination"`
Driver string `json:"Driver"`
Mode string `json:"Mode"`
RW bool `json:"RW"`
Propagation string `json:"Propagation"`
}
type ContainerState string
const (
StateCreated ContainerState = "created"
StateRunning ContainerState = "running"
StateRestarting ContainerState = "restarting"
StateExited ContainerState = "exited"
StatePaused ContainerState = "paused"
StateDead ContainerState = "dead"
)
func (ct ContainerState) Num() int {
switch ct {
case StateCreated:
return 0
case StateRunning:
return 1
case StateRestarting:
return 2
case StateExited:
return 3
case StatePaused:
return 4
case StateDead:
return 5
default:
return 999
}
}