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

Look for "docker-compose" in path #152

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,53 @@
*/
package com.palantir.docker.compose.execution;

import static java.util.Arrays.asList;

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;

public class DockerCommandLocations {
import org.immutables.value.Value;

@Value.Immutable
public abstract 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();

private final List<String> possiblePaths;
private final String exeName;
private final boolean lookInPath;

public DockerCommandLocations(String exeName, boolean lookInPath, String... possiblePaths) {
this.exeName = exeName;
this.lookInPath = lookInPath;
this.possiblePaths = asList(possiblePaths);
protected abstract String executableName();
protected abstract List<Optional<String>> additionalSearchLocations();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

additionalSearchLocations() has to be a list of optionals since System.getenv might or might not return a null value.
Alternatively, immutables optionally (sic) allows null values for plain lists but that would require an update to the latest version and wouldn't make a big difference imho.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not filter out the nulls?

Copy link
Contributor Author

@diamondT diamondT Feb 16, 2017

Choose a reason for hiding this comment

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

it's the immutables generated method that does not allow nulls. having something like this

DOCKER_COMPOSE_LOCATIONS = DockerCommandLocations.builder()
            .executableName("docker-compose")
            .additionalSearchLocations(Arrays.asList(
                System.getenv("DOCKER_COMPOSE_LOCATION"),
                "/usr/local/bin/docker-compose",
                "/usr/bin/docker-compose"
            )).build();

for an unset DOCKER_COMPOSE_LOCATION would fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

Stream.of(...).filter(o -> o != null).collect(toList()) ought to do it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed. however, having a signature of List<String> would fail in runtime (under certain circumstances). the Stream.of() solution is fine but the caller would have to know that they should call this method using this specific construct and not by using any other plain list.
putting the Optional in the signature ensures null-safety at compile time, right?

@Value.Default protected List<String> pathLocations() {
String envpath = System.getenv("PATH");
return envpath == null ? Collections.emptyList() : Arrays.asList(envpath.split(File.pathSeparator));
}

@Value.Derived
public Optional<String> preferredLocation() {
List<String> envPath = (StringUtils.isNotBlank(exeName) && lookInPath)
? Arrays.asList(System.getenv("PATH").split(File.pathSeparator)) :
Collections.emptyList();

return Stream.concat(
envPath.stream().map(path -> Paths.get(path, exeName).toAbsolutePath().toString()),
possiblePaths.stream())
pathLocations().stream().map(path -> Paths.get(path, executableName()).toAbsolutePath().toString()),
additionalSearchLocations().stream().filter(Optional::isPresent).map(Optional::get))
.filter(IS_NOT_NULL)
.filter(FILE_EXISTS)
.findFirst();
}

@Override
public String toString() {
return "DockerCommandLocations{possiblePaths=" + possiblePaths + "}";
return "DockerCommandLocations{additionalSearchLocations=" + additionalSearchLocations() + "}";
}

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

/**
* convert an array of strings to a list of optionals. used to make calling `additionalSearchLocations' a bit friendlier.
*/
public static List<Optional<String>> optionals(String ... strings) {
return Arrays.stream(strings).map(Optional::ofNullable).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
public abstract class DockerComposeExecutable implements Executable {
private static final Logger log = LoggerFactory.getLogger(DockerComposeExecutable.class);

private static final DockerCommandLocations DOCKER_COMPOSE_LOCATIONS = new DockerCommandLocations(
"docker-compose", true,
System.getenv("DOCKER_COMPOSE_LOCATION"),
"/usr/local/bin/docker-compose",
"/usr/bin/docker-compose"
);
private static final DockerCommandLocations DOCKER_COMPOSE_LOCATIONS = DockerCommandLocations.builder()
.executableName("docker-compose")
.additionalSearchLocations(DockerCommandLocations.optionals(
System.getenv("DOCKER_COMPOSE_LOCATION"),
"/usr/local/bin/docker-compose",
"/usr/bin/docker-compose"
)).build();

@Value.Parameter protected abstract DockerComposeFiles dockerComposeFiles();
@Value.Parameter protected abstract DockerConfiguration dockerConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
public abstract class DockerExecutable implements Executable {
private static final Logger log = LoggerFactory.getLogger(DockerExecutable.class);

private static final DockerCommandLocations DOCKER_LOCATIONS = new DockerCommandLocations(
"docker", true,
System.getenv("DOCKER_LOCATION"),
"/usr/local/bin/docker",
"/usr/bin/docker"
);
private static final DockerCommandLocations DOCKER_LOCATIONS = DockerCommandLocations.builder()
.executableName("docker")
.additionalSearchLocations(DockerCommandLocations.optionals(
System.getenv("DOCKER_LOCATION"),
"/usr/local/bin/docker",
"/usr/bin/docker"
)).build();

@Value.Parameter protected abstract DockerConfiguration dockerConfiguration();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -28,44 +32,101 @@
public class DockerCommandLocationsShould {
private static final String badLocation = "file/that/does/not/exist";
private static final String otherBadLocation = "another/file/that/does/not/exist";
private static final String someLocationInPath = "/folder/not/containing/docker-cmd";
private static final String someOtherLocationInPath = "/folder/not/containing/docker-cmd";
private static final String FILENAME = "docker-compose.yml";


@Rule public TemporaryFolder folder = new TemporaryFolder();

private String goodLocation;
private String goodLocationParent;

@Before
public void setup() throws IOException {
goodLocation = folder.newFile("docker-compose.yml").getAbsolutePath();
File file = folder.newFile(FILENAME);
goodLocation = file.getAbsolutePath();
goodLocationParent = file.getParent();
}

@Test public void
provide_the_first_docker_command_location_if_it_exists() {
DockerCommandLocations dockerCommandLocations = new DockerCommandLocations(
null, false,
badLocation,
goodLocation,
otherBadLocation);
DockerCommandLocations dockerCommandLocations = DockerCommandLocations.builder()
.executableName(FILENAME)
.pathLocations(Collections.emptyList())
.additionalSearchLocations(DockerCommandLocations.optionals(
badLocation,
goodLocation,
otherBadLocation
)).build();

assertThat(dockerCommandLocations.preferredLocation().get(),
is(goodLocation));
}

@Test public void
skip_paths_from_environment_variables_that_are_unset() {
DockerCommandLocations dockerCommandLocations = new DockerCommandLocations(
null, false,
System.getenv("AN_UNSET_DOCKER_COMPOSE_PATH"),
goodLocation);
DockerCommandLocations dockerCommandLocations = DockerCommandLocations.builder()
.executableName(FILENAME)
.pathLocations(Collections.emptyList())
.additionalSearchLocations(DockerCommandLocations.optionals(
System.getenv("AN_UNSET_DOCKER_COMPOSE_PATH"),
goodLocation
)).build();

assertThat(dockerCommandLocations.preferredLocation().get(),
is(goodLocation));
}

@Test public void
have_no_preferred_path_when_all_possible_paths_are_all_invalid() {
DockerCommandLocations dockerCommandLocations = new DockerCommandLocations(
null, false,
badLocation);
DockerCommandLocations dockerCommandLocations = DockerCommandLocations.builder()
.executableName(FILENAME)
.pathLocations(Collections.emptyList())
.additionalSearchLocations(DockerCommandLocations.optionals(
badLocation
)).build();

assertThat(dockerCommandLocations.preferredLocation(),
is(empty()));
}

@Test public void
discover_docker_command_in_envpath() {
DockerCommandLocations dockerCommandLocations = DockerCommandLocations.builder()
.executableName(FILENAME)
//path will contain directories, not full path to docker cmd
.pathLocations(Arrays.asList(someLocationInPath, someOtherLocationInPath, goodLocationParent))
.additionalSearchLocations(DockerCommandLocations.optionals(
badLocation,
otherBadLocation
)).build();

assertThat(dockerCommandLocations.preferredLocation().get(),
is(goodLocation)); //but result will be the full path to docker cmd
}

@Test public void
have_no_preferred_path_when_all_possible_paths_are_all_invalid_with_envpath() {
DockerCommandLocations dockerCommandLocations = DockerCommandLocations.builder()
.executableName(FILENAME)
.pathLocations(Arrays.asList(someLocationInPath, someOtherLocationInPath))
.additionalSearchLocations(DockerCommandLocations.optionals(
badLocation,
otherBadLocation
)).build();

assertThat(dockerCommandLocations.preferredLocation(),
is(empty()));
}

@Test public void
dont_fail_for_empty_lists() {
DockerCommandLocations dockerCommandLocations = DockerCommandLocations.builder()
.executableName(FILENAME)
.pathLocations(Collections.emptyList())
.additionalSearchLocations(Collections.emptyList())
.build();

assertThat(dockerCommandLocations.preferredLocation(),
is(empty()));
Expand Down