Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibiliy with Windows fixing the /bin/sh problem. #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.gradle
.idea
.settings
.project
.classpath
rbss-devops-plugin.iml
build/
rbss-platform-deploy-gradle-plugin.iml
/bin/
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
}

group 'io.advantageous.gradle'
version '0.1.7'
version '0.1.8'

apply plugin: 'maven'
apply plugin: 'signing'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.advantageous.gradle.docker

import org.apache.tools.ant.taskdefs.condition.Os

class DockerContainer {

private final String name
Expand Down Expand Up @@ -105,14 +107,14 @@ class DockerContainer {
}

if (env.size() > 0) {
def delimiter = Os.isFamily(Os.FAMILY_WINDOWS) ? "" : "'";
env.entrySet().stream().forEach { entry ->
builder.append(" --env=")
.append("'")
.append(delimiter)
.append(entry.key.toString().toUpperCase())
.append('=')
.append(entry.value)
.append("'")

.append(delimiter)
}
}

Expand All @@ -136,5 +138,4 @@ class DockerContainer {

builder.toString()
}

}
16 changes: 12 additions & 4 deletions src/main/groovy/io/advantageous/gradle/docker/DockerUtils.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.advantageous.gradle.docker

import org.apache.tools.ant.taskdefs.condition.Os

class DockerUtils {

static Map<String, String> getDockerEnv() {
Expand All @@ -14,7 +16,7 @@ class DockerUtils {
.findAll { it.startsWith('export') }
.collect { it.replace('export', '').replace('"', '').trim() }
.collect { it.split('=') }
.collectEntries { [it[0], it[1]] }
.collectEntries { [it[0], it[1]]}
}
}
}
Expand All @@ -40,12 +42,18 @@ class DockerUtils {
runCommand("docker", "build", "-t", dockerCoordinates(namespace, projectName, projectVersion), "build/")
}

static runCommand(String command) {

static prepareArgs(String command, boolean runningOnWindows) {
if (!runningOnWindows)
return ["/bin/sh", "-c", "$command"]
else
return ["cmd", "/c", "$command"]
}

static runCommand(String command) {
println("Running command $command")

String[] args = ["/bin/sh", "-c", "$command"]
def runningOnWindows = Os.isFamily(Os.FAMILY_WINDOWS);
String[] args = prepareArgs(command, runningOnWindows);

def stringBuilder = new StringBuilder()
def processBuilder = new ProcessBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.advantageous.gradle.docker

import org.apache.tools.ant.taskdefs.condition.Os
import org.junit.Test
import org.testng.Assert

class DockerContainerTest {

@Test
void testRunCommand() throws Exception {
void testRunCommand_nonWin() throws Exception {

def socat = new DockerContainer("socat")
.containerName("docker-http")
Expand All @@ -15,7 +16,10 @@ class DockerContainerTest {
.image("sequenceiq/socat").env("foo":"bar")
.runCommand()

Assert.assertEquals(
"docker run -d -p 2375:2375 --volume=/var/run/docker.sock:/var/run/docker.sock --env='FOO=bar' --name=docker-http sequenceiq/socat", socat)
def expected = Os.isFamily(Os.FAMILY_WINDOWS) ?
"docker run -d -p 2375:2375 --volume=/var/run/docker.sock:/var/run/docker.sock --env=FOO=bar --name=docker-http sequenceiq/socat":
"docker run -d -p 2375:2375 --volume=/var/run/docker.sock:/var/run/docker.sock --env='FOO=bar' --name=docker-http sequenceiq/socat" ;

Assert.assertEquals(expected, socat)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.advantageous.gradle.docker

import org.junit.Test
import org.testng.Assert

class DockerUtilsTest {
final SAMPLE = "docker run -d -p 2375:2375 --volume=/var/run/docker.sock:/var/run/docker.sock --env='FOO=bar' --name=docker-http sequenceiq/socat"

@Test
void prepareArgs_windows() throws Exception {
def actualWin = DockerUtils.prepareArgs(SAMPLE, true)
Assert.assertEquals("cmd", actualWin.get(0))
Assert.assertEquals("/c", actualWin.get(1))
Assert.assertEquals(SAMPLE, actualWin.get(2))
}

@Test
void prepareArgs_linux() throws Exception {
def actualLinux = DockerUtils.prepareArgs(SAMPLE, false)
Assert.assertEquals("/bin/sh", actualLinux.get(0))
Assert.assertEquals("-c", actualLinux.get(1))
Assert.assertEquals(SAMPLE, actualLinux.get(2))
}
}