Skip to content

Commit

Permalink
Merge pull request #16 from ITman1/master
Browse files Browse the repository at this point in the history
feat(runImage): allow environment variables using config
  • Loading branch information
elmariofredo authored Jul 2, 2019
2 parents cd5567e + a9e354b commit 7ab3db9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
6 changes: 5 additions & 1 deletion .soos.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
],
"ExposePorts": [
"3000:3000"
]
],
"EnvVariables": {
"BUILD_VERSION": "1.2.3",
"BUILD_ENV": "test"
}
}
29 changes: 19 additions & 10 deletions soos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7ab3db9

Please sign in to comment.