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

Conversation

diamondT
Copy link
Contributor

Adds directories found in PATH to the list of possible paths for docker executables.
It would be nice to work out-of-the-box, i.e. without needing a special env variable (DOCKER_LOCATION or DOCKER_COMPOSE_LOCATION) if the executable(s) already exists in path, even if not in one of the well-known locations.


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.

Copy link
Contributor

@hpryce hpryce left a comment

Choose a reason for hiding this comment

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

See comment on DockerCommandLocation

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?

@hpryce hpryce mentioned this pull request Mar 7, 2017
@hpryce
Copy link
Contributor

hpryce commented Mar 7, 2017

@diamondT sorry for the delay here. I've pulled these changes into #175 to include support for Windows, all going well we should be able to ship them soon.

I'm going to close this PR out as the linked one includes everything this does.

@hpryce hpryce closed this Mar 7, 2017
@diamondT
Copy link
Contributor Author

diamondT commented Mar 8, 2017

@hpryce Cool, thanks for the update.

@diamondT diamondT deleted the feature/search-docker-compose-in-path branch March 8, 2017 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants