update list containers
This commit is contained in:
@@ -3,19 +3,30 @@ package containers
|
||||
import (
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"github.com/samber/lo"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Ports struct {
|
||||
PrivatePort int `json:"container_port"`
|
||||
PublicPort int `json:"host_port"`
|
||||
}
|
||||
type Port struct {
|
||||
PrivatePort int `json:"PrivatePort"`
|
||||
PublicPort int `json:"PublicPort"`
|
||||
Type string `json:"Type"`
|
||||
}
|
||||
type ListContainers struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Created string `json:"created"`
|
||||
Ports []interface{} `json:"ports"`
|
||||
State string `json:"state"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Created string `json:"created"`
|
||||
Ports []Ports `json:"ports"`
|
||||
Network []string `json:"network"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
type Containers struct {
|
||||
Id string `json:"Id"`
|
||||
@@ -24,7 +35,10 @@ type Containers struct {
|
||||
ImageID string `json:"ImageID"`
|
||||
Command string `json:"Command"`
|
||||
Created int `json:"Created"`
|
||||
//Ports []interface{} `json:"Ports"`
|
||||
Ports []Port `json:"Ports"`
|
||||
Network struct {
|
||||
Networks map[string]any `json:"Networks"`
|
||||
} `json:"NetworkSettings"`
|
||||
State string `json:"State"`
|
||||
Status string `json:"Status"`
|
||||
}
|
||||
@@ -45,15 +59,25 @@ func List() []ListContainers {
|
||||
if err != nil {
|
||||
log.Println("json decode container list error:", err)
|
||||
}
|
||||
var list []ListContainers
|
||||
list := make([]ListContainers, 0)
|
||||
for _, container := range containers {
|
||||
var p []Ports
|
||||
for _, containerPort := range container.Ports {
|
||||
p = append(p, Ports{
|
||||
PrivatePort: containerPort.PrivatePort,
|
||||
PublicPort: containerPort.PublicPort,
|
||||
})
|
||||
}
|
||||
list = append(list, ListContainers{
|
||||
Id: container.Id,
|
||||
Name: strings.TrimPrefix(container.Names[0], "/"),
|
||||
Image: container.Image,
|
||||
Created: time.Unix(int64(container.Created), 0).Format(time.DateTime),
|
||||
State: container.State,
|
||||
Network: lo.Keys(container.Network.Networks),
|
||||
Ports: append([]Ports{}, p...),
|
||||
})
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user