-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from nvervelle/issue-41-option-to-download-opa
Add an option to download OPA during gradle build
- Loading branch information
Showing
9 changed files
with
301 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.bisnode.opa; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.channels.Channels; | ||
import java.nio.channels.ReadableByteChannel; | ||
|
||
import com.bisnode.opa.configuration.OpaPlatform; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
public abstract class DownloadOpaTask extends DefaultTask { | ||
|
||
public static final String TASK_BASE_NAME = "downloadOpa"; | ||
|
||
@Input | ||
public abstract Property<String> getVersion(); | ||
|
||
@OutputFile | ||
public abstract RegularFileProperty getOutputFile(); | ||
|
||
public DownloadOpaTask() { | ||
setGroup("opa"); | ||
setDescription("Download OPA"); | ||
} | ||
|
||
@TaskAction | ||
public void downloadOpa() throws IOException { | ||
final String downloadUrl = OpaPlatform.getPlatform().getDownloadUrl(getVersion().get()); | ||
getLogger().info("Retrieving OPA executable from " + downloadUrl); | ||
final File targetFile = getOutputFile().getAsFile().get(); | ||
getLogger().info("Saving OPA executable to " + targetFile.getAbsolutePath()); | ||
try (FileOutputStream output = new FileOutputStream(targetFile); | ||
ReadableByteChannel input = Channels.newChannel(new URL(downloadUrl).openStream())) { | ||
output.getChannel().transferFrom(input, 0, Long.MAX_VALUE); | ||
} | ||
if (!targetFile.setReadable(true) || !targetFile.setExecutable(true)) { | ||
throw new IllegalStateException("Unable to set permissions on OPA executable"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/com/bisnode/opa/configuration/ExecutableMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.bisnode.opa.configuration; | ||
|
||
public enum ExecutableMode { | ||
LOCAL, DOWNLOAD | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/bisnode/opa/configuration/OpaConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/bisnode/opa/configuration/OpaPlatform.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.bisnode.opa.configuration; | ||
|
||
import java.nio.file.Path; | ||
|
||
public enum OpaPlatform { | ||
|
||
MAC_OS_AMD64("darwin_amd64", ""), | ||
LINUX_AMD64("linux_amd64_static", ""), | ||
WINDOWS_AMD64("windows_amd64", ".exe"); | ||
|
||
private final String platformQualifier; | ||
private final String executableExtension; | ||
|
||
OpaPlatform(final String platformQualifier, final String executableExtension) { | ||
this.platformQualifier = platformQualifier; | ||
this.executableExtension = executableExtension; | ||
} | ||
|
||
public String getDownloadUrl(final String opaVersion) { | ||
return String.format("https://openpolicyagent.org/downloads/v%s/opa_%s%s", | ||
opaVersion, | ||
platformQualifier, | ||
executableExtension); | ||
} | ||
|
||
public Path getExecutablePath(final Path rootPath, final String version) { | ||
return rootPath.resolve("opa").resolve(version).resolve(String.format("opa%s", executableExtension)); | ||
} | ||
|
||
public static OpaPlatform getPlatform() { | ||
final String osName = System.getProperty("os.name"); | ||
final String osArch = System.getProperty("os.arch"); | ||
if (osName.contains("win")) { | ||
if (osArch.equals("amd64")) { | ||
return WINDOWS_AMD64; | ||
} | ||
} else if (osName.contains("Mac")) { | ||
if (osArch.equals("x86_64")) { | ||
return MAC_OS_AMD64; | ||
} | ||
} else if (osName.contains("Linux")) { | ||
if (osArch.equals("amd64")) { | ||
return LINUX_AMD64; | ||
} | ||
} | ||
throw new IllegalStateException(String.format("Unsupported combination of OS/arch: %s/%s", | ||
osName, | ||
osArch)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.