Skip to content

Commit d74a65b

Browse files
authored
Add --force option to addInstaller and addPatch (#247)
1 parent c7534e1 commit d74a65b

File tree

4 files changed

+74
-81
lines changed

4 files changed

+74
-81
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/AddInstallerEntry.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,24 @@
33

44
package com.oracle.weblogic.imagetool.cli.cache;
55

6-
import java.nio.file.Files;
7-
import java.nio.file.Path;
8-
96
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
107
import com.oracle.weblogic.imagetool.cachestore.CacheStore;
118
import com.oracle.weblogic.imagetool.cachestore.CacheStoreException;
129
import com.oracle.weblogic.imagetool.installer.InstallerType;
13-
import com.oracle.weblogic.imagetool.util.Utils;
1410
import picocli.CommandLine.Command;
1511
import picocli.CommandLine.Option;
1612

17-
import static com.oracle.weblogic.imagetool.cachestore.CacheStoreFactory.cache;
18-
1913
@Command(
2014
name = "addInstaller",
2115
description = "Add cache entry for wls, fmw, jdk or wdt installer",
2216
sortOptions = false
2317
)
24-
public class AddInstallerEntry extends CacheOperation {
18+
public class AddInstallerEntry extends CacheAddOperation {
2519

2620
@Override
2721
public CommandResponse call() throws CacheStoreException {
28-
if (location != null && Files.isRegularFile(location) && !Utils.isEmptyString(version)) {
29-
String key = String.format("%s%s%s", type, CacheStore.CACHE_KEY_SEPARATOR, version);
30-
if (cache().getValueFromCache(key) != null) {
31-
return new CommandResponse(-1, "IMG-0048", key, cache().getValueFromCache(key));
32-
}
33-
cache().addToCache(key, location.toAbsolutePath().toString());
34-
return new CommandResponse(0, "IMG-0050", key, cache().getValueFromCache(key));
35-
}
36-
return new CommandResponse(-1, "IMG-0049", location);
22+
String key = String.format("%s%s%s", type, CacheStore.CACHE_KEY_SEPARATOR, version);
23+
return addToCache(key);
3724
}
3825

3926
@Option(
@@ -51,10 +38,4 @@ public CommandResponse call() throws CacheStoreException {
5138
)
5239
private String version;
5340

54-
@Option(
55-
names = {"--path"},
56-
description = "Location on disk. For ex: /path/to/FMW/installer.zip",
57-
required = true
58-
)
59-
private Path location;
6041
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntry.java

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,38 @@
33

44
package com.oracle.weblogic.imagetool.cli.cache;
55

6-
import java.nio.file.Files;
7-
import java.nio.file.Path;
86
import java.util.ArrayList;
97
import java.util.List;
108

119
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
12-
import com.oracle.weblogic.imagetool.cachestore.CacheStoreException;
1310
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
1411
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
1512
import com.oracle.weblogic.imagetool.util.Utils;
1613
import picocli.CommandLine.Command;
1714
import picocli.CommandLine.Option;
1815

19-
import static com.oracle.weblogic.imagetool.cachestore.CacheStoreFactory.cache;
20-
2116
@Command(
2217
name = "addPatch",
2318
description = "Add cache entry for wls|fmw patch or psu",
2419
sortOptions = false
2520
)
26-
public class AddPatchEntry extends CacheOperation {
21+
public class AddPatchEntry extends CacheAddOperation {
2722

2823
private static final LoggingFacade logger = LoggingFactory.getLogger(AddPatchEntry.class);
2924

3025
@Override
3126
public CommandResponse call() throws Exception {
3227

33-
if (type != null) {
34-
logger.warning("IMG-0078");
35-
}
36-
37-
if (patchId != null && !patchId.isEmpty()
38-
&& location != null && Files.exists(location)
39-
&& Files.isRegularFile(location)) {
40-
28+
if (patchId != null && !patchId.isEmpty()) {
4129
List<String> patches = new ArrayList<>();
4230
patches.add(patchId);
4331
if (!Utils.validatePatchIds(patches, true)) {
4432
return new CommandResponse(-1, "Patch ID validation failed");
4533
}
4634
return addToCache(patchId);
35+
} else {
36+
return new CommandResponse(-1, "IMG-0076", "--patchId");
4737
}
48-
49-
String msg = "Invalid arguments";
50-
if (patchId == null || patchId.isEmpty()) {
51-
msg += " : --patchId was not supplied";
52-
}
53-
if (location == null || !Files.exists(location) || !Files.isRegularFile(location)) {
54-
msg += " : --path is invalid";
55-
}
56-
57-
return new CommandResponse(-1, msg);
58-
}
59-
60-
/**
61-
* Add patch to the cache.
62-
*
63-
* @param patchNumber the patchId (minus the 'p') of the patch to add
64-
* @return CLI command response
65-
*/
66-
private CommandResponse addToCache(String patchNumber) throws CacheStoreException {
67-
if (cache().getValueFromCache(patchNumber) != null) {
68-
return new CommandResponse(-1, "IMG-0076", patchNumber);
69-
}
70-
cache().addToCache(patchNumber, location.toAbsolutePath().toString());
71-
return new CommandResponse(0, Utils.getMessage("IMG-0075", patchNumber, location.toAbsolutePath()));
7238
}
7339

7440
@Option(
@@ -77,19 +43,4 @@ private CommandResponse addToCache(String patchNumber) throws CacheStoreExceptio
7743
required = true
7844
)
7945
private String patchId;
80-
81-
@Option(
82-
names = {"--type"},
83-
description = "Type of patch. DEPRECATED"
84-
)
85-
@Deprecated
86-
private String type;
87-
88-
@Option(
89-
names = {"--path"},
90-
description = "Location on disk. For ex: /path/to/FMW/patch.zip",
91-
required = true
92-
)
93-
private Path location;
94-
9546
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2021, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cli.cache;
5+
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
9+
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
10+
import com.oracle.weblogic.imagetool.cachestore.CacheStoreException;
11+
import picocli.CommandLine;
12+
13+
import static com.oracle.weblogic.imagetool.cachestore.CacheStoreFactory.cache;
14+
15+
public abstract class CacheAddOperation extends CacheOperation {
16+
17+
CommandResponse addToCache(String key) throws CacheStoreException {
18+
// if file is invalid or does not exist, return an error
19+
if (filePath == null || !Files.isRegularFile(filePath)) {
20+
return new CommandResponse(-1, "IMG-0049", filePath);
21+
}
22+
23+
// if the new value is the same as the existing cache value, do nothing
24+
String existingValue = cache().getValueFromCache(key);
25+
if (absolutePath().toString().equals(existingValue)) {
26+
return new CommandResponse(0, "IMG-0075");
27+
}
28+
29+
// if there is already a cache entry and the user did not ask to force it, return an error
30+
if (!force && existingValue != null) {
31+
return new CommandResponse(-1, "IMG-0048", key, existingValue);
32+
}
33+
34+
// input appears valid, add the entry to the cache and exit
35+
cache().addToCache(key, absolutePath().toString());
36+
return new CommandResponse(0, "IMG-0050", key, cache().getValueFromCache(key));
37+
}
38+
39+
Path absolutePath() {
40+
if (absolutePath == null) {
41+
absolutePath = filePath.toAbsolutePath();
42+
}
43+
return absolutePath;
44+
}
45+
46+
private Path absolutePath = null;
47+
48+
@CommandLine.Option(
49+
names = {"--force"},
50+
description = "Overwrite existing entry, if it exists"
51+
)
52+
private boolean force = false;
53+
54+
55+
@CommandLine.Option(
56+
names = {"--path"},
57+
description = "Location on disk. For ex: /path/to/patch-or-installer.zip",
58+
required = true
59+
)
60+
private Path filePath;
61+
}

imagetool/src/main/resources/ImageTool.properties

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ IMG-0044=Invalid arguments. Both --key and --path are required parameters, and c
4646
IMG-0045=Invalid argument. The value for --key should correspond to a valid entry in the cache.
4747
IMG-0046=Deleted all entries from cache
4848
IMG-0047=cache is empty
49-
IMG-0048=Installer already exists {0}={1}. Try removing it, before adding it again: imagetool cache deleteEntry --key={0}
50-
IMG-0049=Could not find value for --path, file not found: {0}
51-
IMG-0050=Successfully added to cache. {0}={1}
49+
IMG-0048=Entry {0} already exists as {1}. Use --force to override the entry, or remove the existing entry first: imagetool cache deleteEntry --key={0}
50+
IMG-0049=File not found, invalid value for --path {0}
51+
IMG-0050=Successfully added to cache. [[cyan: {0}]]={1}
5252
IMG-0051=Deleted entry [[cyan: {0}]]={1}
5353
IMG-0052=Nothing to delete for key: {0}
5454
IMG-0053=[[brightgreen: Build successful.]] Build time={0}s. Image tag={1}
@@ -73,10 +73,10 @@ IMG-0071=WDT Operation is set to UPDATE, but the DOMAIN_HOME environment variabl
7373
IMG-0072=ORACLE_HOME environment variable is not defined in the base image: {0}
7474
IMG-0073=Invalid file {0} listed for --resourceTemplates
7575
IMG-0074=OPatch will not be updated, fromImage has version {0}, available version is {1}
76-
IMG-0075=Added patch entry [[cyan: {0}]]={1}
77-
IMG-0076=Cache key {0} already exists, remove it first
76+
IMG-0075=Nothing to do, cache entry already exists
77+
IMG-0076=Invalid argument, the value provided for {0} is invalid or empty.
7878
IMG-0077=Skipping duplicate patch {0}. Patch file already exists in the build context folder. Did you accidentally list the patch twice?
79-
IMG-0078=[[cyan: --type]] is [[brightred: DEPRECATED]] and will be removed in an upcoming release.
79+
IMG-0078=REMOVED
8080
IMG-0079=OS Package Manager override, changed from {0} to {1}
8181
IMG-0080=Argument {0} does not match any FmwInstallerType names
8282
IMG-0081=Unable to retrieve list of Oracle releases from Oracle Updates (ARU). Try again later.

0 commit comments

Comments
 (0)