-
Notifications
You must be signed in to change notification settings - Fork 90
Feature/look in path #175
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
base: master
Are you sure you want to change the base?
Feature/look in path #175
Changes from 6 commits
195d9af
ab078a6
a12076d
35ab870
252a88e
0a61921
d39fb31
19791dd
a734f16
fa10060
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,75 @@ | ||
| /* | ||
| * 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 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() { | ||
|
||
| 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() { | ||
|
||
| 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() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. You could remove the It might even be worth moving |
||
| 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 |
|---|---|---|
|
|
@@ -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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| .locationOverride(System.getenv("DOCKER_COMPOSE_LOCATION")) | ||
| .build(); | ||
| String pathToUse = commandLocator.getLocation(); | ||
| log.debug("Using docker-compose found at " + pathToUse); | ||
|
|
||
| return pathToUse; | ||
| } | ||
|
|
||
|
|
||
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.
Are we keeping or removing the License? It's removed here but kept in the following file.
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.
All files should have the Apache licence. I'll fix the ones that had it removed.