Skip to content

Commit 13c6324

Browse files
committed
chimera: update shell command to set a proper error code on exit
Motivation: When a single command is executed via chimera-cli the caller probably want to get a proper error code to identify unsuccessful execution. Modification: Update ShellApplication#start to return error code that should be reported on exit. Update ShellApplication#execute to return an error code. Update ShellApplication#start to propagate the error code if single command mode is used. Update chimera Shell to exit application with error code returned by ShellApplication#start. Result: chimera errors propagated to the user shell. Fixes: #8129 Acked-by: Lennart Sack Target: master Require-book: no Require-notes: yes
1 parent 4077ddd commit 13c6324

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

modules/common-cli/src/main/java/org/dcache/util/cli/ShellApplication.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ public ShellApplication() throws Exception {
6060

6161
/**
6262
* Start processing the command(s), based on the supplied arguments.
63+
* @return the exit code to return to the caller.
6364
*/
64-
protected void start(Args args) throws Throwable {
65+
protected int start(Args args) throws Throwable {
6566
if (args.hasOption("h")) {
6667
System.out.println("Usage: " + getCommandName() + " [-e] [-f=<file>]|[-]|[COMMAND]");
6768
System.out.println();
@@ -72,19 +73,22 @@ protected void start(Args args) throws Throwable {
7273

7374
Ansi.setEnabled(isAnsiSupported);
7475

76+
int exitCode = 0;
7577
if (args.hasOption("f")) {
7678
try (InputStream in = new FileInputStream(args.getOption("f"))) {
7779
execute(new BufferedInputStream(in), System.out, args.hasOption("e"));
7880
}
7981
} else if (args.argc() == 1 && args.argv(0).equals("-")) {
8082
execute(System.in, System.out, args.hasOption("e"));
8183
} else if (args.argc() > 0) {
82-
execute(args);
84+
exitCode = execute(args);
8385
} else if (!hasConsole) {
8486
execute(System.in, System.out, args.hasOption("e"));
8587
} else {
8688
console();
8789
}
90+
91+
return exitCode;
8892
}
8993

9094
/**
@@ -131,13 +135,15 @@ public Object getResult() {
131135

132136
/**
133137
* Executes a single command with the output being printed to the console.
138+
* @return the exit code to return to the caller.
134139
*/
135-
public void execute(Args args) throws Throwable {
140+
public int execute(Args args) throws Throwable {
136141
if (args.argc() == 0) {
137-
return;
142+
return 0;
138143
}
139144

140145
String out;
146+
int exitCode = 0;
141147
try {
142148
if (isAnsiSupported && args.argc() > 0) {
143149
if (args.argv(0).equals("help")) {
@@ -158,6 +164,7 @@ public void execute(Args args) throws Throwable {
158164
sb.a(help);
159165
}
160166
out = sb.toString();
167+
exitCode = 1;
161168
} catch (CommandExitException e) {
162169
throw e;
163170
} catch (CommandPanicException e) {
@@ -168,8 +175,10 @@ public void execute(Args args) throws Throwable {
168175
StringWriter sw = new StringWriter();
169176
t.printStackTrace(new PrintWriter(sw));
170177
out = sb.a(sw.toString()).toString();
178+
exitCode = 1;
171179
} catch (Exception e) {
172180
out = Ansi.ansi().fg(RED).a(e.getMessage()).reset().toString();
181+
exitCode = 1;
173182
}
174183
if (!isNullOrEmpty(out)) {
175184
console.print(out);
@@ -178,6 +187,7 @@ public void execute(Args args) throws Throwable {
178187
}
179188
}
180189
console.flush();
190+
return exitCode;
181191
}
182192

183193
/**

modules/dcache-chimera/src/main/java/org/dcache/chimera/cli/Shell.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static void main(String[] arguments) throws Throwable {
9999

100100
try (Shell shell = new Shell(arguments[0], arguments[1], arguments[2], arguments[3],
101101
arguments[4], arguments[5], arguments[6])) {
102-
shell.start(args);
102+
System.exit(shell.start(args));
103103
}
104104
}
105105

0 commit comments

Comments
 (0)