1package cmdparams
2
3import (
4 "fmt"
5 "net/url"
6 "strings"
7
8 "github.com/bnema/gordon/internal/common"
9 "github.com/bnema/gordon/internal/server"
10 "github.com/bnema/gordon/pkg/docker"
11)
12
13// FromYAMLStructToCmdParams converts a YAMLContainerParams struct to a ContainerCommandParams struct
14func FromPayloadStructToCmdParams(ppl *common.DeployPayload, a *server.App, imageID string) (docker.ContainerCommandParams, error) {
15
16 // volume empty for now
17 volumeSlice := ParseVolumeSlice("")
18
19 environmentSlice := ParseEnvironmentSlice("")
20
21 // from ppl.TargetDomain, (eg. "https://test.example.com") get container_name (eg. "test"), get container_subdomain (eg. "test"), get container_domain (eg. "example.com") and get container_protocol (eg. "https")
22 // Parse TargetDomain to get the various components
23 parsedURL, err := url.Parse(ppl.TargetDomain)
24 if err != nil {
25 return docker.ContainerCommandParams{}, err
26 }
27
28 // Extract the subdomain and domain from the host
29 hostParts := strings.Split(parsedURL.Host, ".")
30 if len(hostParts) < 2 {
31 return docker.ContainerCommandParams{}, fmt.Errorf("invalid domain: %s", parsedURL.Host)
32 }
33
34 // Protocol (http/https), Container name, Container subdomain, and Container domain
35 protocol := parsedURL.Scheme
36 containerSubdomain := hostParts[0]
37 containerDomain := strings.Join(hostParts[1:], ".")
38
39 params := docker.ContainerCommandParams{
40 IsSSL: protocol == "https",
41 ContainerName: containerSubdomain,
42 ServiceName: containerSubdomain,
43 Domain: containerDomain,
44 ImageName: ppl.ImageName,
45 ImageID: imageID,
46 Restart: "always",
47 Volumes: volumeSlice,
48 Environment: environmentSlice,
49 Network: a.Config.ContainerEngine.Network,
50 TraefikEntryPoint: ppl.Port,
51 }
52
53 err = CreateTraefikLabels(¶ms, params.TraefikEntryPoint, a)
54 if err != nil {
55 return docker.ContainerCommandParams{}, fmt.Errorf("error creating Traefik labels: %w", err)
56 }
57
58 return params, nil
59}