Skip to content

Commit fbaffde

Browse files
committed
feat: adding a jinja templated docker env file
1 parent b0469d2 commit fbaffde

File tree

4 files changed

+95
-79
lines changed

4 files changed

+95
-79
lines changed

.tool-versions

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# golang 1.23.2
2+
golang 1.20.2

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ latestTag=$(git describe --tags)
66
echo "latest tag: $latestTag"
77

88
# Build the Go program, injecting the latest tag into the version variable
9+
echo go build -ldflags "-X main.version=$latestTag" -o jinjafier
910
go build -ldflags "-X main.version=$latestTag" -o jinjafier
1011

jinjafier.go

Lines changed: 89 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,100 @@
11
package main
22

33
import (
4-
"bufio"
5-
"fmt"
6-
"io/ioutil"
7-
"os"
8-
"regexp"
9-
"strings"
10-
11-
"gopkg.in/yaml.v3"
4+
"bufio"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"regexp"
9+
"strings"
10+
11+
"gopkg.in/yaml.v3"
1212
)
1313

1414
var version = "dev"
1515

1616
func main() {
1717

18-
if os.Args[1] == "-v" {
19-
fmt.Println("Jinjafier version:", version)
20-
os.Exit(0)
18+
if len(os.Args) == 2 && os.Args[1] == "-v" {
19+
fmt.Println("Jinjafier version:", version)
20+
os.Exit(0)
21+
}
22+
23+
if len(os.Args) != 2 {
24+
fmt.Println("Usage: jinjafier <java_properties_file>")
25+
os.Exit(1)
26+
}
27+
28+
filename := os.Args[1]
29+
file, err := os.Open(filename)
30+
if err != nil {
31+
fmt.Println(err)
32+
os.Exit(1)
33+
}
34+
defer file.Close()
35+
36+
scanner := bufio.NewScanner(file)
37+
jinjaTemplate := ""
38+
envTemplate := ""
39+
yamlMap := make(map[string]string)
40+
41+
for scanner.Scan() {
42+
line := scanner.Text()
43+
if strings.HasPrefix(line, "#") {
44+
// Add comments to both templates
45+
jinjaTemplate += line + "\n"
46+
envTemplate += line + "\n"
47+
} else if strings.Contains(line, "=") {
48+
split := strings.SplitN(line, "=", 2) // Split at the first "=" only
49+
key := split[0]
50+
value := split[1]
51+
52+
// Convert key to lowercase and replace dots and camel case with underscores
53+
re := regexp.MustCompile("([a-z0-9])([A-Z])")
54+
key = re.ReplaceAllString(key, "${1}_${2}")
55+
key = strings.ReplaceAll(key, ".", "_")
56+
key = strings.ReplaceAll(key, "-", "_")
57+
key = strings.ToUpper(key)
58+
59+
// Add to jinja template
60+
jinjaTemplate += fmt.Sprintf("%s={{ %s }}\n", split[0], key)
61+
62+
// Add to env template
63+
envTemplate += fmt.Sprintf("%s={{ %s }}\n", key, key)
64+
65+
// Add to yaml map
66+
yamlMap[key] = value
67+
} else {
68+
// Add non-comment, non-key-value lines to the Jinja template
69+
jinjaTemplate += line + "\n"
2170
}
22-
23-
if len(os.Args) != 2 {
24-
fmt.Println("Usage: jinjafier <java_properties_file>")
25-
os.Exit(1)
26-
}
27-
28-
filename := os.Args[1]
29-
file, err := os.Open(filename)
30-
if err != nil {
31-
fmt.Println(err)
32-
os.Exit(1)
33-
}
34-
defer file.Close()
35-
36-
scanner := bufio.NewScanner(file)
37-
jinjaTemplate := ""
38-
yamlMap := make(map[string]string)
39-
40-
for scanner.Scan() {
41-
line := scanner.Text()
42-
if strings.HasPrefix(line, "#") {
43-
jinjaTemplate += line + "\n"
44-
} else if strings.Contains(line, "=") {
45-
split := strings.SplitN(line, "=", 2) // Split at the first "=" only
46-
key := split[0]
47-
value := split[1]
48-
49-
// Convert key to lowercase and replace dots and camel case with underscores
50-
re := regexp.MustCompile("([a-z0-9])([A-Z])")
51-
key = re.ReplaceAllString(key, "${1}_${2}")
52-
key = strings.ReplaceAll(key, ".", "_")
53-
key = strings.ReplaceAll(key, "-", "_")
54-
key = strings.ToUpper(key)
55-
56-
// Add to jinja template
57-
jinjaTemplate += fmt.Sprintf("%s={{ %s }}\n", split[0], key)
58-
59-
// Add to yaml map
60-
yamlMap[key] = value
61-
} else {
62-
jinjaTemplate += line + "\n"
63-
}
64-
}
65-
66-
// Write jinja template to file
67-
err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.j2"), []byte(jinjaTemplate), 0644)
68-
if err != nil {
69-
fmt.Println(err)
70-
os.Exit(1)
71-
}
72-
73-
// Convert yaml map to yaml
74-
yamlData, err := yaml.Marshal(&yamlMap)
75-
if err != nil {
76-
fmt.Println(err)
77-
os.Exit(1)
78-
}
79-
80-
// Write yaml to file
81-
err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.yml"), yamlData, 0644)
82-
if err != nil {
83-
fmt.Println(err)
84-
os.Exit(1)
85-
}
71+
}
72+
73+
// Write jinja template to file
74+
err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.j2"), []byte(jinjaTemplate), 0644)
75+
if err != nil {
76+
fmt.Println(err)
77+
os.Exit(1)
78+
}
79+
80+
// Write env template to file
81+
err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.env.j2"), []byte(envTemplate), 0644)
82+
if err != nil {
83+
fmt.Println(err)
84+
os.Exit(1)
85+
}
86+
87+
// Convert yaml map to yaml
88+
yamlData, err := yaml.Marshal(&yamlMap)
89+
if err != nil {
90+
fmt.Println(err)
91+
os.Exit(1)
92+
}
93+
94+
// Write yaml to file
95+
err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.yml"), yamlData, 0644)
96+
if err != nil {
97+
fmt.Println(err)
98+
os.Exit(1)
99+
}
86100
}
87-

test.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ go build -o jinjafier
77
./jinjafier example.properties
88

99
# Check if example.properties.j2 and example.properties.yml have changed
10-
git diff --quiet -- example.properties.j2 example.properties.yml
10+
git diff --quiet -- example.properties.j2 example.properties.yml example.properties.env.j2
1111

1212
# If git diff returns a non-zero exit code, the files have changed
1313
if [ $? -ne 0 ]; then
14-
echo "FATAL: example.properties.j2 or example.properties.yml have changed"
14+
echo "FATAL: example.properties.j2 or example.properties.yml or example.properties.env.j2 have changed"
1515
exit 1
1616
else
17-
echo "No changes in example.properties.j2 or example.properties.yml"
17+
echo "No changes in example.properties.j2 or example.properties.yml or example.properties.env.j2"
1818
fi
1919

0 commit comments

Comments
 (0)