Skip to content

Commit

Permalink
--config-file added
Browse files Browse the repository at this point in the history
  • Loading branch information
munishchouhan committed Aug 11, 2023
1 parent b51bb5f commit c0d4042
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
implementation 'com.squareup.moshi:moshi-adapters:1.14.0'
implementation 'dev.failsafe:failsafe:3.1.0'
implementation 'org.apache.commons:commons-lang3:3.12.0'
testImplementation 'com.github.tomakehurst:wiremock:2.27.2'
annotationProcessor 'info.picocli:picocli-codegen:4.6.1'

testImplementation "org.codehaus.groovy:groovy:3.0.15"
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/java/io/seqera/wavelit/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.seqera.wave.api.SubmitContainerTokenResponse;
import io.seqera.wave.util.Packer;
import io.seqera.wavelit.exception.IllegalCliArgumentException;
import io.seqera.wavelit.util.ConfigFileProcessor;
import io.seqera.wavelit.util.CliVersionProvider;
import picocli.CommandLine;
import static org.apache.commons.lang3.StringUtils.isEmpty;
Expand Down Expand Up @@ -88,6 +89,9 @@ public class App implements Runnable {
@Option(names = {"--config-entrypoint"}, description = "Overwrite the default ENTRYPOINT of the image.")
private String entrypoint;

@Option(names = {"--config-file"}, description = "Configuration file in JSON format to overwrite the default configuration of the image.")
private String configFile;

private BuildContext buildContext;

private ContainerConfig containerConfig;
Expand Down Expand Up @@ -235,7 +239,17 @@ protected BuildContext prepareContext() {
}

protected ContainerConfig prepareConfig() {
final ContainerConfig result = new ContainerConfig();
ContainerConfig result = new ContainerConfig();

// add configuration from config file if specified
if( configFile != null ){
if( "".equals(configFile.trim()) ) throw new IllegalCliArgumentException("The specified config file is an empty string");
try {
result = ConfigFileProcessor.process(configFile);
} catch (IOException e) {
throw new RuntimeException("Unexpected error while setting the configuration from config file: "+e);
}
}

// add the entrypoint if specified
if( entrypoint!=null )
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/io/seqera/wavelit/util/ConfigFileProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.seqera.wavelit.util;

import io.seqera.wave.api.ContainerConfig;
import io.seqera.wavelit.json.JsonHelper;

import java.io.*;
import java.net.URL;

/**
* This implements parsing and assiging the value from json config file to ContainerConfig object
*
* @author Munish Chouhan <[email protected]>
*/
public class ConfigFileProcessor {

public static ContainerConfig process(String configFile) throws IOException {
String content = loadFile(configFile);
ContainerConfig containerConfig = JsonHelper.fromJson(content, ContainerConfig.class);
return containerConfig;
}

private static String loadFile(String configFile) throws IOException {
InputStream inputStream;
if(configFile.startsWith("http")){
URL url = new URL(configFile);
inputStream = url.openStream();
} else {
File inputFile = new File(configFile);
inputStream = new FileInputStream(inputFile);
}
StringBuilder contentBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
contentBuilder.append(line);
}
} finally {
inputStream.close();
}
return contentBuilder.toString();
}
}
85 changes: 85 additions & 0 deletions app/src/test/groovy/io/seqera/wavelit/AppTest.groovy
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
package io.seqera.wavelit

import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.core.WireMockConfiguration
import io.seqera.wave.api.ContainerConfig
import io.seqera.wavelit.exception.IllegalCliArgumentException
import picocli.CommandLine
import spock.lang.Specification

class AppTest extends Specification {

WireMockServer wireMockServer
def setup() {
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().port(8080))
wireMockServer.start()

def jsonContent = getResourceFileContent("config.json")
WireMock.stubFor(
WireMock.get(WireMock.urlEqualTo("/api/data"))
.willReturn(
WireMock.aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(jsonContent)
)
)
}

def cleanup() {
wireMockServer.stop()
}

def "test valid no entrypoint"() {
given:
def app = new App()
Expand Down Expand Up @@ -82,4 +106,65 @@ class AppTest extends Specification {
thrown(IllegalCliArgumentException)

}
def "test valid config file from a path"() {
given:
def app = new App()
String[] args = ["--config-file", getResourceFilePath("config.json")]

when:
new CommandLine(app).parseArgs(args)
then:
app.@configFile == getResourceFilePath("config.json")

when:
def config = app.prepareConfig()
then:
config.entrypoint == ['entrypoint']
def layer = config.layers[0]
layer.location == "https://location"
layer.gzipDigest == "sha256:gzipDigest"
layer.tarDigest == "sha256:tarDigest"
layer.gzipSize == 100
}

def "test valid config file from a URL"() {
given:
def app = new App()
String[] args = ["--config-file", "http://localhost:8080/api/data"]

when:
new CommandLine(app).parseArgs(args)
then:
app.@configFile == "http://localhost:8080/api/data"

when:
def config = app.prepareConfig()
then:
config.entrypoint == ['entrypoint']
def layer = config.layers[0]
layer.location == "https://location"
layer.gzipDigest == "sha256:gzipDigest"
layer.tarDigest == "sha256:tarDigest"
layer.gzipSize == 100
}

def "test invalid command"() {
given:
def app = new App()
String[] args = ["--config-cmd", ""]

when:
new CommandLine(app).parseArgs(args)
app.prepareConfig()
then:
thrown(IllegalCliArgumentException)
}

String getResourceFilePath(String resourceName) {
getClass().getClassLoader().getResource(resourceName).file
}

String getResourceFileContent(String resourceName) {
new File(getClass().getClassLoader().getResource(resourceName).toURI()).text
}
}
12 changes: 12 additions & 0 deletions app/src/test/resources/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"entrypoint": [ "entrypoint" ],
"layers": [
{
"location": "https://location",
"gzipDigest": "sha256:gzipDigest",
"gzipSize": 100,
"tarDigest": "sha256:tarDigest",
"skipHashing": true
}
]
}

0 comments on commit c0d4042

Please sign in to comment.