Skip to content

Commit 7ab3db9

Browse files
Merge pull request #16 from ITman1/master
feat(runImage): allow environment variables using config
2 parents cd5567e + a9e354b commit 7ab3db9

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

.soos.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
],
77
"ExposePorts": [
88
"3000:3000"
9-
]
9+
],
10+
"EnvVariables": {
11+
"BUILD_VERSION": "1.2.3",
12+
"BUILD_ENV": "test"
13+
}
1014
}

soos.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import (
1717

1818
// Configuration : represent .soos.json structure
1919
type Configuration struct {
20-
ImageName string
21-
ExposePorts []string
22-
HashFiles []string
20+
ImageName string
21+
ExposePorts []string
22+
HashFiles []string
23+
EnvVariables map[string]string
2324
}
2425

2526
// DefaultConfig : base for .soos.json
@@ -146,22 +147,30 @@ func cwd() string {
146147
}
147148

148149
func runImage(imageNameWithTag string) {
149-
150-
args := []string{"run"}
151-
152150
user, userErr := user.Current()
153151
if userErr != nil {
154152
log.Fatal(userErr)
155153
}
156154

155+
dockerRunOptions := []string{"--rm", "-u", user.Uid + ":" + user.Gid, "-v", cwd() + ":/build/app"}
156+
157157
if len(getConfig().ExposePorts) != 0 {
158-
exposePortsArg := "-p" + getConfig().ExposePorts[0]
159-
args = append([]string{"run", "--rm", "-u", user.Uid + ":" + user.Gid, exposePortsArg, "-v", cwd() + ":/build/app", imageNameWithTag}, os.Args[1:]...)
160-
} else {
158+
for _, exposePort := range getConfig().ExposePorts {
159+
dockerRunOptions = append(dockerRunOptions, "-p"+exposePort)
160+
}
161+
}
161162

162-
args = append([]string{"run", "--rm", "-u", user.Uid + ":" + user.Gid, "-v", cwd() + ":/build/app", imageNameWithTag}, os.Args[1:]...)
163+
if len(getConfig().EnvVariables) != 0 {
164+
for envVarName, envVarValue := range getConfig().EnvVariables {
165+
dockerRunOptions = append(dockerRunOptions, "-e", envVarName+"="+envVarValue)
166+
}
163167
}
164168

169+
args := []string{"run"}
170+
args = append(args, dockerRunOptions...)
171+
args = append(args, imageNameWithTag)
172+
args = append(args, os.Args[1:]...)
173+
165174
cmd := exec.Command("docker", args...)
166175

167176
var out bytes.Buffer

0 commit comments

Comments
 (0)