Skip to content

Commit

Permalink
bugfix: runiac_container build arg should only be added to docker bui…
Browse files Browse the repository at this point in the history
…ld when value exists (#28)
  • Loading branch information
tiny-dancer committed May 14, 2021
1 parent d2bdc65 commit 5065792
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
25 changes: 19 additions & 6 deletions cmd/cli/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,11 @@ var deployCmd = &cobra.Command{
}

buildKit := "DOCKER_BUILDKIT=1"
containerTag := "sample"
containerTag := viper.GetString("project")

// check viper configuration if not set
if Container == "" && viper.GetString("container") != "" {
Container = viper.GetString("container")
}
cmdd := exec.Command("docker", "build", "-t", containerTag, "-f", ".runiac/Dockerfile")

cmdd := exec.Command("docker", "build", "-t", containerTag, "-f", ".runiac/Dockerfile", "--build-arg", fmt.Sprintf("RUNIAC_CONTAINER=%s", Container), ".")
cmdd.Args = append(cmdd.Args, getBuildArguments()...)

var stdoutBuf, stderrBuf bytes.Buffer

Expand Down Expand Up @@ -202,6 +199,22 @@ func checkInitialized() bool {
return InitAction()
}

func getBuildArguments() (args []string) {
// check viper configuration if not set
if Container == "" && viper.GetString("container") != "" {
Container = viper.GetString("container")
}

if Container != "" {
args = append(args, "--build-arg", fmt.Sprintf("RUNIAC_CONTAINER=%s", Container))
}

// must be last argument added for docker build current directory context
args = append(args, ".")

return
}

func getMachineName() (string, error) {
// This handles most *nix platforms
username := os.Getenv("USER")
Expand Down
25 changes: 21 additions & 4 deletions cmd/cli/cmd/deploy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import "testing"
import (
"github.com/stretchr/testify/require"
"testing"
)

func TestSanitizeMachinename(t *testing.T) {
tests := map[string]string{
Expand All @@ -15,8 +18,22 @@ func TestSanitizeMachinename(t *testing.T) {

for in, expected := range tests {
result := sanitizeMachineName(in)
if result != expected {
t.Errorf("sanitizeMachineName(\"%s\") = \"%s\"; want \"%s\"", in, result, expected)
}

require.Equal(t, expected, result, "sanitizeMachineName(\"%s\") = \"%s\"; want \"%s\"", in, result, expected)
}
}

func TestGetBuildArguments_ShouldSetBuildArgContainerOnlyWhenValueExists(t *testing.T) {
// if container is set, include in docker build. if not, do not include.
tests := map[string][]string{
"foobar": {"--build-arg", "RUNIAC_CONTAINER=foobar", "."},
"": {"."},
}

for in, expected := range tests {
Container = in

result := getBuildArguments()
require.Equal(t, expected, result)
}
}

0 comments on commit 5065792

Please sign in to comment.