Skip to content

Commit dd63b55

Browse files
committed
refactoring
1 parent 64cc4b5 commit dd63b55

29 files changed

+362
-216
lines changed

astra-shell/src/main/java/com/datastax/astra/shell/ShellContext.java

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import com.datastax.astra.shell.cmd.BaseCliCommand;
2020
import com.datastax.astra.shell.cmd.BaseCommand;
2121
import com.datastax.astra.shell.cmd.BaseShellCommand;
22+
import com.datastax.astra.shell.output.OutputFormat;
2223
import com.datastax.astra.shell.utils.LoggerShell;
24+
import com.datastax.astra.shell.utils.ShellPrinter;
2325

2426
/**
2527
* Hold the context of CLI to know where we are.
@@ -87,6 +89,9 @@ public static synchronized ShellContext getInstance() {
8789
/** Raw command. */
8890
private List<String> rawCommand = new ArrayList<>();
8991

92+
/** Raw command. */
93+
private String rawShellCommand;
94+
9095
/** History of commands in shell. */
9196
private List<BaseShellCommand> history = new ArrayList<>();
9297

@@ -109,7 +114,7 @@ public static synchronized ShellContext getInstance() {
109114
*/
110115
private boolean isSectionValid(BaseCommand cmd) {
111116
if (!this.astraRc.isSectionExists(this.configSection)) {
112-
cmd.outputError(CANNOT_CONNECT, "No token provided (-t), no config provided (--config), section '" + this.configSection
117+
ShellPrinter.outputError(CANNOT_CONNECT, "No token provided (-t), no config provided (--config), section '" + this.configSection
113118
+ "' has not been found in config file '"
114119
+ this.astraRc.getConfigFile().getPath() + "'. Try [astra setup]");
115120
return false;
@@ -129,7 +134,7 @@ private boolean isSectionTokenValid(BaseCommand cmd) {
129134
if (StringUtils.isEmpty(this.astraRc
130135
.getSection(this.configSection)
131136
.get(AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN))) {
132-
cmd.outputError(
137+
ShellPrinter.outputError(
133138
INVALID_PARAMETER,
134139
"Key '" + AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN +
135140
"' has not found been in config [section '" + this.configSection + "']");
@@ -175,7 +180,7 @@ public void init(BaseCliCommand cli) {
175180
}
176181

177182
if (token != null) {
178-
LoggerShell.debug("Token retrieved: " + token);
183+
LoggerShell.debug("Token: " + token);
179184
connect(token);
180185
} else {
181186
INVALID_PARAMETER.exit();
@@ -194,7 +199,7 @@ public void connect(String token) {
194199
this.token = token;
195200

196201
if (!token.startsWith(token)) {
197-
startCommand.outputError(INVALID_PARAMETER, "Token provided is invalid. It should start with 'AstraCS:...'. Try [astra setup]");
202+
ShellPrinter.outputError(INVALID_PARAMETER, "Token provided is invalid. It should start with 'AstraCS:...'. Try [astra setup]");
198203
INVALID_PARAMETER.exit();
199204
}
200205

@@ -206,7 +211,7 @@ public void connect(String token) {
206211
this.organization = apiDevopsOrganizations.organization();
207212
LoggerShell.success("Cli successfully initialized");
208213
} catch(Exception e) {
209-
startCommand.outputError(CANNOT_CONNECT, "Token provided is invalid. Try [astra setup]");
214+
ShellPrinter.outputError(CANNOT_CONNECT, "Token provided is invalid. Try [astra setup]");
210215
INVALID_PARAMETER.exit();
211216
}
212217
}
@@ -289,6 +294,57 @@ public void exitDatabase() {
289294
this.databaseRegion = null;
290295
}
291296

297+
/**
298+
* user flag as no color to get a fixed size output.
299+
*
300+
* @return
301+
* if no color flag is toggled.
302+
*/
303+
public boolean isNoColor() {
304+
BaseShellCommand sh = getCurrentShellCommand();
305+
BaseCliCommand cli = getStartCommand();
306+
if (cli == null) return false;
307+
return (cli.isNoColor() || (sh != null && sh.isNoColor()));
308+
}
309+
310+
/**
311+
* Log in the console only if verbose is enabled.
312+
*
313+
* @return
314+
* if verbose
315+
*/
316+
public boolean isVerbose() {
317+
BaseShellCommand sh = getCurrentShellCommand();
318+
BaseCliCommand cli = getStartCommand();
319+
if (cli == null) return false;
320+
return (cli.isVerbose() || (sh != null && sh.isVerbose()));
321+
}
322+
323+
/**
324+
* Trigger a log only if relevant.
325+
*
326+
* @return
327+
* check if logger is enabled
328+
*/
329+
public boolean isFileLoggerEnabled() {
330+
BaseCliCommand cli = getStartCommand();
331+
return (cli != null && cli.getLogFileWriter() != null);
332+
}
333+
334+
/**
335+
* Retrieve output format based on raw command.
336+
*
337+
* @return
338+
* output format
339+
*/
340+
public OutputFormat getOutputFormat() {
341+
BaseCliCommand cli = getStartCommand();
342+
BaseShellCommand sh = getCurrentShellCommand();
343+
if (cli == null) return OutputFormat.human;
344+
if (sh != null) return sh.getFormat();
345+
return cli.getFormat();
346+
}
347+
292348
/**
293349
* Getter accessor for attribute 'apiDevopsDatabases'.
294350
*
@@ -416,6 +472,25 @@ public void setRawCommand(String... args) {
416472
* current command as a String
417473
*/
418474
public String getRawCommandString() {
419-
return "astra " + StringUtils.join(" ", getRawCommand());
475+
return "astra " + StringUtils.join(getRawCommand(), " ");
476+
}
477+
478+
/**
479+
* Getter accessor for attribute 'rawShellCommand'.
480+
*
481+
* @return
482+
* current value of 'rawShellCommand'
483+
*/
484+
public String getRawShellCommand() {
485+
return rawShellCommand;
486+
}
487+
488+
/**
489+
* Setter accessor for attribute 'rawShellCommand'.
490+
* @param rawShellCommand
491+
* new value for 'rawShellCommand '
492+
*/
493+
public void setRawShellCommand(String rawShellCommand) {
494+
this.rawShellCommand = rawShellCommand;
420495
}
421496
}
Lines changed: 10 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
package com.datastax.astra.shell.cmd;
22

3-
import java.util.Arrays;
4-
import java.util.HashMap;
5-
import java.util.Map;
6-
7-
import com.datastax.astra.shell.ExitCode;
8-
import com.datastax.astra.shell.output.CsvOutput;
9-
import com.datastax.astra.shell.output.JsonOutput;
103
import com.datastax.astra.shell.output.OutputFormat;
11-
import com.datastax.astra.shell.utils.LoggerShell;
12-
import com.datastax.astra.shell.utils.ShellPrinter;
134
import com.github.rvesse.airline.annotations.Option;
145

156
/**
@@ -53,96 +44,6 @@ public abstract class BaseCommand implements Runnable {
5344
description = "Output format, valid values are: human,json,csv")
5445
protected OutputFormat format = OutputFormat.human;
5546

56-
/**
57-
* Exit program with error.
58-
*
59-
* @param code
60-
* error code
61-
* @param msg
62-
* error message
63-
*/
64-
public void outputError(ExitCode code, String msg) {
65-
switch(format) {
66-
case json:
67-
ShellPrinter.printJson(new JsonOutput(code, code.name() + ": " + msg));
68-
break;
69-
case csv:
70-
ShellPrinter.printCsv(new CsvOutput(code, code.name() + ": " + msg));
71-
break;
72-
case human:
73-
default:
74-
LoggerShell.error( code.name() + ": " + msg);
75-
break;
76-
}
77-
}
78-
79-
/**
80-
* Exit program with no operation
81-
*
82-
* @param code
83-
* error code
84-
* @param msg
85-
* error message
86-
*/
87-
public void outputWarning(ExitCode code, String msg) {
88-
switch(format) {
89-
case json:
90-
ShellPrinter.printJson(new JsonOutput(code, code.name() + ": " + msg));
91-
break;
92-
case csv:
93-
ShellPrinter.printCsv(new CsvOutput(code, code.name() + ": " + msg));
94-
break;
95-
case human:
96-
default:
97-
LoggerShell.warning(code.name() + ": " + msg);
98-
break;
99-
}
100-
}
101-
102-
/**
103-
* Exit program with error.
104-
*
105-
* @param msg
106-
* return message
107-
*/
108-
public void outputData(String label, String data) {
109-
switch(format) {
110-
case json:
111-
ShellPrinter.printJson(new JsonOutput(ExitCode.SUCCESS, label, data));
112-
break;
113-
case csv:
114-
Map<String, String> m = new HashMap<>();
115-
m.put(label, data);
116-
ShellPrinter.printCsv(new CsvOutput(Arrays.asList(label), Arrays.asList(m)));
117-
break;
118-
case human:
119-
default:
120-
System.out.println(data);
121-
break;
122-
}
123-
}
124-
125-
/**
126-
* Exit program with error.
127-
*
128-
* @param msg
129-
* return message
130-
*/
131-
public void outputSuccess(String msg) {
132-
switch(format) {
133-
case json:
134-
ShellPrinter.printJson(new JsonOutput(ExitCode.SUCCESS, msg));
135-
break;
136-
case csv:
137-
ShellPrinter.printCsv(new CsvOutput(ExitCode.SUCCESS, msg));
138-
break;
139-
case human:
140-
default:
141-
LoggerShell.success(msg);
142-
break;
143-
}
144-
}
145-
14647
/**
14748
* Getter accessor for attribute 'format'.
14849
*
@@ -162,6 +63,16 @@ public OutputFormat getFormat() {
16263
public boolean isVerbose() {
16364
return verbose;
16465
}
66+
67+
/**
68+
* Getter accessor for attribute 'noColor'.
69+
*
70+
* @return
71+
* current value of 'noColor'
72+
*/
73+
public boolean isNoColor() {
74+
return noColor;
75+
}
16576

16677

16778
}

astra-shell/src/main/java/com/datastax/astra/shell/cmd/BaseShellCommand.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import com.datastax.astra.shell.ExitCode;
44
import com.datastax.astra.shell.ShellContext;
5-
import com.datastax.astra.shell.output.OutputFormat;
5+
import com.datastax.astra.shell.utils.LoggerShell;
6+
import com.datastax.astra.shell.utils.ShellPrinter;
67

78
/**
89
* Base command.
@@ -15,12 +16,12 @@ public abstract class BaseShellCommand extends BaseCommand {
1516
public void run() {
1617

1718
// As a shell command it should be initialized
18-
if (!ShellContext.getInstance().isInitialized()) {
19-
outputError(ExitCode.CONFLICT, "A shell command should have the connection set");
19+
if (!ctx().isInitialized()) {
20+
ShellPrinter.outputError(ExitCode.CONFLICT, "A shell command should have the connection set");
2021
} else {
21-
// Keep history of commands and options of the shell
22-
ShellContext.getInstance().setCurrentShellCommand(this);
23-
this.format = OutputFormat.human;
22+
ctx().setCurrentShellCommand(this);
23+
LoggerShell.info("Shell : " + ShellContext.getInstance().getRawShellCommand());
24+
LoggerShell.info("Class : " + getClass().getName());
2425
execute();
2526
}
2627
}
@@ -36,7 +37,7 @@ public void run() {
3637
* @return
3738
* current context
3839
*/
39-
protected ShellContext getContext() {
40+
protected ShellContext ctx() {
4041
return ShellContext.getInstance();
4142
}
4243

astra-shell/src/main/java/com/datastax/astra/shell/cmd/ExitCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.datastax.astra.shell.cmd;
22

33
import com.datastax.astra.shell.ExitCode;
4+
import com.datastax.astra.shell.utils.ShellPrinter;
45
import com.github.rvesse.airline.annotations.Command;
56

67
/**
@@ -14,7 +15,7 @@ public class ExitCommand extends BaseShellCommand {
1415
/** {@inheritDoc} */
1516
@Override
1617
public ExitCode execute() {
17-
outputSuccess("Exiting Astra Cli");
18+
ShellPrinter.outputSuccess("Exiting Astra Cli");
1819
ExitCode.SUCCESS.exit();
1920
// Nerver reachede
2021
return ExitCode.SUCCESS;

astra-shell/src/main/java/com/datastax/astra/shell/cmd/QuitCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class QuitCommand extends BaseShellCommand {
1515
/** {@inheritDoc} */
1616
@Override
1717
public ExitCode execute() {
18-
if (null != getContext().getDatabase()) {
19-
getContext().exitDatabase();
18+
if (null != ctx().getDatabase()) {
19+
ctx().exitDatabase();
2020
return ExitCode.SUCCESS;
2121
}
2222
LoggerShell.warning("You have no base selected.");

astra-shell/src/main/java/com/datastax/astra/shell/cmd/config/ConfigCreate.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.datastax.astra.sdk.organizations.OrganizationsClient;
77
import com.datastax.astra.sdk.organizations.domain.Organization;
88
import com.datastax.astra.shell.ExitCode;
9+
import com.datastax.astra.shell.utils.ShellPrinter;
910
import com.github.rvesse.airline.annotations.Arguments;
1011
import com.github.rvesse.airline.annotations.Command;
1112
import com.github.rvesse.airline.annotations.Option;
@@ -36,11 +37,11 @@ public class ConfigCreate extends BaseConfigCommand implements Runnable {
3637
@Override
3738
public void run() {
3839
if (token == null) {
39-
outputError(ExitCode.INVALID_PARAMETER, "Please Provide a token with option -t, --token");
40+
ShellPrinter.outputError(ExitCode.INVALID_PARAMETER, "Please Provide a token with option -t, --token");
4041
ExitCode.INVALID_PARAMETER.exit();
4142
}
4243
if (!token.startsWith("AstraCS:")) {
43-
outputError(ExitCode.INVALID_PARAMETER, "Your token should start with 'AstraCS:'");
44+
ShellPrinter.outputError(ExitCode.INVALID_PARAMETER, "Your token should start with 'AstraCS:'");
4445
ExitCode.INVALID_PARAMETER.exit();
4546
}
4647

@@ -52,9 +53,9 @@ public void run() {
5253
}
5354
getAstraRc().createSectionWithToken(sectionName, token);
5455
getAstraRc().save();
55-
outputSuccess("Configuration Saved.\n");
56+
ShellPrinter.outputSuccess("Configuration Saved.\n");
5657
} catch(Exception e) {
57-
outputError(CANNOT_CONNECT, "Token provided is invalid. It was not possible to connect to Astra.");
58+
ShellPrinter.outputError(CANNOT_CONNECT, "Token provided is invalid. It was not possible to connect to Astra.");
5859
INVALID_PARAMETER.exit();
5960
}
6061
}

0 commit comments

Comments
 (0)