Skip to content

added --label option #59

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
24 changes: 24 additions & 0 deletions app/src/main/java/io/seqera/wave/cli/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,20 @@
import io.seqera.wave.util.Packer;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;

import static io.seqera.wave.cli.util.Checkers.isEmpty;
import static io.seqera.wave.cli.util.Checkers.isEnvVar;
import static io.seqera.wave.cli.util.Checkers.isLabel;
import static io.seqera.wave.util.DockerHelper.addPackagesToSpackFile;
import static io.seqera.wave.util.DockerHelper.condaFileFromPackages;
import static io.seqera.wave.util.DockerHelper.condaFileFromPath;
import static io.seqera.wave.util.DockerHelper.condaFileToDockerFile;
import static io.seqera.wave.util.DockerHelper.condaFileToSingularityFile;
import static io.seqera.wave.util.DockerHelper.condaPackagesToDockerFile;
import static io.seqera.wave.util.DockerHelper.condaPackagesToSingularityFile;
import static io.seqera.wave.util.DockerHelper.spackFileToDockerFile;
import static io.seqera.wave.util.DockerHelper.spackFileToSingularityFile;
import static io.seqera.wave.util.DockerHelper.spackPackagesToSpackFile;
import static io.seqera.wave.cli.util.StreamHelper.tryReadStdin;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.Option;
Expand Down Expand Up @@ -133,6 +145,9 @@ public class App implements Runnable {
@Option(names = {"--config-env"}, paramLabel = "''", description = "Overwrite the environment of the image e.g. NAME=VALUE")
private List<String> environment;

@Option(names = {"--config-label"}, paramLabel = "false", description = "Add one or more labels to the container image, e.g. KEY=VALUE.")
private List<String> labels;

@Option(names = {"--config-cmd"}, paramLabel = "''", description = "Overwrite the default CMD (command) of the image.")
private String command;

Expand Down Expand Up @@ -400,6 +415,7 @@ protected Client client() {
}

protected SubmitContainerTokenRequest createRequest() {

return new SubmitContainerTokenRequest()
.withContainerImage(image)
.withContainerFile(containerFileBase64())
Expand Down Expand Up @@ -539,6 +555,14 @@ protected ContainerConfig prepareConfig() {
result.env = environment;
}

//add labels if specified
if( labels!=null ) {
for( String it : labels) {
if( !isLabel(it) ) throw new IllegalCliArgumentException("Invalid container image label syntax - offending value: " + it);
}
result.labels = labels;
}

//add the working directory if specified
if( workingDir != null ){
if( "".equals(workingDir.trim()) ) throw new IllegalCliArgumentException("The working directory cannot be empty string");
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/io/seqera/wave/cli/util/Checkers.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class Checkers {

private static final Pattern ENV_REGEX = Pattern.compile("^[A-Za-z_][A-Za-z0-9_]*=.*$");
private static final Pattern LABEL_REGEX = Pattern.compile("^[a-z][a-z0-9.-]*[a-z0-9]=.*$");

static public boolean isEmpty(String value) {
return value==null || "".equals(value.trim());
Expand All @@ -38,4 +39,8 @@ static public boolean isEmpty(List list) {
static public boolean isEnvVar(String value) {
return value!=null && ENV_REGEX.matcher(value).matches();
}

static public boolean isLabel(String value) {
return value!=null && LABEL_REGEX.matcher(value).matches();
}
}
23 changes: 23 additions & 0 deletions app/src/test/groovy/io/seqera/wave/cli/AppTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.seqera.wave.cli

import io.seqera.wave.api.ContainerConfig
import io.seqera.wave.cli.util.DurationConverter

import java.nio.file.Files
Expand Down Expand Up @@ -278,6 +279,28 @@ class AppTest extends Specification {
app.@towerToken == 'xyz'
}

def "test valid labels"(){
given:
def app = new App()
String[] args = ["--config-label", "key1=value1","--config-label", "key2=this value2", "--config-label", "[email protected]"]

when:
new CommandLine(app).parseArgs(args)
then:
app.@labels[0] == "key1=value1"
app.@labels[1] == "key2=this value2"
app.@labels[2] == "[email protected]"

when:
def config = app.prepareConfig()
then:
config.labels == [
"key1=value1",
"key2=this value2",
"[email protected]"
]
}

def 'should get the correct await duration in minutes'(){
given:
def app = new App()
Expand Down
Loading