-
Notifications
You must be signed in to change notification settings - Fork 90
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
Look for "docker-compose" in path #152
Conversation
|
||
public DockerCommandLocations(String... possiblePaths) { | ||
public DockerCommandLocations(String exeName, boolean lookInPath, String... possiblePaths) { |
There was a problem hiding this comment.
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.
There was a problem hiding this 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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 Cool, thanks for the update. |
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
orDOCKER_COMPOSE_LOCATION
) if the executable(s) already exists in path, even if not in one of the well-known locations.