Skip to content
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

added --label option #59

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
19 changes: 15 additions & 4 deletions app/src/main/java/io/seqera/wave/cli/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

import ch.qos.logback.classic.Level;
Expand All @@ -60,8 +57,10 @@
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;
Expand Down Expand Up @@ -136,6 +135,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 @@ -389,6 +391,7 @@ protected Client client() {
}

protected SubmitContainerTokenRequest createRequest() {

return new SubmitContainerTokenRequest()
.withContainerImage(image)
.withContainerFile(containerFileBase64())
Expand Down Expand Up @@ -528,6 +531,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,8 @@

package io.seqera.wave.cli

import io.seqera.wave.api.ContainerConfig

import java.nio.file.Files
import java.time.Instant

Expand Down Expand Up @@ -269,4 +271,25 @@ class AppTest extends Specification {
app.@towerToken == 'xyz'
}

def "test valid labels"(){
marcodelapierre marked this conversation as resolved.
Show resolved Hide resolved
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]"
]
}
}
Loading