Skip to content

Commit 8397c8a

Browse files
committed
--label option added
Signed-off-by: munishchouhan <[email protected]>
1 parent 2d0e267 commit 8397c8a

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

app/src/main/java/io/seqera/wave/cli/App.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@
3131
import java.nio.file.NoSuchFileException;
3232
import java.nio.file.Path;
3333
import java.time.OffsetDateTime;
34-
import java.util.Arrays;
35-
import java.util.Base64;
36-
import java.util.List;
37-
import java.util.Optional;
34+
import java.util.*;
3835
import java.util.stream.Collectors;
3936

4037
import ch.qos.logback.classic.Level;
@@ -60,8 +57,8 @@
6057
import io.seqera.wave.util.Packer;
6158
import org.slf4j.LoggerFactory;
6259
import picocli.CommandLine;
63-
import static io.seqera.wave.cli.util.Checkers.isEmpty;
64-
import static io.seqera.wave.cli.util.Checkers.isEnvVar;
60+
61+
import static io.seqera.wave.cli.util.Checkers.*;
6562
import static io.seqera.wave.util.DockerHelper.addPackagesToSpackFile;
6663
import static io.seqera.wave.util.DockerHelper.condaFileFromPackages;
6764
import static io.seqera.wave.util.DockerHelper.condaFileFromPath;
@@ -200,6 +197,9 @@ public class App implements Runnable {
200197
@Option(names = {"--include"}, paramLabel = "false", description = "Include one or more containers in the specified base image")
201198
List<String> includes;
202199

200+
@Option(names = {"--label"}, paramLabel = "false", description = "Include one or more labels in the wave build image. e.g. KEY=VALUE")
201+
List<String> label;
202+
203203
public static void main(String[] args) {
204204
try {
205205
final App app = new App();
@@ -382,13 +382,28 @@ protected void validateArgs() {
382382
if( !isEmpty(platform) && !VALID_PLATFORMS.contains(platform) )
383383
throw new IllegalCliArgumentException(String.format("Unsupported container platform: '%s'", platform));
384384

385+
// check labels
386+
if( label!=null ) {
387+
for( String it : label) {
388+
if( !isLabel(it) ) throw new IllegalCliArgumentException("Invalid docker label syntax - offending value: " + it);
389+
}
390+
}
391+
385392
}
386393

387394
protected Client client() {
388395
return new Client().withEndpoint(waveEndpoint);
389396
}
390397

391398
protected SubmitContainerTokenRequest createRequest() {
399+
Map<String, String> labels = null;
400+
if(label!=null){
401+
labels = new HashMap<>();
402+
for(String singleLabel: label){
403+
String[] singleLabelArray = singleLabel.split("=");
404+
labels.put(singleLabelArray[0], singleLabelArray[1]);
405+
}
406+
}
392407
return new SubmitContainerTokenRequest()
393408
.withContainerImage(image)
394409
.withContainerFile(containerFileBase64())
@@ -407,6 +422,7 @@ protected SubmitContainerTokenRequest createRequest() {
407422
.withFreezeMode(freeze)
408423
.withDryRun(dryRun)
409424
.withContainerIncludes(includes)
425+
.withLabels(labels)
410426
;
411427
}
412428

app/src/main/java/io/seqera/wave/cli/util/Checkers.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public class Checkers {
2727

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

3031
static public boolean isEmpty(String value) {
3132
return value==null || "".equals(value.trim());
@@ -38,4 +39,8 @@ static public boolean isEmpty(List list) {
3839
static public boolean isEnvVar(String value) {
3940
return value!=null && ENV_REGEX.matcher(value).matches();
4041
}
42+
43+
static public boolean isLabel(String value) {
44+
return value!=null && LABEL_REGEX.matcher(value).matches();
45+
}
4146
}

app/src/test/groovy/io/seqera/wave/cli/AppTest.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package io.seqera.wave.cli
1919

20+
import io.seqera.wave.api.ContainerConfig
21+
2022
import java.nio.file.Files
2123
import java.time.Instant
2224

@@ -269,4 +271,23 @@ class AppTest extends Specification {
269271
app.@towerToken == 'xyz'
270272
}
271273

274+
def "test valid labels"(){
275+
given:
276+
def app = new App()
277+
String[] args = ["--label", "key1=value1","--label", "key 2=value2"]
278+
279+
when:
280+
new CommandLine(app).parseArgs(args)
281+
then:
282+
app.@label[0] == "key1=value1"
283+
app.@label[1] == "key 2=value2"
284+
285+
when:
286+
def request = app.createRequest()
287+
then:
288+
request.labels == [
289+
"key1":"value1",
290+
"key 2":"value2"
291+
]
292+
}
272293
}

0 commit comments

Comments
 (0)