Skip to content

Commit 4d3d10a

Browse files
authored
removed usage of deprecated PicoCLI API (#141)
* removed usage of deprecated PicoCLI API * resolve master merge conflicts and cleanup CommandResponse usage
1 parent 216adbf commit 4d3d10a

File tree

23 files changed

+190
-234
lines changed

23 files changed

+190
-234
lines changed

imagetool/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<argument>--force</argument><!-- overwrite if exists -->
9999
<argument>--completionScript</argument>
100100
<argument>${project.build.directory}/imagetool_completion.sh</argument>
101-
<argument>com.oracle.weblogic.imagetool.cli.CLIDriver</argument>
101+
<argument>com.oracle.weblogic.imagetool.cli.ImageTool</argument>
102102
</arguments>
103103
</configuration>
104104
</plugin>

imagetool/src/main/bin/imagetool.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ FOR /F %%i IN ('%JAVA_EXE% -version 2^>^&1') DO (
2424
)
2525
)
2626
SET IMAGETOOL_HOME=%~dp0%/..
27-
%JAVA_HOME%\bin\java -cp %IMAGETOOL_HOME%\lib\* -Djava.util.logging.config.file=%IMAGETOOL_HOME%\bin\logging.properties com.oracle.weblogic.imagetool.cli.CLIDriver %*
27+
%JAVA_HOME%\bin\java -cp %IMAGETOOL_HOME%\lib\* -Djava.util.logging.config.file=%IMAGETOOL_HOME%\bin\logging.properties com.oracle.weblogic.imagetool.cli.ImageTool %*

imagetool/src/main/bin/imagetool.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ echo $RESULT_PATH
5454
script_dir=$( dirname "$( read_link "${BASH_SOURCE[0]}" )" )
5555
IMAGETOOL_HOME=`cd "${script_dir}/.." ; pwd`
5656
export IMAGETOOL_HOME
57-
${JAVA_HOME}/bin/java -cp "${IMAGETOOL_HOME}/lib/*" -Djava.util.logging.config.file=${IMAGETOOL_HOME}/bin/logging.properties com.oracle.weblogic.imagetool.cli.CLIDriver $@
57+
${JAVA_HOME}/bin/java -cp "${IMAGETOOL_HOME}/lib/*" -Djava.util.logging.config.file=${IMAGETOOL_HOME}/bin/logging.properties com.oracle.weblogic.imagetool.cli.ImageTool $@
5858

imagetool/src/main/bin/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ unalias imagetool 2> /dev/null
5353
script_dir=$( dirname "$( read_link "${BASH_SOURCE[0]}" )" )
5454
IMAGETOOL_HOME=`cd "${script_dir}/.." ; pwd`
5555
export IMAGETOOL_HOME
56-
alias imagetool="${JAVA_HOME}/bin/java -cp \"${IMAGETOOL_HOME}/lib/*\" -Djava.util.logging.config.file=${IMAGETOOL_HOME}/bin/logging.properties com.oracle.weblogic.imagetool.cli.CLIDriver"
56+
alias imagetool="${JAVA_HOME}/bin/java -cp \"${IMAGETOOL_HOME}/lib/*\" -Djava.util.logging.config.file=${IMAGETOOL_HOME}/bin/logging.properties com.oracle.weblogic.imagetool.cli.ImageTool"
5757
source ${IMAGETOOL_HOME}/lib/imagetool_completion.sh

imagetool/src/main/java/com/oracle/weblogic/imagetool/api/model/CommandResponse.java

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

44
package com.oracle.weblogic.imagetool.api.model;
55

6+
import com.oracle.weblogic.imagetool.util.Utils;
7+
68
public class CommandResponse {
79

810
private int status;
911
private String message;
1012
private Object result;
13+
private Object[] messageParams;
1114

1215
/**
1316
* For use with PicoCLI to return the response to the command line.
1417
* @param status CLI status, 0 if normal.
1518
* @param message message to the user.
1619
*/
17-
public CommandResponse(int status, String message) {
18-
this.status = status;
19-
this.message = message;
20-
}
21-
22-
/**
23-
* For use with PicoCLI to return the response to the command line.
24-
* @param status CLI status, 0 if normal.
25-
* @param message message to the user.
26-
* @param result more details that help the user understand the message.
27-
*/
28-
public CommandResponse(int status, String message, Object result) {
20+
public CommandResponse(int status, String message, Object... messageParams) {
2921
this.status = status;
3022
this.message = message;
31-
this.result = result;
23+
this.messageParams = messageParams;
3224
}
3325

3426
/**
@@ -44,26 +36,7 @@ public int getStatus() {
4436
* @return message to the user
4537
*/
4638
public String getMessage() {
47-
return message;
48-
}
49-
50-
51-
/**
52-
* Get the result in this response.
53-
* @param <T> result type.
54-
* @return the result object.
55-
*/
56-
@SuppressWarnings("unchecked")
57-
public <T> T getResult() {
58-
return (T) result;
59-
}
60-
61-
/**
62-
* True if the status was 0.
63-
* @return true if the status was 0.
64-
*/
65-
public boolean isSuccess() {
66-
return status == 0;
39+
return Utils.getMessage(message, messageParams);
6740
}
6841

6942
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/CLIDriver.java

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) 2019, 2020, Oracle Corporation 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;
5+
6+
import java.io.PrintWriter;
7+
import java.util.concurrent.Callable;
8+
9+
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
10+
import com.oracle.weblogic.imagetool.cli.cache.CacheCLI;
11+
import com.oracle.weblogic.imagetool.cli.menu.CreateImage;
12+
import com.oracle.weblogic.imagetool.cli.menu.RebaseImage;
13+
import com.oracle.weblogic.imagetool.cli.menu.UpdateImage;
14+
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
15+
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
16+
import picocli.CommandLine;
17+
import picocli.CommandLine.Command;
18+
import picocli.CommandLine.HelpCommand;
19+
import picocli.CommandLine.ParseResult;
20+
21+
@Command(
22+
name = "imagetool",
23+
mixinStandardHelpOptions = true,
24+
description = "%nA tool to build docker images of WebLogic with selected patches and/or psu(s) applied.%n",
25+
versionProvider = HelpVersionProvider.class,
26+
sortOptions = false,
27+
subcommands = {
28+
CacheCLI.class,
29+
CreateImage.class,
30+
UpdateImage.class,
31+
RebaseImage.class,
32+
HelpCommand.class
33+
},
34+
requiredOptionMarker = '*',
35+
abbreviateSynopsis = true,
36+
usageHelpWidth = 120,
37+
commandListHeading = "%nCommands:%n%nChoose from:%n"
38+
)
39+
public class ImageTool implements Callable<CommandResponse> {
40+
41+
private static final LoggingFacade logger = LoggingFactory.getLogger(ImageTool.class);
42+
43+
@Override
44+
public CommandResponse call() {
45+
CommandLine.usage(ImageTool.class, System.out);
46+
return new CommandResponse(0, "");
47+
}
48+
49+
/**
50+
* Entry point for Image Tool.
51+
* @param args command line arguments.
52+
*/
53+
public static void main(String[] args) {
54+
CommandResponse response = run(new ImageTool(),
55+
new PrintWriter(System.out, true),
56+
new PrintWriter(System.err, true),
57+
args);
58+
59+
if (response != null) {
60+
if (response.getStatus() != 0) {
61+
String message = String.format("Response code: %d, message: %s", response.getStatus(),
62+
response.getMessage());
63+
logger.severe(message);
64+
} else if (!response.getMessage().isEmpty()) {
65+
logger.info(response.getMessage());
66+
}
67+
System.exit(response.getStatus());
68+
}
69+
System.exit(-1);
70+
}
71+
72+
/**
73+
* Used from main entry point, and also entry point for unit tests.
74+
* @param out where to send stdout
75+
* @param err where to send stderr
76+
* @param args the command line arguments (minus the sub commands themselves)
77+
*/
78+
public static CommandResponse run(Callable<CommandResponse> callable, PrintWriter out, PrintWriter err,
79+
String... args) {
80+
81+
CommandLine cmd = new CommandLine(callable)
82+
.setCaseInsensitiveEnumValuesAllowed(true)
83+
.setToggleBooleanFlags(false)
84+
.setUnmatchedArgumentsAllowed(false)
85+
.setColorScheme(CommandLine.Help.defaultColorScheme(CommandLine.Help.Ansi.AUTO))
86+
.setOut(out)
87+
.setErr(err);
88+
89+
cmd.execute(args);
90+
//Get the command line for the sub-command (if exists), and ignore the parents, like "imagetool cache listItems"
91+
return getSubcommand(cmd).getExecutionResult();
92+
}
93+
94+
/**
95+
* Recursive method to find the deepest sub-command that was executed.
96+
* @param commandLine the picocli command line object to search
97+
* @return the lowest level command line executed
98+
*/
99+
private static CommandLine getSubcommand(CommandLine commandLine) {
100+
ParseResult parseResult = commandLine.getParseResult();
101+
if (parseResult.subcommand() != null) {
102+
CommandLine sub = parseResult.subcommand().commandSpec().commandLine();
103+
return getSubcommand(sub);
104+
}
105+
106+
return commandLine;
107+
}
108+
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/WLSCommandLine.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public CommandResponse call() {
3333
return new CommandResponse(-1, "Command Failed");
3434
}
3535
}
36-
spec.commandLine().usage(System.out);
37-
return new CommandResponse(-1, "Invalid arguments. --key & --path required.");
36+
return new CommandResponse(-1, "IMG-0044");
3837
}
3938

4039
@Option(

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@ public CommandResponse call() {
2828
if (location != null && Files.isRegularFile(location) && !Utils.isEmptyString(version)) {
2929
String key = String.format("%s%s%s", type, CacheStore.CACHE_KEY_SEPARATOR, version);
3030
if (cacheStore.getValueFromCache(key) != null) {
31-
return new CommandResponse(-1, String.format("Installer already exists %s=%s. Try removing it "
32-
+ "using the deleteEntry command before adding it again.",
33-
key,
34-
cacheStore.getValueFromCache(key)));
31+
return new CommandResponse(-1, "IMG-0048", key, cacheStore.getValueFromCache(key));
3532
}
3633
cacheStore.addToCache(key, location.toAbsolutePath().toString());
37-
return new CommandResponse(0, String.format("Successfully added to cache. %s=%s", key,
38-
cacheStore.getValueFromCache(key)));
34+
return new CommandResponse(0, "IMG-0050", key, cacheStore.getValueFromCache(key));
3935
}
40-
return new CommandResponse(-1, "Command Failed. Check arguments. --path should exist on disk");
36+
return new CommandResponse(-1, "IMG-0049", location);
4137
}
4238

4339
@Option(

0 commit comments

Comments
 (0)