-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Locate docker and docker-compose using the path variable.
This searches through the path variable's directories for the docker commands, instead of looking in predetermined OS-specific locations or requiring new environmental variables to be set. This also renames DockerCommandLocations to DockerCommandLocator, which better reflects what it is.
- Loading branch information
Sam Wright
committed
Mar 1, 2017
1 parent
040af72
commit d3390be
Showing
7 changed files
with
187 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 0 additions & 47 deletions
47
...rule-core/src/main/java/com/palantir/docker/compose/execution/DockerCommandLocations.java
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
...e-rule-core/src/main/java/com/palantir/docker/compose/execution/DockerCommandLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2016 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.palantir.docker.compose.execution; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.lang3.SystemUtils; | ||
|
||
public class DockerCommandLocator | ||
{ | ||
private static final Pattern PATH_SPLITTER = Pattern.compile(File.pathSeparator); | ||
|
||
private final String command; | ||
|
||
public DockerCommandLocator(String command) { | ||
this.command = command; | ||
} | ||
|
||
public String getLocation() { | ||
// Get path variable, ignoring the case of its name | ||
String path = getEnv().entrySet().stream() | ||
.filter(e -> e.getKey().equalsIgnoreCase("path")) | ||
.findFirst() | ||
.map(Map.Entry::getValue) | ||
.orElseThrow(() -> new IllegalStateException("Could not find path variable in env")); | ||
|
||
// The filename is the same as the command, except on Windows where it ends with ".exe" | ||
String filename = isWindows() ? command + ".exe" : command; | ||
|
||
// Look through the directories in path for the given command which must exist and be executable | ||
return PATH_SPLITTER.splitAsStream(path) | ||
.map(p -> Paths.get(p, filename)) | ||
.filter(Files::exists) | ||
.findFirst() | ||
.map(Path::toString) | ||
.orElseThrow(() -> new IllegalStateException("Could not find " + command + " in path")); | ||
} | ||
|
||
protected Map<String, String> getEnv() { | ||
return System.getenv(); | ||
} | ||
|
||
protected boolean isWindows() { | ||
return SystemUtils.IS_OS_WINDOWS; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "DockerCommandLocator{command=" + command + "}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
...ore/src/test/java/com/palantir/docker/compose/execution/DockerCommandLocationsShould.java
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
...-core/src/test/java/com/palantir/docker/compose/execution/DockerCommandLocatorShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright 2016 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.palantir.docker.compose.execution; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.spy; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
public class DockerCommandLocatorShould { | ||
private static final String command = "command"; | ||
private static final String windowsCommand = command + ".exe"; | ||
|
||
@Rule public TemporaryFolder folder = new TemporaryFolder(); | ||
|
||
@Rule public ExpectedException exception = ExpectedException.none(); | ||
|
||
private final DockerCommandLocator locator = spy(new DockerCommandLocator(command)); | ||
|
||
private final Map<String, String> env = new HashMap<>(); | ||
|
||
private Path emptyFolder; | ||
|
||
private Path firstFolder; | ||
|
||
private Path secondFolder; | ||
|
||
private String commandFile; | ||
|
||
private String windowsCommandFile; | ||
|
||
private String pathString; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
emptyFolder = folder.newFolder("empty").toPath(); | ||
firstFolder = folder.newFolder("first").toPath(); | ||
secondFolder = folder.newFolder("second").toPath(); | ||
|
||
commandFile = Files.createFile(firstFolder.resolve(command)).toString(); | ||
windowsCommandFile = Files.createFile(firstFolder.resolve(windowsCommand)).toString(); | ||
Files.createFile(secondFolder.resolve(command)); | ||
Files.createFile(secondFolder.resolve(windowsCommand)); | ||
|
||
pathString = Stream.of(emptyFolder, firstFolder, secondFolder) | ||
.map(Path::toString) | ||
.collect(Collectors.joining(File.pathSeparator)); | ||
|
||
doReturn(env).when(locator).getEnv(); | ||
} | ||
|
||
@Test public void | ||
provide_the_first_command_location() { | ||
env.put("path", pathString); | ||
doReturn(false).when(locator).isWindows(); | ||
assertThat(locator.getLocation(), is(commandFile)); | ||
} | ||
|
||
@Test public void | ||
provide_the_first_command_location_using_capitalised_path() { | ||
env.put("PATH", pathString); | ||
doReturn(false).when(locator).isWindows(); | ||
assertThat(locator.getLocation(), is(commandFile)); | ||
} | ||
|
||
@Test public void | ||
provide_the_first_command_location_on_windows() { | ||
env.put("path", pathString); | ||
doReturn(true).when(locator).isWindows(); | ||
assertThat(locator.getLocation(), is(windowsCommandFile)); | ||
} | ||
|
||
@Test public void | ||
fail_when_no_paths_contain_command() { | ||
env.put("path", emptyFolder.toString()); | ||
exception.expect(IllegalStateException.class); | ||
exception.expectMessage("Could not find " + command + " in path"); | ||
locator.getLocation(); | ||
} | ||
|
||
@Test public void | ||
fail_when_no_path_variable_is_set() { | ||
exception.expect(IllegalStateException.class); | ||
exception.expectMessage("Could not find path variable in env"); | ||
locator.getLocation(); | ||
} | ||
} |