Skip to content

Commit 570e0d0

Browse files
committed
--log working
1 parent 4082e58 commit 570e0d0

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ public static void main(String[] args) {
8585
try {
8686

8787
// Enable Colored outputs
88-
AnsiConsole.systemInstall();
88+
//AnsiConsole.systemInstall();
89+
90+
// Save the command line to log it later
91+
ShellContext.getInstance().setRawCommand(args);
8992

9093
// Command Line Interface
9194
new com.github.rvesse.airline.Cli<Runnable>(AstraCli.class)

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static com.datastax.astra.shell.ExitCode.INVALID_PARAMETER;
55

66
import java.util.ArrayList;
7+
import java.util.Arrays;
78
import java.util.List;
89

910
import org.apache.commons.lang3.StringUtils;
@@ -83,6 +84,9 @@ public static synchronized ShellContext getInstance() {
8384
/** Current shell command (overriding Cli eventually). */
8485
private BaseShellCommand currentShellCommand;
8586

87+
/** Raw command. */
88+
private List<String> rawCommand = new ArrayList<>();
89+
8690
/** History of commands in shell. */
8791
private List<BaseShellCommand> history = new ArrayList<>();
8892

@@ -142,19 +146,25 @@ private boolean isSectionTokenValid(BaseCommand cmd) {
142146
*/
143147
public void init(BaseCliCommand cli) {
144148
this.startCommand = cli;
149+
LoggerShell.info("-----------------------------------------------------");
150+
LoggerShell.info("Command : astra " + String.join(" ", ShellContext.getInstance().getRawCommand()));
151+
LoggerShell.info("Class : " + cli.getClass());
145152
this.token = cli.getToken();
146153

147154
// No token = use configuration file
148155
if (this.token == null) {
149156
// Overriding default config
150157
if (cli.getConfigFilename() != null) {
158+
LoggerShell.trace("ConfigFilename: " + cli.getConfigFilename());
151159
this.astraRc = new AstraRc(cli.getConfigFilename());
152160
} else {
161+
LoggerShell.trace("ConfigFilename: " + AstraRc.getDefaultConfigurationFileName());
153162
this.astraRc = new AstraRc();
154163
}
155164
// Overriding default section
156165
if (!StringUtils.isEmpty(cli.getConfigSectionName())) {
157166
this.configSection = cli.getConfigSectionName();
167+
LoggerShell.trace("ConfigSectionName: " + configSection);
158168
}
159169

160170
if (isSectionValid(cli) && isSectionTokenValid(cli)) {
@@ -378,5 +388,25 @@ public void setCurrentShellCommand(BaseShellCommand currentShellCommand) {
378388
public List<BaseShellCommand> getHistory() {
379389
return history;
380390
}
391+
392+
/**
393+
* Getter accessor for attribute 'rawCommand'.
394+
*
395+
* @return
396+
* current value of 'rawCommand'
397+
*/
398+
public List<String> getRawCommand() {
399+
return rawCommand;
400+
}
401+
402+
/**
403+
* Setter accessor for attribute 'rawCommand'.
404+
*
405+
* @param args
406+
* input arguments for the command.
407+
*/
408+
public void setRawCommand(String... args) {
409+
this.rawCommand = Arrays.asList(args);
410+
}
381411

382412
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.datastax.astra.sdk.utils.AstraRc;
99
import com.datastax.astra.shell.ExitCode;
1010
import com.datastax.astra.shell.ShellContext;
11-
import com.datastax.astra.shell.utils.LoggerShell;
1211
import com.github.rvesse.airline.annotations.Option;
1312

1413
/**
@@ -72,16 +71,11 @@ private void initLogFile() {
7271
@Override
7372
public void run() {
7473
// Initialization of Logger
75-
initLogFile();
76-
74+
initLogFile();
75+
7776
// Initialization of context
7877
ShellContext.getInstance().init(this);
7978

80-
LoggerShell.trace("Command " + getClass().getName());
81-
LoggerShell.trace("Token: " + token);
82-
LoggerShell.trace("ConfigFilename: " + configFilename);
83-
LoggerShell.trace("ConfigSectionName: " + configSectionName);
84-
8579
// Execute command and exit program
8680
execute().exit();
8781
}

astra-shell/src/main/java/com/datastax/astra/shell/utils/LoggerShell.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private static void logToFile(String level, String text) {
5353
+ " - "
5454
+ StringUtils.rightPad(level, 5)
5555
+ " - " + text + System.lineSeparator());
56+
cli.getLogFileWriter().flush();
5657
} catch (IOException e) {
5758
System.out.println("Writes in log file failed: " + e.getMessage());
5859
}
@@ -112,7 +113,7 @@ public static void warning(String text) {
112113
*/
113114
public static void trace(String text) {
114115
if (isVerbose()) {
115-
System.out.println(ansi().fg(YELLOW).a("[DEBUG ] - ").reset().a(text));
116+
System.out.println(ansi().fg(YELLOW).a("[DEBUG] - ").reset().a(text));
116117
}
117118
logToFile("DEBUG", text);
118119
}

astra-shell/src/test/java/com/datastax/astra/RolesCommandsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void interactive() throws Exception {
1717

1818
@Test
1919
public void listRoles() throws Exception {
20-
astraCli("role", "list", "--verbose" /*, "-f", "csv"*/);
20+
astraCli("role", "list", /*"-f", "csv" , "--verbose",*/ "--log", "/Users/cedricklunven/Downloads/log2.txt");
2121
}
2222

2323
@Test

0 commit comments

Comments
 (0)