detached d515416

Volumes and Env inputs can be empty

commit
d51541699613f6951265b05247ad758cc38e5346
author
bnema · · 2023-10-20 14:12 UTC
parent
257faae
signature
unsigned
4 files · +48 -15
M internal/templating/render/cmdparams.go +10 -0
@@ -13,10 +13,20 @@ func FromInputsToCmdParams(inputs map[string]string, a *app.App) (docker.Contain
1313 volumeStr := inputs["volumes"]
1414 volumeSlice := strings.Split(volumeStr, ",")
1515
16+ // If there is no volume value we just resturn an empty strings ""
17+ if len(volumeSlice) == 1 && volumeSlice[0] == "" {
18+ volumeSlice = []string{}
19+ }
20+
1621 // Retreive the content of the textarea environment_variables and create a slice of strings
1722 environmentStr := inputs["environment_variables"]
1823 environmentSlice := strings.Split(environmentStr, "\n")
1924
25+ // If there is no environment value we just resturn an empty strings ""
26+ if len(environmentSlice) == 1 && environmentSlice[0] == "" {
27+ environmentSlice = []string{}
28+ }
29+
2030 // Ports input parsing
2131 portMappingsStr := inputs["ports"]
2232 portMappingsSliceRaw := strings.Split(portMappingsStr, ",")
@@ -13,10 +13,20 @@ func FromInputsToCmdParams(inputs map[string]string, a *app.App) (docker.Contain
13 volumeStr := inputs["volumes"]13 volumeStr := inputs["volumes"]
14 volumeSlice := strings.Split(volumeStr, ",")14 volumeSlice := strings.Split(volumeStr, ",")
15 15
16+ // If there is no volume value we just resturn an empty strings ""
17+ if len(volumeSlice) == 1 && volumeSlice[0] == "" {
18+ volumeSlice = []string{}
19+ }
20+
16 // Retreive the content of the textarea environment_variables and create a slice of strings21 // Retreive the content of the textarea environment_variables and create a slice of strings
17 environmentStr := inputs["environment_variables"]22 environmentStr := inputs["environment_variables"]
18 environmentSlice := strings.Split(environmentStr, "\n")23 environmentSlice := strings.Split(environmentStr, "\n")
19 24
25+ // If there is no environment value we just resturn an empty strings ""
26+ if len(environmentSlice) == 1 && environmentSlice[0] == "" {
27+ environmentSlice = []string{}
28+ }
29+
20 // Ports input parsing30 // Ports input parsing
21 portMappingsStr := inputs["ports"]31 portMappingsStr := inputs["ports"]
22 portMappingsSliceRaw := strings.Split(portMappingsStr, ",")32 portMappingsSliceRaw := strings.Split(portMappingsStr, ",")
M pkg/docker/client.go +29 -9
@@ -3,9 +3,10 @@ package docker
33 import (
44 "context"
55 "fmt"
6- "log"
76 "os"
7+ "time"
88
9+ "github.com/docker/docker/api/types"
910 "github.com/docker/docker/client"
1011 )
1112
@@ -18,7 +19,10 @@ type Config struct {
1819 PodmanEnable bool
1920 }
2021
21-var dockerCli *client.Client
22+var (
23+ dockerCli *client.Client
24+ currentConfig *Config
25+)
2226
2327 // DockerClient implements the ContainerEngineClient interface for Docker
2428 type DockerClient struct{}
@@ -31,30 +35,46 @@ func (d *DockerClient) InitializeClient(config *Config) error {
3135
3236 // Check if the socket file exists if not return an error
3337 if _, err := os.Stat(config.Sock); os.IsNotExist(err) {
34- return fmt.Errorf("Sock file does not exist: %s", config.Sock)
38+ return err
3539 }
3640
3741 // Initialize Docker client
3842 cli, err := client.NewClientWithOpts(
3943 client.WithAPIVersionNegotiation(),
40- client.WithHost("unix://"+config.Sock), // Prepend "unix://" to the Unix socket path
44+ client.WithHost("unix://"+config.Sock),
4145 )
4246 if err != nil {
43- log.Printf("Error initializing Docker client: %s", err)
44- return err
47+ return fmt.Errorf("Error initializing Docker client: %s", err)
4548 }
4649 dockerCli = cli
50+ currentConfig = config
4751 return nil
4852 }
4953
54+// CheckIfInitialized checks if the Docker client is initialized
5055 func CheckIfInitialized() error {
56+ // Check if there is a socket file
57+ if currentConfig == nil || currentConfig.Sock == "" {
58+ return fmt.Errorf("Docker client is not initialized or configuration is missing")
59+ }
60+
61+ socketPath := currentConfig.Sock // Use the package-level variable
62+ if _, err := os.Stat(socketPath); os.IsNotExist(err) {
63+ return fmt.Errorf("Sock file does not exist: %s", socketPath)
64+ }
65+
66+ // Then we check if the dockerCli is nil
5167 if dockerCli == nil {
5268 return fmt.Errorf("Docker client is not initialized")
5369 }
54- // ping the Docker daemon to check if it's running
55- _, err := dockerCli.Ping(context.Background())
70+
71+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
72+ defer cancel()
73+
74+ // More stringent check: try to list containers
75+ _, err := dockerCli.ContainerList(ctx, types.ContainerListOptions{})
5676 if err != nil {
57- return fmt.Errorf("Docker client is not initialized")
77+ return fmt.Errorf("Cannot connect to Docker daemon: %s", err)
5878 }
5979 return nil
6080 }
@@ -3,9 +3,10 @@ package docker
3 import (3 import (
4 "context"4 "context"
5 "fmt"5 "fmt"
6- "log"
7 "os"6 "os"
7+ "time"
8 8
9+ "github.com/docker/docker/api/types"
9 "github.com/docker/docker/client"10 "github.com/docker/docker/client"
10 )11 )
11 12
@@ -18,7 +19,10 @@ type Config struct {
18 PodmanEnable bool19 PodmanEnable bool
19 }20 }
20 21
21-var dockerCli *client.Client22+var (
23+ dockerCli *client.Client
24+ currentConfig *Config
25+)
22 26
23 // DockerClient implements the ContainerEngineClient interface for Docker27 // DockerClient implements the ContainerEngineClient interface for Docker
24 type DockerClient struct{}28 type DockerClient struct{}
@@ -31,30 +35,46 @@ func (d *DockerClient) InitializeClient(config *Config) error {
31 35
32 // Check if the socket file exists if not return an error36 // Check if the socket file exists if not return an error
33 if _, err := os.Stat(config.Sock); os.IsNotExist(err) {37 if _, err := os.Stat(config.Sock); os.IsNotExist(err) {
34- return fmt.Errorf("Sock file does not exist: %s", config.Sock)38+ return err
35 }39 }
36 40
37 // Initialize Docker client41 // Initialize Docker client
38 cli, err := client.NewClientWithOpts(42 cli, err := client.NewClientWithOpts(
39 client.WithAPIVersionNegotiation(),43 client.WithAPIVersionNegotiation(),
40- client.WithHost("unix://"+config.Sock), // Prepend "unix://" to the Unix socket path44+ client.WithHost("unix://"+config.Sock),
41 )45 )
42 if err != nil {46 if err != nil {
43- log.Printf("Error initializing Docker client: %s", err)47+ return fmt.Errorf("Error initializing Docker client: %s", err)
44- return err
45 }48 }
46 dockerCli = cli49 dockerCli = cli
50+ currentConfig = config
47 return nil51 return nil
48 }52 }
49 53
54+// CheckIfInitialized checks if the Docker client is initialized
50 func CheckIfInitialized() error {55 func CheckIfInitialized() error {
56+ // Check if there is a socket file
57+ if currentConfig == nil || currentConfig.Sock == "" {
58+ return fmt.Errorf("Docker client is not initialized or configuration is missing")
59+ }
60+
61+ socketPath := currentConfig.Sock // Use the package-level variable
62+ if _, err := os.Stat(socketPath); os.IsNotExist(err) {
63+ return fmt.Errorf("Sock file does not exist: %s", socketPath)
64+ }
65+
66+ // Then we check if the dockerCli is nil
51 if dockerCli == nil {67 if dockerCli == nil {
52 return fmt.Errorf("Docker client is not initialized")68 return fmt.Errorf("Docker client is not initialized")
53 }69 }
54- // ping the Docker daemon to check if it's running70+
55- _, err := dockerCli.Ping(context.Background())71+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
72+ defer cancel()
73+
74+ // More stringent check: try to list containers
75+ _, err := dockerCli.ContainerList(ctx, types.ContainerListOptions{})
56 if err != nil {76 if err != nil {
57- return fmt.Errorf("Docker client is not initialized")77+ return fmt.Errorf("Cannot connect to Docker daemon: %s", err)
58 }78 }
59 return nil79 return nil
60 }80 }
M pkg/docker/containers.go +8 -5
@@ -88,12 +88,19 @@ func ContainerCommandParamsToConfig(cmdParams ContainerCommandParams) (*containe
8888 // ListRunningContainers lists all running containers
8989 func ListRunningContainers() ([]types.Container, error) {
9090 // Check if the Docker client has been initialized
91- CheckIfInitialized()
91+ err := CheckIfInitialized()
92+ if err != nil {
93+ return nil, err
94+ }
9295 // List containers using the Docker client:
9396 containers, err := dockerCli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
9497 if err != nil {
9598 return nil, err
9699 }
100+ // Check if the list is empty
101+ if len(containers) == 0 {
102+ return nil, fmt.Errorf("No containers found")
103+ }
97104 return containers, nil
98105 }
99106
@@ -106,7 +113,6 @@ func StopContainer(containerID string) error {
106113
107114 // StopContainerGracefully stops a container by sending a SIGTERM and waiting for it to stop
108115 func StopContainerGracefully(containerID string, timeoutDuration time.Duration) (bool, error) {
109-
110116 // Start by sending a SIGTERM
111117 err := dockerCli.ContainerKill(context.Background(), containerID, "SIGTERM")
112118 if err != nil {
@@ -253,7 +259,6 @@ func CreateContainer(cmdParams ContainerCommandParams) (string, error) {
253259
254260 // GetContainerInfo returns information about a container
255261 func GetContainerInfo(containerID string) (types.ContainerJSON, error) {
256-
257262 // Get container info using the Docker client
258263 containerInfo, err := dockerCli.ContainerInspect(context.Background(), containerID)
259264 if err != nil {
@@ -300,7 +305,6 @@ func UpdateContainerConfig(containerID string, newConfig *container.Config, newH
300305
301306 // GetContainerLogs returns the logs of a container
302307 func GetContainerLogs(containerID string) (string, error) {
303-
304308 // Get container logs using the Docker client
305309 containerLogs, err := dockerCli.ContainerLogs(context.Background(), containerID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
306310 if err != nil {
@@ -317,7 +321,6 @@ func GetContainerLogs(containerID string) (string, error) {
317321
318322 // CheckContainerStatus checks if a container with the given name exists and is running
319323 func CheckContainerStatus(containerName string) (bool, bool, error) {
320-
321324 // List all containers
322325 containers, err := ListRunningContainers()
323326 if err != nil {
@@ -88,12 +88,19 @@ func ContainerCommandParamsToConfig(cmdParams ContainerCommandParams) (*containe
88 // ListRunningContainers lists all running containers88 // ListRunningContainers lists all running containers
89 func ListRunningContainers() ([]types.Container, error) {89 func ListRunningContainers() ([]types.Container, error) {
90 // Check if the Docker client has been initialized90 // Check if the Docker client has been initialized
91- CheckIfInitialized()91+ err := CheckIfInitialized()
92+ if err != nil {
93+ return nil, err
94+ }
92 // List containers using the Docker client:95 // List containers using the Docker client:
93 containers, err := dockerCli.ContainerList(context.Background(), types.ContainerListOptions{All: true})96 containers, err := dockerCli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
94 if err != nil {97 if err != nil {
95 return nil, err98 return nil, err
96 }99 }
100+ // Check if the list is empty
101+ if len(containers) == 0 {
102+ return nil, fmt.Errorf("No containers found")
103+ }
97 return containers, nil104 return containers, nil
98 }105 }
99 106
@@ -106,7 +113,6 @@ func StopContainer(containerID string) error {
106 113
107 // StopContainerGracefully stops a container by sending a SIGTERM and waiting for it to stop114 // StopContainerGracefully stops a container by sending a SIGTERM and waiting for it to stop
108 func StopContainerGracefully(containerID string, timeoutDuration time.Duration) (bool, error) {115 func StopContainerGracefully(containerID string, timeoutDuration time.Duration) (bool, error) {
109-
110 // Start by sending a SIGTERM116 // Start by sending a SIGTERM
111 err := dockerCli.ContainerKill(context.Background(), containerID, "SIGTERM")117 err := dockerCli.ContainerKill(context.Background(), containerID, "SIGTERM")
112 if err != nil {118 if err != nil {
@@ -253,7 +259,6 @@ func CreateContainer(cmdParams ContainerCommandParams) (string, error) {
253 259
254 // GetContainerInfo returns information about a container260 // GetContainerInfo returns information about a container
255 func GetContainerInfo(containerID string) (types.ContainerJSON, error) {261 func GetContainerInfo(containerID string) (types.ContainerJSON, error) {
256-
257 // Get container info using the Docker client262 // Get container info using the Docker client
258 containerInfo, err := dockerCli.ContainerInspect(context.Background(), containerID)263 containerInfo, err := dockerCli.ContainerInspect(context.Background(), containerID)
259 if err != nil {264 if err != nil {
@@ -300,7 +305,6 @@ func UpdateContainerConfig(containerID string, newConfig *container.Config, newH
300 305
301 // GetContainerLogs returns the logs of a container306 // GetContainerLogs returns the logs of a container
302 func GetContainerLogs(containerID string) (string, error) {307 func GetContainerLogs(containerID string) (string, error) {
303-
304 // Get container logs using the Docker client308 // Get container logs using the Docker client
305 containerLogs, err := dockerCli.ContainerLogs(context.Background(), containerID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})309 containerLogs, err := dockerCli.ContainerLogs(context.Background(), containerID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
306 if err != nil {310 if err != nil {
@@ -317,7 +321,6 @@ func GetContainerLogs(containerID string) (string, error) {
317 321
318 // CheckContainerStatus checks if a container with the given name exists and is running322 // CheckContainerStatus checks if a container with the given name exists and is running
319 func CheckContainerStatus(containerName string) (bool, bool, error) {323 func CheckContainerStatus(containerName string) (bool, bool, error) {
320-
321 // List all containers324 // List all containers
322 containers, err := ListRunningContainers()325 containers, err := ListRunningContainers()
323 if err != nil {326 if err != nil {
M pkg/htmx/htmlfragments.go +1 -1
@@ -6,7 +6,7 @@ import (
66
77 "github.com/PuerkitoBio/goquery"
88 "github.com/bnema/gordon/internal/app"
9- "github.com/bnema/gordon/internal/templates/render"
9+ "github.com/bnema/gordon/internal/templating/render"
1010 )
1111
1212 // GetHTMLFragmentByID returns the HTML fragment with the specified id
@@ -6,7 +6,7 @@ import (
6 6
7 "github.com/PuerkitoBio/goquery"7 "github.com/PuerkitoBio/goquery"
8 "github.com/bnema/gordon/internal/app"8 "github.com/bnema/gordon/internal/app"
9- "github.com/bnema/gordon/internal/templates/render"9+ "github.com/bnema/gordon/internal/templating/render"
10 )10 )
11 11
12 // GetHTMLFragmentByID returns the HTML fragment with the specified id12 // GetHTMLFragmentByID returns the HTML fragment with the specified id

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