detached c6b1346

Refactor docker package file struct

commit
c6b1346302795bf7290dbd2314af10efb3c1c8ca
author
bnema · · 2023-09-21 12:44 UTC
parent
6950cb5
signature
unsigned
6 files · +76 -56
M .air.toml +1 -1
@@ -5,7 +5,7 @@ tmp_dir = "tmp"
55 [build]
66 args_bin = []
77 bin = "./tmp/main"
8- cmd = "go build -o ./tmp/main cmd/main.go"
8+ cmd = "go build -o ./tmp/main cmd/srv/main.go"
99 delay = 0
1010 exclude_dir = ["assets", "tmp", "vendor", "testdata", "node_modules"]
1111 exclude_file = []
@@ -5,7 +5,7 @@ tmp_dir = "tmp"
5 [build]5 [build]
6 args_bin = []6 args_bin = []
7 bin = "./tmp/main"7 bin = "./tmp/main"
8- cmd = "go build -o ./tmp/main cmd/main.go"8+ cmd = "go build -o ./tmp/main cmd/srv/main.go"
9 delay = 09 delay = 0
10 exclude_dir = ["assets", "tmp", "vendor", "testdata", "node_modules"]10 exclude_dir = ["assets", "tmp", "vendor", "testdata", "node_modules"]
11 exclude_file = []11 exclude_file = []
M .env.example +1 -8
@@ -1,8 +1,1 @@
1-VERSION= VERSION OF THE APP
2-ENV=EITHER PRODUCTION OR DEVELOPMENT
3-LOG_LEVEL= DEBUG, INFO, WARN, ERROR
4-PORT= DEFAULT 1323
5-ABSOLUTE_PATH= ABSOLUTE INSTALL PATH OF THE APP
6-TOP_DOMAIN= EXAMPLE.COM
7-GORDON_DOMAIN= GORDON.EXAMPLE.COM
8-ADMIN_EMAIL= EMAIL ADDRESS OF THE ADMIN⏎̸
1+# its empty here⏎̸
@@ -1,8 +1,1 @@
1-VERSION= VERSION OF THE APP1+# its empty here⏎̸
2-ENV=EITHER PRODUCTION OR DEVELOPMENT
3-LOG_LEVEL= DEBUG, INFO, WARN, ERROR
4-PORT= DEFAULT 1323
5-ABSOLUTE_PATH= ABSOLUTE INSTALL PATH OF THE APP
6-TOP_DOMAIN= EXAMPLE.COM
7-GORDON_DOMAIN= GORDON.EXAMPLE.COM
8-ADMIN_EMAIL= EMAIL ADDRESS OF THE ADMIN⏎̸
A cmd/cli/main.go +8 -0
@@ -0,0 +1,8 @@
1+package main
2+
3+import "fmt"
4+
5+func main() {
6+ fmt.Println("Hello World")
7+
8+}
@@ -0,0 +1,8 @@
1+package main
2+
3+import "fmt"
4+
5+func main() {
6+ fmt.Println("Hello World")
7+
8+}
R cmd/main.go → cmd/srv/main.go +0 -0 renamed, 100% similar

renamed, 100% similar

R pkg/utils/docker/initclient.go → pkg/utils/docker/client.go +13 -47 renamed, 57% similar
@@ -20,6 +20,19 @@ type ContainerImage struct {
2020 Containers int64
2121 }
2222
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+
2336 type ContainerEngineClient interface {
2437 InitializeClient(*Config) error
2538 // Add other methods that both Docker and Podman should implement
@@ -29,7 +42,6 @@ type Config struct {
2942 PodmanEnable bool
3043 }
3144
32-var engineClient ContainerEngineClient
3345 var dockerCli *client.Client
3446
3547 // DockerClient implements the ContainerEngineClient interface for Docker
@@ -53,37 +65,6 @@ func (d *DockerClient) InitializeClient(config *Config) error {
5365 return nil
5466 }
5567
56-func ListContainerImages() ([]ContainerImage, error) {
57- // Check if the Docker client has been initialized
58- if dockerCli == nil {
59- return nil, fmt.Errorf("Docker client has not been initialized")
60- }
61-
62- // List images using the Docker client
63- images, err := dockerCli.ImageList(context.Background(), types.ImageListOptions{})
64- if err != nil {
65- return nil, err
66- }
67-
68- // Populate the ContainerImage slice
69- var containerImages []ContainerImage
70- for _, image := range images {
71- containerImages = append(containerImages, ContainerImage{
72- ID: image.ID,
73- ParentID: image.ParentID,
74- RepoTags: image.RepoTags,
75- RepoDigests: image.RepoDigests,
76- Created: image.Created,
77- Size: image.Size,
78- SharedSize: image.SharedSize,
79- Labels: image.Labels,
80- Containers: image.Containers,
81- })
82- }
83-
84- return containerImages, nil
85-}
86-
8768 func ListRunningContainers() ([]types.Container, error) {
8869 // Check if the Docker client has been initialized
8970 if dockerCli == nil {
@@ -98,18 +79,3 @@ func ListRunningContainers() ([]types.Container, error) {
9879
9980 return containers, nil
10081 }
101-
102-func DeleteContainerImage(imageID string) error {
103- // Check if the Docker client has been initialized
104- if dockerCli == nil {
105- return fmt.Errorf("Docker client has not been initialized")
106- }
107-
108- // Delete the image using the Docker client
109- _, err := dockerCli.ImageRemove(context.Background(), imageID, types.ImageRemoveOptions{})
110- if err != nil {
111- return err
112- }
113-
114- return nil
115-}
@@ -20,6 +20,19 @@ type ContainerImage struct {
20 Containers int6420 Containers int64
21 }21 }
22 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+
23 type ContainerEngineClient interface {36 type ContainerEngineClient interface {
24 InitializeClient(*Config) error37 InitializeClient(*Config) error
25 // Add other methods that both Docker and Podman should implement38 // Add other methods that both Docker and Podman should implement
@@ -29,7 +42,6 @@ type Config struct {
29 PodmanEnable bool42 PodmanEnable bool
30 }43 }
31 44
32-var engineClient ContainerEngineClient
33 var dockerCli *client.Client45 var dockerCli *client.Client
34 46
35 // DockerClient implements the ContainerEngineClient interface for Docker47 // DockerClient implements the ContainerEngineClient interface for Docker
@@ -53,37 +65,6 @@ func (d *DockerClient) InitializeClient(config *Config) error {
53 return nil65 return nil
54 }66 }
55 67
56-func ListContainerImages() ([]ContainerImage, error) {
57- // Check if the Docker client has been initialized
58- if dockerCli == nil {
59- return nil, fmt.Errorf("Docker client has not been initialized")
60- }
61-
62- // List images using the Docker client
63- images, err := dockerCli.ImageList(context.Background(), types.ImageListOptions{})
64- if err != nil {
65- return nil, err
66- }
67-
68- // Populate the ContainerImage slice
69- var containerImages []ContainerImage
70- for _, image := range images {
71- containerImages = append(containerImages, ContainerImage{
72- ID: image.ID,
73- ParentID: image.ParentID,
74- RepoTags: image.RepoTags,
75- RepoDigests: image.RepoDigests,
76- Created: image.Created,
77- Size: image.Size,
78- SharedSize: image.SharedSize,
79- Labels: image.Labels,
80- Containers: image.Containers,
81- })
82- }
83-
84- return containerImages, nil
85-}
86-
87 func ListRunningContainers() ([]types.Container, error) {68 func ListRunningContainers() ([]types.Container, error) {
88 // Check if the Docker client has been initialized69 // Check if the Docker client has been initialized
89 if dockerCli == nil {70 if dockerCli == nil {
@@ -98,18 +79,3 @@ func ListRunningContainers() ([]types.Container, error) {
98 79
99 return containers, nil80 return containers, nil
100 }81 }
101-
102-func DeleteContainerImage(imageID string) error {
103- // Check if the Docker client has been initialized
104- if dockerCli == nil {
105- return fmt.Errorf("Docker client has not been initialized")
106- }
107-
108- // Delete the image using the Docker client
109- _, err := dockerCli.ImageRemove(context.Background(), imageID, types.ImageRemoveOptions{})
110- if err != nil {
111- return err
112- }
113-
114- return nil
115-}
A pkg/utils/docker/images.go +53 -0
@@ -0,0 +1,53 @@
1+package docker
2+
3+import (
4+ "context"
5+ "fmt"
6+
7+ "github.com/docker/docker/api/types"
8+)
9+
10+func ListContainerImages() ([]ContainerImage, 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 images using the Docker client
17+ images, err := dockerCli.ImageList(context.Background(), types.ImageListOptions{})
18+ if err != nil {
19+ return nil, err
20+ }
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
39+}
40+func DeleteContainerImage(imageID string) error {
41+ // Check if the Docker client has been initialized
42+ if dockerCli == nil {
43+ return fmt.Errorf("Docker client has not been initialized")
44+ }
45+
46+ // Delete the image using the Docker client
47+ _, err := dockerCli.ImageRemove(context.Background(), imageID, types.ImageRemoveOptions{})
48+ if err != nil {
49+ return err
50+ }
51+
52+ return nil
53+}
@@ -0,0 +1,53 @@
1+package docker
2+
3+import (
4+ "context"
5+ "fmt"
6+
7+ "github.com/docker/docker/api/types"
8+)
9+
10+func ListContainerImages() ([]ContainerImage, 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 images using the Docker client
17+ images, err := dockerCli.ImageList(context.Background(), types.ImageListOptions{})
18+ if err != nil {
19+ return nil, err
20+ }
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
39+}
40+func DeleteContainerImage(imageID string) error {
41+ // Check if the Docker client has been initialized
42+ if dockerCli == nil {
43+ return fmt.Errorf("Docker client has not been initialized")
44+ }
45+
46+ // Delete the image using the Docker client
47+ _, err := dockerCli.ImageRemove(context.Background(), imageID, types.ImageRemoveOptions{})
48+ if err != nil {
49+ return err
50+ }
51+
52+ return nil
53+}

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