detached 2451b8a

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 (
1010 "github.com/bnema/gordon/pkg/utils/docker"
1111 "github.com/bnema/gordon/pkg/utils/humanize"
1212 "github.com/bnema/gordon/pkg/utils/sanitize"
13+ "github.com/docker/docker/api/types"
1314 "github.com/labstack/echo/v4"
1415 )
1516
1617 var idMap = make(map[string]string)
1718
1819 type HumanReadableContainerImage struct {
19- docker.ContainerImage
20+ *types.ImageSummary
2021 ShortID string
2122 CreatedStr string
2223 SizeStr string
@@ -44,10 +45,10 @@ func ImageManagerHandler(c echo.Context, a *app.App) error {
4445 sizeStr := humanize.BytesToReadableSize(image.Size)
4546
4647 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,
5152 })
5253 }
5354 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.ContainerImage20+ *types.ImageSummary
20 ShortID string21 ShortID string
21 CreatedStr string22 CreatedStr string
22 SizeStr string23 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 @@
11 package docker
22
33 import (
4- "context"
54 "fmt"
65
7- "github.com/docker/docker/api/types"
86 "github.com/docker/docker/client"
97 )
108
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-
369 type ContainerEngineClient interface {
3710 InitializeClient(*Config) error
3811 // Add other methods that both Docker and Podman should implement
@@ -64,18 +37,3 @@ func (d *DockerClient) InitializeClient(config *Config) error {
6437 dockerCli = cli
6538 return nil
6639 }
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 docker1 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) error10 InitializeClient(*Config) error
38 // Add other methods that both Docker and Podman should implement11 // Add other methods that both Docker and Podman should implement
@@ -64,18 +37,3 @@ func (d *DockerClient) InitializeClient(config *Config) error {
64 dockerCli = cli37 dockerCli = cli
65 return nil38 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 (
77 "github.com/docker/docker/api/types"
88 )
99
10-func ListContainerImages() ([]ContainerImage, error) {
10+func ListContainerImages() ([]types.ImageSummary, error) {
1111 // Check if the Docker client has been initialized
1212 if dockerCli == nil {
1313 return nil, fmt.Errorf("Docker client has not been initialized")
@@ -19,23 +19,7 @@ func ListContainerImages() ([]ContainerImage, error) {
1919 return nil, err
2020 }
2121
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
3923 }
4024 func DeleteContainerImage(imageID string) error {
4125 // 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 initialized11 // 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, err19 return nil, err
20 }20 }
21 21
22- // Populate the ContainerImage slice22+ 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 initialized25 // Check if the Docker client has been initialized

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close