Use types for docker client package directly
- commit
- 2451b8a79ae83ca30ef1ccfe7f078d4ced999ef8
- author
- bnema · · 2023-09-21 12:54 UTC
- parent
- c6b1346
- signature
- unsigned
4 files · +31 -65
M internal/httpserve/handler/docker.go +6 -5
| @@ -10,13 +10,14 @@ import ( | |||
| 10 | 10 | "github.com/bnema/gordon/pkg/utils/docker" | |
| 11 | 11 | "github.com/bnema/gordon/pkg/utils/humanize" | |
| 12 | 12 | "github.com/bnema/gordon/pkg/utils/sanitize" | |
| 13 | + | "github.com/docker/docker/api/types" | |
| 13 | 14 | "github.com/labstack/echo/v4" | |
| 14 | 15 | ) | |
| 15 | 16 | ||
| 16 | 17 | var idMap = make(map[string]string) | |
| 17 | 18 | ||
| 18 | 19 | type HumanReadableContainerImage struct { | |
| 19 | - | docker.ContainerImage | |
| 20 | + | *types.ImageSummary | |
| 20 | 21 | ShortID string | |
| 21 | 22 | CreatedStr string | |
| 22 | 23 | SizeStr string | |
| @@ -44,10 +45,10 @@ func ImageManagerHandler(c echo.Context, a *app.App) error { | |||
| 44 | 45 | sizeStr := humanize.BytesToReadableSize(image.Size) | |
| 45 | 46 | ||
| 46 | 47 | humanReadableImages = append(humanReadableImages, HumanReadableContainerImage{ | |
| 47 | - | ShortID: ShortID, | |
| 48 | - | ContainerImage: image, | |
| 49 | - | CreatedStr: createdStr, | |
| 50 | - | SizeStr: sizeStr, | |
| 48 | + | ShortID: ShortID, | |
| 49 | + | ImageSummary: &image, | |
| 50 | + | CreatedStr: createdStr, | |
| 51 | + | SizeStr: sizeStr, | |
| 51 | 52 | }) | |
| 52 | 53 | } | |
| 53 | 54 | data := map[string]interface{}{ | |
| @@ -10,13 +10,14 @@ import ( | |||||
| 10 | "github.com/bnema/gordon/pkg/utils/docker" | 10 | "github.com/bnema/gordon/pkg/utils/docker" | ||
| 11 | "github.com/bnema/gordon/pkg/utils/humanize" | 11 | "github.com/bnema/gordon/pkg/utils/humanize" | ||
| 12 | "github.com/bnema/gordon/pkg/utils/sanitize" | 12 | "github.com/bnema/gordon/pkg/utils/sanitize" | ||
| 13 | + | "github.com/docker/docker/api/types" | |||
| 13 | "github.com/labstack/echo/v4" | 14 | "github.com/labstack/echo/v4" | ||
| 14 | ) | 15 | ) | ||
| 15 | 16 | ||||
| 16 | var idMap = make(map[string]string) | 17 | var idMap = make(map[string]string) | ||
| 17 | 18 | ||||
| 18 | type HumanReadableContainerImage struct { | 19 | type HumanReadableContainerImage struct { | ||
| 19 | - | docker.ContainerImage | 20 | + | *types.ImageSummary |
| 20 | ShortID string | 21 | ShortID string | ||
| 21 | CreatedStr string | 22 | CreatedStr string | ||
| 22 | SizeStr string | 23 | SizeStr string | ||
| @@ -44,10 +45,10 @@ func ImageManagerHandler(c echo.Context, a *app.App) error { | |||||
| 44 | sizeStr := humanize.BytesToReadableSize(image.Size) | 45 | sizeStr := humanize.BytesToReadableSize(image.Size) | ||
| 45 | 46 | ||||
| 46 | humanReadableImages = append(humanReadableImages, HumanReadableContainerImage{ | 47 | humanReadableImages = append(humanReadableImages, HumanReadableContainerImage{ | ||
| 47 | - | ShortID: ShortID, | 48 | + | ShortID: ShortID, |
| 48 | - | ContainerImage: image, | 49 | + | ImageSummary: &image, |
| 49 | - | CreatedStr: createdStr, | 50 | + | CreatedStr: createdStr, |
| 50 | - | SizeStr: sizeStr, | 51 | + | SizeStr: sizeStr, |
| 51 | }) | 52 | }) | ||
| 52 | } | 53 | } | ||
| 53 | data := map[string]interface{}{ | 54 | data := map[string]interface{}{ | ||
M pkg/utils/docker/client.go +0 -42
| @@ -1,38 +1,11 @@ | |||
| 1 | 1 | package docker | |
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | - | "context" | |
| 5 | 4 | "fmt" | |
| 6 | 5 | ||
| 7 | - | "github.com/docker/docker/api/types" | |
| 8 | 6 | "github.com/docker/docker/client" | |
| 9 | 7 | ) | |
| 10 | 8 | ||
| 11 | - | type ContainerImage struct { | |
| 12 | - | ID string `json:"Id"` | |
| 13 | - | ParentID string `json:",omitempty"` | |
| 14 | - | RepoTags []string `json:",omitempty"` | |
| 15 | - | RepoDigests []string `json:",omitempty"` | |
| 16 | - | Created int64 | |
| 17 | - | Size int64 | |
| 18 | - | SharedSize int64 | |
| 19 | - | Labels map[string]string `json:",omitempty"` | |
| 20 | - | Containers int64 | |
| 21 | - | } | |
| 22 | - | ||
| 23 | - | type Container struct { | |
| 24 | - | ID string `json:"Id"` | |
| 25 | - | Names []string | |
| 26 | - | Image string | |
| 27 | - | ImageID string | |
| 28 | - | Command string | |
| 29 | - | Created int64 | |
| 30 | - | State string | |
| 31 | - | Status string | |
| 32 | - | Ports []types.Port | |
| 33 | - | Labels map[string]string `json:",omitempty"` | |
| 34 | - | } | |
| 35 | - | ||
| 36 | 9 | type ContainerEngineClient interface { | |
| 37 | 10 | InitializeClient(*Config) error | |
| 38 | 11 | // Add other methods that both Docker and Podman should implement | |
| @@ -64,18 +37,3 @@ func (d *DockerClient) InitializeClient(config *Config) error { | |||
| 64 | 37 | dockerCli = cli | |
| 65 | 38 | return nil | |
| 66 | 39 | } | |
| 67 | - | ||
| 68 | - | func ListRunningContainers() ([]types.Container, error) { | |
| 69 | - | // Check if the Docker client has been initialized | |
| 70 | - | if dockerCli == nil { | |
| 71 | - | return nil, fmt.Errorf("Docker client has not been initialized") | |
| 72 | - | } | |
| 73 | - | ||
| 74 | - | // List containers using the Docker client | |
| 75 | - | containers, err := dockerCli.ContainerList(context.Background(), types.ContainerListOptions{}) | |
| 76 | - | if err != nil { | |
| 77 | - | return nil, err | |
| 78 | - | } | |
| 79 | - | ||
| 80 | - | return containers, nil | |
| 81 | - | } | |
| @@ -1,38 +1,11 @@ | |||||
| 1 | package docker | 1 | package docker | ||
| 2 | 2 | ||||
| 3 | import ( | 3 | import ( | ||
| 4 | - | "context" | |||
| 5 | "fmt" | 4 | "fmt" | ||
| 6 | 5 | ||||
| 7 | - | "github.com/docker/docker/api/types" | |||
| 8 | "github.com/docker/docker/client" | 6 | "github.com/docker/docker/client" | ||
| 9 | ) | 7 | ) | ||
| 10 | 8 | ||||
| 11 | - | type ContainerImage struct { | |||
| 12 | - | ID string `json:"Id"` | |||
| 13 | - | ParentID string `json:",omitempty"` | |||
| 14 | - | RepoTags []string `json:",omitempty"` | |||
| 15 | - | RepoDigests []string `json:",omitempty"` | |||
| 16 | - | Created int64 | |||
| 17 | - | Size int64 | |||
| 18 | - | SharedSize int64 | |||
| 19 | - | Labels map[string]string `json:",omitempty"` | |||
| 20 | - | Containers int64 | |||
| 21 | - | } | |||
| 22 | - | ||||
| 23 | - | type Container struct { | |||
| 24 | - | ID string `json:"Id"` | |||
| 25 | - | Names []string | |||
| 26 | - | Image string | |||
| 27 | - | ImageID string | |||
| 28 | - | Command string | |||
| 29 | - | Created int64 | |||
| 30 | - | State string | |||
| 31 | - | Status string | |||
| 32 | - | Ports []types.Port | |||
| 33 | - | Labels map[string]string `json:",omitempty"` | |||
| 34 | - | } | |||
| 35 | - | ||||
| 36 | type ContainerEngineClient interface { | 9 | type ContainerEngineClient interface { | ||
| 37 | InitializeClient(*Config) error | 10 | InitializeClient(*Config) error | ||
| 38 | // Add other methods that both Docker and Podman should implement | 11 | // Add other methods that both Docker and Podman should implement | ||
| @@ -64,18 +37,3 @@ func (d *DockerClient) InitializeClient(config *Config) error { | |||||
| 64 | dockerCli = cli | 37 | dockerCli = cli | ||
| 65 | return nil | 38 | return nil | ||
| 66 | } | 39 | } | ||
| 67 | - | ||||
| 68 | - | func ListRunningContainers() ([]types.Container, error) { | |||
| 69 | - | // Check if the Docker client has been initialized | |||
| 70 | - | if dockerCli == nil { | |||
| 71 | - | return nil, fmt.Errorf("Docker client has not been initialized") | |||
| 72 | - | } | |||
| 73 | - | ||||
| 74 | - | // List containers using the Docker client | |||
| 75 | - | containers, err := dockerCli.ContainerList(context.Background(), types.ContainerListOptions{}) | |||
| 76 | - | if err != nil { | |||
| 77 | - | return nil, err | |||
| 78 | - | } | |||
| 79 | - | ||||
| 80 | - | return containers, nil | |||
| 81 | - | } | |||
A pkg/utils/docker/containers.go +23 -0
| @@ -0,0 +1,23 @@ | |||
| 1 | + | package docker | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "context" | |
| 5 | + | "fmt" | |
| 6 | + | ||
| 7 | + | "github.com/docker/docker/api/types" | |
| 8 | + | ) | |
| 9 | + | ||
| 10 | + | func ListRunningContainers() ([]types.Container, error) { | |
| 11 | + | // Check if the Docker client has been initialized | |
| 12 | + | if dockerCli == nil { | |
| 13 | + | return nil, fmt.Errorf("Docker client has not been initialized") | |
| 14 | + | } | |
| 15 | + | ||
| 16 | + | // List containers using the Docker client | |
| 17 | + | containers, err := dockerCli.ContainerList(context.Background(), types.ContainerListOptions{}) | |
| 18 | + | if err != nil { | |
| 19 | + | return nil, err | |
| 20 | + | } | |
| 21 | + | ||
| 22 | + | return containers, nil | |
| 23 | + | } | |
| @@ -0,0 +1,23 @@ | |||||
| 1 | + | package docker | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "context" | |||
| 5 | + | "fmt" | |||
| 6 | + | ||||
| 7 | + | "github.com/docker/docker/api/types" | |||
| 8 | + | ) | |||
| 9 | + | ||||
| 10 | + | func ListRunningContainers() ([]types.Container, error) { | |||
| 11 | + | // Check if the Docker client has been initialized | |||
| 12 | + | if dockerCli == nil { | |||
| 13 | + | return nil, fmt.Errorf("Docker client has not been initialized") | |||
| 14 | + | } | |||
| 15 | + | ||||
| 16 | + | // List containers using the Docker client | |||
| 17 | + | containers, err := dockerCli.ContainerList(context.Background(), types.ContainerListOptions{}) | |||
| 18 | + | if err != nil { | |||
| 19 | + | return nil, err | |||
| 20 | + | } | |||
| 21 | + | ||||
| 22 | + | return containers, nil | |||
| 23 | + | } | |||
M pkg/utils/docker/images.go +2 -18
| @@ -7,7 +7,7 @@ import ( | |||
| 7 | 7 | "github.com/docker/docker/api/types" | |
| 8 | 8 | ) | |
| 9 | 9 | ||
| 10 | - | func ListContainerImages() ([]ContainerImage, error) { | |
| 10 | + | func ListContainerImages() ([]types.ImageSummary, error) { | |
| 11 | 11 | // Check if the Docker client has been initialized | |
| 12 | 12 | if dockerCli == nil { | |
| 13 | 13 | return nil, fmt.Errorf("Docker client has not been initialized") | |
| @@ -19,23 +19,7 @@ func ListContainerImages() ([]ContainerImage, error) { | |||
| 19 | 19 | return nil, err | |
| 20 | 20 | } | |
| 21 | 21 | ||
| 22 | - | // Populate the ContainerImage slice | |
| 23 | - | var containerImages []ContainerImage | |
| 24 | - | for _, image := range images { | |
| 25 | - | containerImages = append(containerImages, ContainerImage{ | |
| 26 | - | ID: image.ID, | |
| 27 | - | ParentID: image.ParentID, | |
| 28 | - | RepoTags: image.RepoTags, | |
| 29 | - | RepoDigests: image.RepoDigests, | |
| 30 | - | Created: image.Created, | |
| 31 | - | Size: image.Size, | |
| 32 | - | SharedSize: image.SharedSize, | |
| 33 | - | Labels: image.Labels, | |
| 34 | - | Containers: image.Containers, | |
| 35 | - | }) | |
| 36 | - | } | |
| 37 | - | ||
| 38 | - | return containerImages, nil | |
| 22 | + | return images, nil | |
| 39 | 23 | } | |
| 40 | 24 | func DeleteContainerImage(imageID string) error { | |
| 41 | 25 | // Check if the Docker client has been initialized | |
| @@ -7,7 +7,7 @@ import ( | |||||
| 7 | "github.com/docker/docker/api/types" | 7 | "github.com/docker/docker/api/types" | ||
| 8 | ) | 8 | ) | ||
| 9 | 9 | ||||
| 10 | - | func ListContainerImages() ([]ContainerImage, error) { | 10 | + | func ListContainerImages() ([]types.ImageSummary, error) { |
| 11 | // Check if the Docker client has been initialized | 11 | // Check if the Docker client has been initialized | ||
| 12 | if dockerCli == nil { | 12 | if dockerCli == nil { | ||
| 13 | return nil, fmt.Errorf("Docker client has not been initialized") | 13 | return nil, fmt.Errorf("Docker client has not been initialized") | ||
| @@ -19,23 +19,7 @@ func ListContainerImages() ([]ContainerImage, error) { | |||||
| 19 | return nil, err | 19 | return nil, err | ||
| 20 | } | 20 | } | ||
| 21 | 21 | ||||
| 22 | - | // Populate the ContainerImage slice | 22 | + | return images, nil |
| 23 | - | var containerImages []ContainerImage | |||
| 24 | - | for _, image := range images { | |||
| 25 | - | containerImages = append(containerImages, ContainerImage{ | |||
| 26 | - | ID: image.ID, | |||
| 27 | - | ParentID: image.ParentID, | |||
| 28 | - | RepoTags: image.RepoTags, | |||
| 29 | - | RepoDigests: image.RepoDigests, | |||
| 30 | - | Created: image.Created, | |||
| 31 | - | Size: image.Size, | |||
| 32 | - | SharedSize: image.SharedSize, | |||
| 33 | - | Labels: image.Labels, | |||
| 34 | - | Containers: image.Containers, | |||
| 35 | - | }) | |||
| 36 | - | } | |||
| 37 | - | ||||
| 38 | - | return containerImages, nil | |||
| 39 | } | 23 | } | ||
| 40 | func DeleteContainerImage(imageID string) error { | 24 | func DeleteContainerImage(imageID string) error { | ||
| 41 | // Check if the Docker client has been initialized | 25 | // Check if the Docker client has been initialized | ||