Skip to content
Open
2 changes: 1 addition & 1 deletion .baseline/findbugs/excludeFilter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<!-- Ignore the following bug patterns in test code -->
<!-- (i.e., classes ending in 'Test' or 'Tests', and inner classes of same) -->
<Match>
<Class name="~.*\.*Tests?(\$.*)?" />
<Class name="~.*\.*(Test|Tests|Should)?(\$.*)?" />
<Or>
<Bug pattern="NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"/> <!-- common in tests to have non-final variables instantiated in @Before methods, which FindBugs can't detect -->
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/> <!-- if a null is dereferenced, test will fail anyway. Plus assertNotNull() is often a bad pattern. -->
Expand Down
2 changes: 1 addition & 1 deletion docker-compose-rule-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies {
compile "com.google.guava:guava:$guavaVersion"
compile "joda-time:joda-time:$jodaVersion"
compile "com.github.zafarkhaja:java-semver:$javaSemverVersion"
compile "com.google.code.findbugs:jsr305:3.0.0"

compile 'com.jayway.awaitility:awaitility:1.6.5'

Expand All @@ -18,7 +19,6 @@ dependencies {
testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
testCompile "org.mockito:mockito-core:$mockitoVersion"
testCompile "com.github.tomakehurst:wiremock:2.0.6-beta"
testCompile "com.google.code.findbugs:jsr305:3.0.0"
testCompile "com.github.stefanbirkner:system-rules:1.16.1"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,75 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we keeping or removing the License? It's removed here but kept in the following file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All files should have the Apache licence. I'll fix the ones that had it removed.

*
* 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 java.util.Arrays.asList;
package com.palantir.docker.compose.execution;

import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.immutables.value.Value;

public class DockerCommandLocations {
private static final Predicate<String> IS_NOT_NULL = path -> path != null;
private static final Predicate<String> FILE_EXISTS = path -> new File(path).exists();
@Value.Immutable
public abstract class DockerCommandLocations {

private final List<String> possiblePaths;
private static final Pattern PATH_SPLITTER = Pattern.compile(File.pathSeparator);

public DockerCommandLocations(String... possiblePaths) {
this.possiblePaths = asList(possiblePaths);
@Value.Default
protected Map<String, String> env() {
return System.getenv();
}

public Optional<String> preferredLocation() {
@Value.Derived
protected String path() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work on Windows, where the path variable is "Path". You could add another if (path == null) path = env().get("Path"), or iterate through the env() map to find any key that equals "path" ignoring case, e.g.

        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"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this should now cover PATH, path and Path.

String path = env().get("PATH");
if (path == null) {
path = env().get("path");
}
if (path == null) {
throw new IllegalStateException("No path environment variable found");
}
return path;
}

return possiblePaths.stream()
.filter(IS_NOT_NULL)
.filter(FILE_EXISTS)
.findFirst();
@Value.Check
protected void pathIsNotEmpty() {
if (path().isEmpty()) {
throw new IllegalStateException("Path variable was empty");
}
}

@Override
public String toString() {
return "DockerCommandLocations{possiblePaths=" + possiblePaths + "}";
@Nullable
protected abstract String locationOverride();

@Value.Default
protected Stream<String> macSearchLocations() {
return Stream.of("/usr/local/bin", "/usr/bin");
}

private Stream<String> pathLocations() {
Stream<String> pathLocations = Stream.concat(PATH_SPLITTER.splitAsStream(path()), macSearchLocations());
if (locationOverride() == null) {
return pathLocations;
}
return Stream.concat(Stream.of(locationOverride()), pathLocations);
}

public Stream<Path> forCommand() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we returning Stream? They have use once semantics

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purely because it was extracted out the middle of a stream, I'll make it a list.

return pathLocations().map(p -> Paths.get(p));
}

public static DockerCommandLocations withOverride(String override) {
return ImmutableDockerCommandLocations.builder()
.locationOverride(override)
.build();
}

public static ImmutableDockerCommandLocations.Builder builder() {
return ImmutableDockerCommandLocations.builder();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.nio.file.Files;
import java.nio.file.Path;
import javax.annotation.Nullable;
import org.apache.commons.lang3.SystemUtils;
import org.immutables.value.Value;

@Value.Immutable
public abstract class DockerCommandLocator {

protected abstract String command();

@Value.Default
protected boolean isWindows() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning a constant to represent a default where the constant is framed as a question is somewhat misleading (I still don't know what the default is)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is to allow it to be overriden in tests, otherwise it should behave as a constant.

Would "useWindowsExecutableNaming" have been easier to understand?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isWindows is fine, but I'm starting to think that making it a default doesn't really make any sense. Whoever creates this builder will know at the time what environment they're in, and it feels odd to be like "I'm not going to specify if I'm running on windows because defaults"

Unless of course this is for backcompat and it's used in client code then nevermind.

return SystemUtils.IS_OS_WINDOWS;
}

@Value.Derived
protected String executableName() {
if (isWindows()) {
return command() + ".exe";
}
return command();
}

@Nullable
protected abstract String locationOverride();

@Value.Derived
protected DockerCommandLocations searchLocations() {
return DockerCommandLocations.withOverride(locationOverride());
}

public String getLocation() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously DOCKER_COMPOSE_LOCATION would be the path to the binary, e.g. /path/to/docker-compose, but the logic here requires it to point to the directory in which to find docker-compose, i.e. /path/to. I think the previous behaviour is more obvious/expected.

You could remove the searchLocations method, and do the check for the location override here instead, e..g

if (locationOverride() == null) {
  return DockerCommandLocations.pathLocations().stream()....;
} else {
  return locationOverride();
}

It might even be worth moving DockerCommandLocations.pathLocations() to this class, but that's just a design thing.

return searchLocations()
.forCommand()
.map(p -> p.resolve(executableName()))
.filter(Files::exists)
.findFirst()
.map(Path::toString)
.orElseThrow(() -> new IllegalStateException("Could not find " + command() + " in path"));
}

public static ImmutableDockerCommandLocator.Builder forCommand(String command) {
return ImmutableDockerCommandLocator.builder()
.command(command);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,12 @@
public abstract class DockerComposeExecutable implements Executable {
private static final Logger log = LoggerFactory.getLogger(DockerComposeExecutable.class);

private static final DockerCommandLocations DOCKER_COMPOSE_LOCATIONS = new DockerCommandLocations(
System.getenv("DOCKER_COMPOSE_LOCATION"),
"/usr/local/bin/docker-compose",
"/usr/bin/docker-compose"
);

private static String defaultDockerComposePath() {
String pathToUse = DOCKER_COMPOSE_LOCATIONS.preferredLocation()
.orElseThrow(() -> new IllegalStateException(
"Could not find docker-compose, looked in: " + DOCKER_COMPOSE_LOCATIONS));

DockerCommandLocator commandLocator = DockerCommandLocator.forCommand("docker-compose")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning a builder just to do this looks kinda meh, might as well do the entire builder from here and delete the forCommand method

.locationOverride(System.getenv("DOCKER_COMPOSE_LOCATION"))
.build();
String pathToUse = commandLocator.getLocation();
log.debug("Using docker-compose found at " + pathToUse);

return pathToUse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
public abstract class DockerExecutable implements Executable {
private static final Logger log = LoggerFactory.getLogger(DockerExecutable.class);

private static final DockerCommandLocations DOCKER_LOCATIONS = new DockerCommandLocations(
System.getenv("DOCKER_LOCATION"),
"/usr/local/bin/docker",
"/usr/bin/docker"
);

@Value.Parameter protected abstract DockerConfiguration dockerConfiguration();

@Override
Expand All @@ -41,12 +35,11 @@ public final String commandName() {

@Value.Derived
protected String dockerPath() {
String pathToUse = DOCKER_LOCATIONS.preferredLocation()
.orElseThrow(() -> new IllegalStateException(
"Could not find docker, looked in: " + DOCKER_LOCATIONS));

DockerCommandLocator commandLocator = DockerCommandLocator.forCommand("docker")
.locationOverride(System.getenv("DOCKER_LOCATION"))
.build();
String pathToUse = commandLocator.getLocation();
log.debug("Using docker found at " + pathToUse);

return pathToUse;
}

Expand Down
Loading