Refactor docker package file struct
- commit
- a25e03c106eeb3b304113a2d1408bd2e29b874a5
- author
- bnema · · 2023-09-21 12:44 UTC
- parent
- 407cd43
- signature
- unsigned
6 files · +76 -56
M .air.toml +1 -1
| @@ -5,7 +5,7 @@ tmp_dir = "tmp" | |||
| 5 | 5 | [build] | |
| 6 | 6 | args_bin = [] | |
| 7 | 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 | 9 | delay = 0 | |
| 10 | 10 | exclude_dir = ["assets", "tmp", "vendor", "testdata", "node_modules"] | |
| 11 | 11 | 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 = 0 | 9 | 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 APP | 1 | + | # 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 { | |||
| 20 | 20 | 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 | 36 | type ContainerEngineClient interface { | |
| 24 | 37 | InitializeClient(*Config) error | |
| 25 | 38 | // Add other methods that both Docker and Podman should implement | |
| @@ -29,7 +42,6 @@ type Config struct { | |||
| 29 | 42 | PodmanEnable bool | |
| 30 | 43 | } | |
| 31 | 44 | ||
| 32 | - | var engineClient ContainerEngineClient | |
| 33 | 45 | var dockerCli *client.Client | |
| 34 | 46 | ||
| 35 | 47 | // DockerClient implements the ContainerEngineClient interface for Docker | |
| @@ -53,37 +65,6 @@ func (d *DockerClient) InitializeClient(config *Config) error { | |||
| 53 | 65 | 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 | 68 | func ListRunningContainers() ([]types.Container, error) { | |
| 88 | 69 | // Check if the Docker client has been initialized | |
| 89 | 70 | if dockerCli == nil { | |
| @@ -98,18 +79,3 @@ func ListRunningContainers() ([]types.Container, error) { | |||
| 98 | 79 | ||
| 99 | 80 | 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 | - | } | |
| @@ -20,6 +20,19 @@ type ContainerImage struct { | |||||
| 20 | Containers int64 | 20 | 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) error | 37 | InitializeClient(*Config) error | ||
| 25 | // Add other methods that both Docker and Podman should implement | 38 | // Add other methods that both Docker and Podman should implement | ||
| @@ -29,7 +42,6 @@ type Config struct { | |||||
| 29 | PodmanEnable bool | 42 | PodmanEnable bool | ||
| 30 | } | 43 | } | ||
| 31 | 44 | ||||
| 32 | - | var engineClient ContainerEngineClient | |||
| 33 | var dockerCli *client.Client | 45 | var dockerCli *client.Client | ||
| 34 | 46 | ||||
| 35 | // DockerClient implements the ContainerEngineClient interface for Docker | 47 | // DockerClient implements the ContainerEngineClient interface for Docker | ||
| @@ -53,37 +65,6 @@ func (d *DockerClient) InitializeClient(config *Config) error { | |||||
| 53 | return nil | 65 | 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 initialized | 69 | // 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, nil | 80 | 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 | + | } | |||