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 @@ -18,26 +18,40 @@
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.Stream;
import org.apache.commons.lang3.StringUtils;

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

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

public DockerCommandLocations(String... possiblePaths) {
public DockerCommandLocations(String exeName, boolean lookInPath, String... possiblePaths) {
Copy link
Contributor

@hpryce hpryce Feb 14, 2017

Choose a reason for hiding this comment

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

I really like this idea, it should reduce the amount of configuration necessary for people to use the library. However, this constructor now seems to be getting quite complicated and I don't think it's possible to write a test for finding the executable in the path with how this is written.

This seems like a really good candidate for using Immutables (https://immutables.github.io/), which we use elsewhere in the codebase - see DockerComposeExecArgument for a simple example. Using immutables you could do something like this:

@Value.Immutable
public abstract class DockerCommandLocations {

  protected String executableName();
  protected List<String> additionalSearchLocations();

  @Value.Default
   protected List<String> path() {
      // read path environment variable
   }

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

  public static Builder builder();

}

Then, using the builder, it is possible to write unit tests by setting the path @Value.Default value so we can ensure this continues working in the future.

this.exeName = exeName;
this.lookInPath = lookInPath;
this.possiblePaths = asList(possiblePaths);
}

public Optional<String> preferredLocation() {

return possiblePaths.stream()
.filter(IS_NOT_NULL)
.filter(FILE_EXISTS)
.findFirst();
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())
.filter(IS_NOT_NULL)
.filter(FILE_EXISTS)
.findFirst();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void setup() throws IOException {
@Test public void
provide_the_first_docker_command_location_if_it_exists() {
DockerCommandLocations dockerCommandLocations = new DockerCommandLocations(
null, false,
badLocation,
goodLocation,
otherBadLocation);
Expand All @@ -52,6 +53,7 @@ public void setup() throws IOException {
@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);

Expand All @@ -62,6 +64,7 @@ public void setup() throws IOException {
@Test public void
have_no_preferred_path_when_all_possible_paths_are_all_invalid() {
DockerCommandLocations dockerCommandLocations = new DockerCommandLocations(
null, false,
badLocation);

assertThat(dockerCommandLocations.preferredLocation(),
Expand Down