Files
godoxy-yusing/internal/docker/container_helper.go
yusing 35a3e3fef6 refactor(api): restructured API for type safety, maintainability and docs generation
- These changes makes the API incombatible with previous versions
- Added new types for error handling, success responses, and health checks.
- Updated health check logic to utilize the new types for better clarity and structure.
- Refactored existing handlers to improve response consistency and error handling.
- Updated Makefile to include a new target for generating API types from Swagger.
- Updated "new agent" API to respond an encrypted cert pair
2025-08-16 13:04:05 +08:00

81 lines
1.8 KiB
Go

package docker
import (
"strings"
"github.com/docker/docker/api/types/container"
"github.com/yusing/go-proxy/internal/types"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
type containerHelper struct {
*container.Summary
}
// getDeleteLabel gets the value of a label and then deletes it from the container.
// If the label does not exist, an empty string is returned.
func (c containerHelper) getDeleteLabel(label string) string {
if l, ok := c.Labels[label]; ok {
delete(c.Labels, label)
return l
}
return ""
}
func (c containerHelper) getAliases() []string {
if l := c.getDeleteLabel(LabelAliases); l != "" {
return strutils.CommaSeperatedList(l)
}
return []string{c.getName()}
}
func (c containerHelper) getName() string {
return strings.TrimPrefix(c.Names[0], "/")
}
func (c containerHelper) getMounts() []string {
m := make([]string, len(c.Mounts))
for i, v := range c.Mounts {
m[i] = v.Destination
}
return m
}
func (c containerHelper) parseImage() *types.ContainerImage {
colonSep := strutils.SplitRune(c.Image, ':')
slashSep := strutils.SplitRune(colonSep[0], '/')
im := new(types.ContainerImage)
if len(slashSep) > 1 {
im.Author = strings.Join(slashSep[:len(slashSep)-1], "/")
im.Name = slashSep[len(slashSep)-1]
} else {
im.Author = "library"
im.Name = slashSep[0]
}
if len(colonSep) > 1 {
im.Tag = colonSep[1]
} else {
im.Tag = "latest"
}
return im
}
func (c containerHelper) getPublicPortMapping() types.PortMapping {
res := make(types.PortMapping)
for _, v := range c.Ports {
if v.PublicPort == 0 {
continue
}
res[int(v.PublicPort)] = v
}
return res
}
func (c containerHelper) getPrivatePortMapping() types.PortMapping {
res := make(types.PortMapping)
for _, v := range c.Ports {
res[int(v.PrivatePort)] = v
}
return res
}