From a9e354b8b094c5b03bb319368ded467b7f4ed7ae Mon Sep 17 00:00:00 2001 From: Radim Loskot Date: Mon, 1 Jul 2019 17:40:41 +0200 Subject: [PATCH] feat(runImage): allow environment variables using config --- .soos.json | 6 +++++- soos.go | 29 +++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.soos.json b/.soos.json index bd3f12b..29ba60e 100644 --- a/.soos.json +++ b/.soos.json @@ -6,5 +6,9 @@ ], "ExposePorts": [ "3000:3000" - ] + ], + "EnvVariables": { + "BUILD_VERSION": "1.2.3", + "BUILD_ENV": "test" + } } diff --git a/soos.go b/soos.go index e17bc13..8cb5478 100644 --- a/soos.go +++ b/soos.go @@ -17,9 +17,10 @@ import ( // Configuration : represent .soos.json structure type Configuration struct { - ImageName string - ExposePorts []string - HashFiles []string + ImageName string + ExposePorts []string + HashFiles []string + EnvVariables map[string]string } // DefaultConfig : base for .soos.json @@ -146,22 +147,30 @@ func cwd() string { } func runImage(imageNameWithTag string) { - - args := []string{"run"} - user, userErr := user.Current() if userErr != nil { log.Fatal(userErr) } + dockerRunOptions := []string{"--rm", "-u", user.Uid + ":" + user.Gid, "-v", cwd() + ":/build/app"} + if len(getConfig().ExposePorts) != 0 { - exposePortsArg := "-p" + getConfig().ExposePorts[0] - args = append([]string{"run", "--rm", "-u", user.Uid + ":" + user.Gid, exposePortsArg, "-v", cwd() + ":/build/app", imageNameWithTag}, os.Args[1:]...) - } else { + for _, exposePort := range getConfig().ExposePorts { + dockerRunOptions = append(dockerRunOptions, "-p"+exposePort) + } + } - args = append([]string{"run", "--rm", "-u", user.Uid + ":" + user.Gid, "-v", cwd() + ":/build/app", imageNameWithTag}, os.Args[1:]...) + if len(getConfig().EnvVariables) != 0 { + for envVarName, envVarValue := range getConfig().EnvVariables { + dockerRunOptions = append(dockerRunOptions, "-e", envVarName+"="+envVarValue) + } } + args := []string{"run"} + args = append(args, dockerRunOptions...) + args = append(args, imageNameWithTag) + args = append(args, os.Args[1:]...) + cmd := exec.Command("docker", args...) var out bytes.Buffer