Skip to content

Commit b174458

Browse files
committed
Initialise first version code
0 parents  commit b174458

File tree

10 files changed

+206
-0
lines changed

10 files changed

+206
-0
lines changed

.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
AUTH_SECRET=your_secret
2+
AUTH_EXPIRES_IN=1d
3+
4+
MAIL_HOST=smtp.mailtrap.io
5+
MAIL_PORT=2525
6+
MAIL_USER=your_user
7+
MAIL_PASS=your_pass
8+

.gitignore

Whitespace-only changes.

Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM golang:1.22.4 AS builder
2+
3+
ARG VERSION=dev
4+
WORKDIR /app
5+
6+
7+
# {{ENV}}
8+
ENV AUTH_SECRET {AUTH_SECRET}
9+
ENV AUTH_EXPIRES_IN {AUTH_EXPIRES_IN}
10+
ENV MAIL_HOST {MAIL_HOST}
11+
ENV MAIL_PORT {MAIL_PORT}
12+
ENV MAIL_USER {MAIL_USER}
13+
ENV MAIL_PASS {MAIL_PASS}
14+
# {{END ENV}}
15+
16+
RUN go install github.com/swaggo/swag/cmd/swag@latest
17+
COPY . .
18+
ENV PATH=$PATH:/go/bin
19+
RUN make build
20+
CMD ["./strive_backend"]

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build:
2+
go build
3+
cp docker-env ~/go/bin/docker-env

docker-env

2.37 MB
Binary file not shown.

docker.template

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM golang:1.22.4 AS builder
2+
3+
ARG VERSION=dev
4+
WORKDIR /app
5+
# {{ENV}}
6+
ENV AUTH_SECRET {AUTH_SECRET}
7+
ENV AUTH_EXPIRES_IN {AUTH_EXPIRES_IN}
8+
ENV MAIL_HOST {MAIL_HOST}
9+
ENV MAIL_PORT {MAIL_PORT}
10+
ENV MAIL_USER {MAIL_USER}
11+
ENV MAIL_PASS {MAIL_PASS}
12+
# {{END ENV}}
13+
14+
RUN go install github.com/swaggo/swag/cmd/swag@latest
15+
COPY . .
16+
ENV PATH=$PATH:/go/bin
17+
RUN make build
18+
CMD ["./strive_backend"] .

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/SRV332003/docker-env
2+
3+
go 1.22.4

handlers/createTemplate.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package handlers
2+
3+
import "os"
4+
5+
func CreateTemplate(dockerfile string) error {
6+
// open dockerfile in read mode
7+
file, err := os.OpenFile(dockerfile, os.O_RDONLY, 0644)
8+
if err != nil {
9+
return err
10+
}
11+
defer file.Close()
12+
13+
// create a new file in write mode
14+
newFile, err := os.Create("Docker.template")
15+
if err != nil {
16+
return err
17+
}
18+
defer newFile.Close()
19+
20+
// copy the content of dockerfile to newFile
21+
_, err = file.WriteTo(newFile)
22+
if err != nil {
23+
return err
24+
}
25+
26+
return nil
27+
28+
}

handlers/updateEnvs.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package handlers
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"regexp"
8+
"strings"
9+
)
10+
11+
func UpdateEnvs(envfile string, Dockerfile string) error {
12+
// open envfile in read mode
13+
file, err := os.OpenFile(envfile, os.O_RDONLY, 0644)
14+
if err != nil {
15+
return err
16+
}
17+
18+
// read the content of envfile
19+
fileinfo, err := file.Stat()
20+
content := make([]byte, fileinfo.Size())
21+
_, err = file.Read(content)
22+
if err != nil {
23+
return err
24+
}
25+
26+
// parse key value pairs from envfile
27+
envs := strings.Split(string(content), "\n")
28+
envKeys := make([]string, 0)
29+
for _, env := range envs {
30+
if env != "" || env != "\n" || env[0] != '#' {
31+
keyValue := strings.Split(env, "=")
32+
if len(keyValue) != 2 {
33+
continue
34+
}
35+
36+
line := "ENV " + keyValue[0] + " {" + keyValue[0] + "}"
37+
38+
envKeys = append(envKeys, line)
39+
}
40+
}
41+
42+
// open Dockerfile in read & write mode
43+
dockerfile, err := os.OpenFile(Dockerfile, os.O_RDWR, 0644)
44+
if err != nil {
45+
return err
46+
}
47+
48+
// search for {{.ENV}} in Dockerfile
49+
fileinfo, err = dockerfile.Stat()
50+
content = make([]byte, fileinfo.Size())
51+
_, err = dockerfile.Read(content)
52+
if err != nil {
53+
return err
54+
}
55+
56+
// prepare keys to replace {{.ENV}} in Dockerfile
57+
replaceText := strings.Join(envKeys, "\n")
58+
replaceText = replaceText + "\n"
59+
replaceText = "\n\n# {{ENV}}\n" + replaceText + "# {{END ENV}}\n\n"
60+
61+
// search for {{.ENV}} in Dockerfile
62+
63+
re := regexp.MustCompile(`\n*#\s*{{ENV}}(\D|\d)*#\s*{{END ENV}}\n*`)
64+
match := string(re.Find(content))
65+
66+
if match == "" {
67+
log.Println("{{.ENV}} not used previously in Dockerfile")
68+
re = regexp.MustCompile(`\n*#\s*{{ENV}}`)
69+
match = string(re.Find(content))
70+
fmt.Println(match)
71+
if match == "" {
72+
return fmt.Errorf("{{.ENV}} not found in Dockerfile")
73+
}
74+
}
75+
76+
// replace {{.ENV}} with key value pairs from envfile
77+
newContent := strings.Replace(string(content), match, replaceText, 1)
78+
79+
// clear the contents of the Dockerfile
80+
err = dockerfile.Truncate(0)
81+
if err != nil {
82+
return err
83+
}
84+
85+
// write the new content to Dockerfile
86+
_, err = dockerfile.WriteAt([]byte(newContent), 0)
87+
if err != nil {
88+
return err
89+
}
90+
91+
return nil
92+
93+
}

main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/SRV332003/docker-env/handlers"
8+
)
9+
10+
func main() {
11+
// get cli arguments
12+
args := os.Args
13+
if len(args) > 2 {
14+
fmt.Println("Usage: docker-env <envfile> <Dockerfile>")
15+
fmt.Println("Default values are .env and Dockerfile")
16+
17+
os.Exit(1)
18+
}
19+
if len(args) == 1 {
20+
args = append(args, ".env", "Dockerfile")
21+
}
22+
23+
if len(args) == 2 {
24+
args = append(args, "Dockerfile")
25+
}
26+
27+
fmt.Println("Creating Dockerfile template")
28+
err := handlers.UpdateEnvs(args[1], args[2])
29+
if err != nil {
30+
fmt.Println(err)
31+
}
32+
33+
}

0 commit comments

Comments
 (0)