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
28 changes: 22 additions & 6 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,8 @@
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.*;
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 @@ -200,6 +197,9 @@ public class App implements Runnable {
@Option(names = {"--include"}, paramLabel = "false", description = "Include one or more containers in the specified base image")
List<String> includes;

@Option(names = {"--label"}, paramLabel = "false", description = "Include one or more labels in the wave build image. e.g. KEY=VALUE")
marcodelapierre marked this conversation as resolved.
Show resolved Hide resolved
marcodelapierre marked this conversation as resolved.
Show resolved Hide resolved
List<String> label;

public static void main(String[] args) {
try {
final App app = new App();
Expand Down Expand Up @@ -382,13 +382,28 @@ protected void validateArgs() {
if( !isEmpty(platform) && !VALID_PLATFORMS.contains(platform) )
throw new IllegalCliArgumentException(String.format("Unsupported container platform: '%s'", platform));

// check labels
if( label!=null ) {
for( String it : label) {
if( !isLabel(it) ) throw new IllegalCliArgumentException("Invalid docker label syntax - offending value: " + it);
marcodelapierre marked this conversation as resolved.
Show resolved Hide resolved
}
}

}

protected Client client() {
return new Client().withEndpoint(waveEndpoint);
}

protected SubmitContainerTokenRequest createRequest() {
Map<String, String> labels = null;
if(label!=null){
labels = new HashMap<>();
for(String singleLabel: label){
String[] singleLabelArray = singleLabel.split("=");
labels.put(singleLabelArray[0], singleLabelArray[1]);
}
}
return new SubmitContainerTokenRequest()
.withContainerImage(image)
.withContainerFile(containerFileBase64())
Expand All @@ -407,6 +422,7 @@ protected SubmitContainerTokenRequest createRequest() {
.withFreezeMode(freeze)
.withDryRun(dryRun)
.withContainerIncludes(includes)
.withLabels(labels)
;
}

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();
}
}
21 changes: 21 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,23 @@ 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 = ["--label", "key1=value1","--label", "key2=value2"]

when:
new CommandLine(app).parseArgs(args)
then:
app.@label[0] == "key1=value1"
app.@label[1] == "key2=value2"

when:
def request = app.createRequest()
then:
request.labels == [
"key1":"value1",
"key2":"value2"
]
}
}
Loading