Skip to content

Commit 4082e58

Browse files
committed
Logger and Printer updates
1 parent 73aa1aa commit 4082e58

24 files changed

+398
-231
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.datastax.astra.shell.cmd.db.DbCreateCli;
1313
import com.datastax.astra.shell.cmd.db.DbDeleteCli;
1414
import com.datastax.astra.shell.cmd.db.DbListCli;
15+
import com.datastax.astra.shell.cmd.db.DbShow;
1516
import com.datastax.astra.shell.cmd.db.OperationsDb;
1617
import com.datastax.astra.shell.cmd.iam.RoleListCli;
1718
import com.datastax.astra.shell.cmd.iam.RoleShowCli;
@@ -43,7 +44,8 @@
4344
@Group(name = OperationsDb.DB, description = "Commands acting of database", commands = {
4445
DbCreateCli.class,
4546
DbDeleteCli.class,
46-
DbListCli.class
47+
DbListCli.class,
48+
DbShow.class
4749
}),
4850
@Group(name = "config", description = "Edit configuration file", commands = {
4951
ConfigCreate.class,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.datastax.astra.shell.cmd.db.OperationsDb;
1010
import com.datastax.astra.shell.cmd.iam.RoleListShell;
1111
import com.datastax.astra.shell.cmd.iam.RoleShowCli;
12+
import com.datastax.astra.shell.cmd.iam.UserDeleteShell;
1213
import com.datastax.astra.shell.cmd.iam.UserInviteShell;
1314
import com.datastax.astra.shell.cmd.iam.UserListShell;
1415
import com.datastax.astra.shell.cmd.iam.UserShowShell;
@@ -52,7 +53,8 @@
5253
@Group(name= "user", description = "Manage the users permission", commands = {
5354
UserListShell.class,
5455
UserShowShell.class,
55-
UserInviteShell.class
56+
UserInviteShell.class,
57+
UserDeleteShell.class
5658
}),
5759
@Group(name = "use", description = "Focus on an entity (context & prompt changed)", commands = {
5860
UseDb.class

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,22 @@ public enum ExitCode {
1111
SUCCESS(0),
1212

1313
/** code. */
14-
INVALID_PARAMETER(400),
15-
14+
PARSE_ERROR(1),
1615
/** code. */
17-
NOT_FOUND(404),
16+
INVALID_PARAMETER(10),
1817

18+
/** code. */
19+
NOT_FOUND(20),
1920
/** conflict. */
20-
CONFLICT(409),
21-
21+
CONFLICT(21),
2222
/** conflict. */
23-
ALREADY_EXIST(409),
24-
25-
/** code. */
26-
PARSE_ERROR(-1),
23+
ALREADY_EXIST(22),
2724

2825
/** code. */
29-
CANNOT_CONNECT(408),
26+
CANNOT_CONNECT(30),
3027

3128
/** Internal error. */
32-
INTERNAL_ERROR(500);
29+
INTERNAL_ERROR(40);
3330

3431
/* Exit code. */
3532
private int code;

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

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import static com.datastax.astra.shell.ExitCode.CANNOT_CONNECT;
44
import static com.datastax.astra.shell.ExitCode.INVALID_PARAMETER;
55

6+
import java.util.ArrayList;
7+
import java.util.List;
8+
69
import org.apache.commons.lang3.StringUtils;
710

811
import com.datastax.astra.sdk.config.AstraClientConfig;
@@ -14,6 +17,8 @@
1417
import com.datastax.astra.sdk.utils.AstraRc;
1518
import com.datastax.astra.shell.cmd.BaseCliCommand;
1619
import com.datastax.astra.shell.cmd.BaseCommand;
20+
import com.datastax.astra.shell.cmd.BaseShellCommand;
21+
import com.datastax.astra.shell.utils.LoggerShell;
1722

1823
/**
1924
* Hold the context of CLI to know where we are.
@@ -72,6 +77,15 @@ public static synchronized ShellContext getInstance() {
7277

7378
// -- Selection --
7479

80+
/** Current command. */
81+
private BaseCliCommand startCommand;
82+
83+
/** Current shell command (overriding Cli eventually). */
84+
private BaseShellCommand currentShellCommand;
85+
86+
/** History of commands in shell. */
87+
private List<BaseShellCommand> history = new ArrayList<>();
88+
7589
/** Organization informations (prompt). */
7690
private Organization organization;
7791

@@ -80,7 +94,7 @@ public static synchronized ShellContext getInstance() {
8094

8195
/** Database informations. */
8296
private String databaseRegion;
83-
97+
8498
/**
8599
* Valid section.
86100
*
@@ -127,7 +141,8 @@ private boolean isSectionTokenValid(BaseCommand cmd) {
127141
* command line cli
128142
*/
129143
public void init(BaseCliCommand cli) {
130-
this.token = cli.getToken();
144+
this.startCommand = cli;
145+
this.token = cli.getToken();
131146

132147
// No token = use configuration file
133148
if (this.token == null) {
@@ -150,7 +165,8 @@ public void init(BaseCliCommand cli) {
150165
}
151166

152167
if (token != null) {
153-
connect(cli, token);
168+
LoggerShell.trace("Token retrieved: " + token);
169+
connect(token);
154170
} else {
155171
INVALID_PARAMETER.exit();
156172
}
@@ -162,13 +178,13 @@ public void init(BaseCliCommand cli) {
162178
* @param token
163179
* token loaded from param
164180
*/
165-
public void connect(BaseCommand cmd, String token) {
181+
public void connect(String token) {
166182

167183
// Persist Token
168184
this.token = token;
169185

170186
if (!token.startsWith(token)) {
171-
cmd.outputError(INVALID_PARAMETER, "Token provided is invalid. It should start with 'AstraCS:...'. Try [astra setup]");
187+
startCommand.outputError(INVALID_PARAMETER, "Token provided is invalid. It should start with 'AstraCS:...'. Try [astra setup]");
172188
INVALID_PARAMETER.exit();
173189
}
174190

@@ -178,8 +194,9 @@ public void connect(BaseCommand cmd, String token) {
178194

179195
try {
180196
this.organization = apiDevopsOrganizations.organization();
197+
LoggerShell.success("Cli successfully initialized");
181198
} catch(Exception e) {
182-
cmd.outputError(CANNOT_CONNECT, "Token provided is invalid. Try [astra setup]");
199+
startCommand.outputError(CANNOT_CONNECT, "Token provided is invalid. Try [astra setup]");
183200
INVALID_PARAMETER.exit();
184201
}
185202
}
@@ -321,5 +338,45 @@ public String getConfigSection() {
321338
public AstraRc getAstraRc() {
322339
return astraRc;
323340
}
341+
342+
/**
343+
* Getter accessor for attribute 'startCommand'.
344+
*
345+
* @return
346+
* current value of 'startCommand'
347+
*/
348+
public BaseCliCommand getStartCommand() {
349+
return startCommand;
350+
}
351+
352+
/**
353+
* Getter accessor for attribute 'currentShellCommand'.
354+
*
355+
* @return
356+
* current value of 'currentShellCommand'
357+
*/
358+
public BaseShellCommand getCurrentShellCommand() {
359+
return currentShellCommand;
360+
}
361+
362+
/**
363+
* Setter accessor for attribute 'currentShellCommand'.
364+
* @param currentShellCommand
365+
* new value for 'currentShellCommand '
366+
*/
367+
public void setCurrentShellCommand(BaseShellCommand currentShellCommand) {
368+
this.currentShellCommand = currentShellCommand;
369+
this.history.add(currentShellCommand);
370+
}
371+
372+
/**
373+
* Getter accessor for attribute 'history'.
374+
*
375+
* @return
376+
* current value of 'history'
377+
*/
378+
public List<BaseShellCommand> getHistory() {
379+
return history;
380+
}
324381

325382
}

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

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

3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
6+
import org.apache.commons.lang3.StringUtils;
7+
38
import com.datastax.astra.sdk.utils.AstraRc;
9+
import com.datastax.astra.shell.ExitCode;
410
import com.datastax.astra.shell.ShellContext;
11+
import com.datastax.astra.shell.utils.LoggerShell;
512
import com.github.rvesse.airline.annotations.Option;
613

714
/**
@@ -36,19 +43,45 @@ public abstract class BaseCliCommand extends BaseShellCommand {
3643
description= "Section in configuration file (default = ~/.astrarc)")
3744
protected String configSectionName = AstraRc.ASTRARC_DEFAULT;
3845

39-
// -- Option on CLI --
40-
4146
/**
4247
* No log but provide output as a JSON
4348
*/
4449
@Option(name = { "--log"}, title = "LOG_FILE", description = "Logs will go in the file plus on console")
4550
protected String logFile;
4651

52+
/**
53+
* Reference to write data into log file.
54+
*/
55+
protected FileWriter logFileWriter;
56+
57+
/**
58+
* If logger is required, write in dedicated file.
59+
*/
60+
private void initLogFile() {
61+
if (!StringUtils.isEmpty(logFile)) {
62+
try {
63+
logFileWriter = new FileWriter(logFile, true);
64+
} catch (IOException e) {
65+
System.out.println("[ERROR] - Cannot open log file " + logFile + ":" + e.getMessage());
66+
ExitCode.INVALID_PARAMETER.exit();
67+
}
68+
}
69+
}
70+
4771
/** {@inheritDoc} */
4872
@Override
4973
public void run() {
74+
// Initialization of Logger
75+
initLogFile();
76+
5077
// Initialization of context
5178
ShellContext.getInstance().init(this);
79+
80+
LoggerShell.trace("Command " + getClass().getName());
81+
LoggerShell.trace("Token: " + token);
82+
LoggerShell.trace("ConfigFilename: " + configFilename);
83+
LoggerShell.trace("ConfigSectionName: " + configSectionName);
84+
5285
// Execute command and exit program
5386
execute().exit();
5487
}
@@ -92,6 +125,17 @@ public String getConfigSectionName() {
92125
public String getLogFile() {
93126
return logFile;
94127
}
128+
129+
130+
/**
131+
* Getter accessor for attribute 'logFileWriter'.
132+
*
133+
* @return
134+
* current value of 'logFileWriter'
135+
*/
136+
public FileWriter getLogFileWriter() {
137+
return logFileWriter;
138+
}
95139

96140

97141
}

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.datastax.astra.shell.cmd;
22

33
import java.util.Arrays;
4-
import java.util.Map;
54
import java.util.HashMap;
5+
import java.util.Map;
66

77
import com.datastax.astra.shell.ExitCode;
88
import com.datastax.astra.shell.output.CsvOutput;
99
import com.datastax.astra.shell.output.JsonOutput;
1010
import com.datastax.astra.shell.output.OutputFormat;
1111
import com.datastax.astra.shell.utils.LoggerShell;
12+
import com.datastax.astra.shell.utils.ShellPrinter;
1213
import com.github.rvesse.airline.annotations.Option;
1314

1415
/**
@@ -35,8 +36,14 @@ public abstract class BaseCommand implements Runnable {
3536
/**
3637
* Each command can have a verbose mode.
3738
**/
38-
@Option(name = { "--debug" }, description = "Enter Debug mode")
39-
protected boolean debug = false;
39+
@Option(name = { "--verbose" }, description = "Enter Debug mode")
40+
protected boolean verbose = false;
41+
42+
/**
43+
* Each command can have a verbose mode.
44+
**/
45+
@Option(name = { "--no-color" }, description = "Remove all colors in output")
46+
protected boolean noColor = false;
4047

4148
/**
4249
* No log but provide output as a JSON
@@ -57,10 +64,10 @@ public abstract class BaseCommand implements Runnable {
5764
public void outputError(ExitCode code, String msg) {
5865
switch(format) {
5966
case json:
60-
LoggerShell.json(new JsonOutput(code, code.name() + ": " + msg));
67+
ShellPrinter.printJson(new JsonOutput(code, code.name() + ": " + msg));
6168
break;
6269
case csv:
63-
LoggerShell.csv(new CsvOutput(code, code.name() + ": " + msg));
70+
ShellPrinter.printCsv(new CsvOutput(code, code.name() + ": " + msg));
6471
break;
6572
case human:
6673
default:
@@ -80,14 +87,14 @@ public void outputError(ExitCode code, String msg) {
8087
public void outputWarning(ExitCode code, String msg) {
8188
switch(format) {
8289
case json:
83-
LoggerShell.json(new JsonOutput(code, code.name() + ": " + msg));
90+
ShellPrinter.printJson(new JsonOutput(code, code.name() + ": " + msg));
8491
break;
8592
case csv:
86-
LoggerShell.csv(new CsvOutput(code, code.name() + ": " + msg));
93+
ShellPrinter.printCsv(new CsvOutput(code, code.name() + ": " + msg));
8794
break;
8895
case human:
8996
default:
90-
LoggerShell.warning( code.name() + ": " + msg);
97+
LoggerShell.warning(code.name() + ": " + msg);
9198
break;
9299
}
93100
}
@@ -101,12 +108,12 @@ public void outputWarning(ExitCode code, String msg) {
101108
public void outputData(String label, String data) {
102109
switch(format) {
103110
case json:
104-
LoggerShell.json(new JsonOutput(ExitCode.SUCCESS, label, data));
111+
ShellPrinter.printJson(new JsonOutput(ExitCode.SUCCESS, label, data));
105112
break;
106113
case csv:
107114
Map<String, String> m = new HashMap<>();
108115
m.put(label, data);
109-
LoggerShell.csv(new CsvOutput(Arrays.asList(label), Arrays.asList(m)));
116+
ShellPrinter.printCsv(new CsvOutput(Arrays.asList(label), Arrays.asList(m)));
110117
break;
111118
case human:
112119
default:
@@ -124,10 +131,10 @@ public void outputData(String label, String data) {
124131
public void outputSuccess(String msg) {
125132
switch(format) {
126133
case json:
127-
LoggerShell.json(new JsonOutput(ExitCode.SUCCESS, msg));
134+
ShellPrinter.printJson(new JsonOutput(ExitCode.SUCCESS, msg));
128135
break;
129136
case csv:
130-
LoggerShell.csv(new CsvOutput(ExitCode.SUCCESS, msg));
137+
ShellPrinter.printCsv(new CsvOutput(ExitCode.SUCCESS, msg));
131138
break;
132139
case human:
133140
default:
@@ -152,8 +159,8 @@ public OutputFormat getFormat() {
152159
* @return
153160
* current value of 'debug'
154161
*/
155-
public boolean isDebug() {
156-
return debug;
162+
public boolean isVerbose() {
163+
return verbose;
157164
}
158165

159166

0 commit comments

Comments
 (0)