Skip to content

Commit 4b92804

Browse files
committed
OK
1 parent 685fd2c commit 4b92804

File tree

16 files changed

+333
-77
lines changed

16 files changed

+333
-77
lines changed

astra-sdk/src/main/java/com/datastax/astra/sdk/config/AstraClientConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.datastax.astra.sdk.config;
22

3-
import static com.datastax.astra.sdk.utils.AstraRc.readRcVariable;
3+
import static com.datastax.astra.sdk.utils.AstraRcParser.readRcVariable;
44
import static com.datastax.stargate.sdk.utils.Assert.hasLength;
55
import static com.datastax.stargate.sdk.utils.Assert.notNull;
66
import static com.datastax.stargate.sdk.utils.Utils.readEnvVariable;
@@ -16,7 +16,7 @@
1616
import org.slf4j.LoggerFactory;
1717

1818
import com.datastax.astra.sdk.AstraClient;
19-
import com.datastax.astra.sdk.utils.AstraRc;
19+
import com.datastax.astra.sdk.utils.AstraRcParser;
2020
import com.datastax.oss.driver.api.core.ConsistencyLevel;
2121
import com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder;
2222
import com.datastax.oss.driver.api.core.config.TypedDriverOption;
@@ -924,8 +924,8 @@ public AstraClientConfig() {
924924
stargateConfig = new StargateClientConfig();
925925

926926
// Loading ~/.astrarc section default if present
927-
if (AstraRc.exists()) {
928-
loadFromAstraRc(AstraRc.load(), AstraRc.ASTRARC_DEFAULT);
927+
if (AstraRcParser.exists()) {
928+
loadFromAstraRc(AstraRcParser.load(), AstraRcParser.ASTRARC_DEFAULT);
929929
}
930930

931931
// Authentication
@@ -951,7 +951,7 @@ public AstraClientConfig() {
951951
* @param sectionName String
952952
* @return AstraClientBuilder
953953
*/
954-
public AstraClientConfig loadFromAstraRc(AstraRc arc, String sectionName) {
954+
public AstraClientConfig loadFromAstraRc(AstraRcParser arc, String sectionName) {
955955
notNull(arc, "AstraRc");
956956
hasLength(sectionName, "sectionName");
957957
readRcVariable(arc, ASTRA_DB_ID, sectionName).ifPresent(this::withDatabaseId);

astra-sdk/src/main/java/com/datastax/astra/sdk/utils/AstraRc.java renamed to astra-sdk/src/main/java/com/datastax/astra/sdk/utils/AstraRcParser.java

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
*
4646
* @author Cedrick LUNVEN (@clunven)
4747
*/
48-
public class AstraRc {
48+
public class AstraRcParser {
4949

5050
/** Logger for our Client. */
51-
private static final Logger LOGGER = LoggerFactory.getLogger(AstraRc.class);
51+
private static final Logger LOGGER = LoggerFactory.getLogger(AstraRcParser.class);
5252

5353
/** Default filename we are looking for. */
5454
public static final String ASTRARC_FILENAME = ".astrarc";
@@ -67,12 +67,12 @@ public class AstraRc {
6767

6868
/** Sections in the file. [sectionName] -> key=Value. */
6969
private final Map<String, Map<String, String>> sections;
70-
70+
7171
/**
7272
* Load from ~/.astrarc
7373
*/
74-
public AstraRc() {
75-
this.sections = AstraRc.load().getSections();
74+
public AstraRcParser() {
75+
this.sections = AstraRcParser.load().getSections();
7676
}
7777

7878
/**
@@ -81,8 +81,8 @@ public AstraRc() {
8181
* @param fileName
8282
* String
8383
*/
84-
public AstraRc(String fileName) {
85-
this.sections = AstraRc.load(fileName).getSections();
84+
public AstraRcParser(String fileName) {
85+
this.sections = AstraRcParser.load(fileName).getSections();
8686
}
8787

8888
/**
@@ -91,7 +91,7 @@ public AstraRc(String fileName) {
9191
* @param s
9292
* Map
9393
*/
94-
public AstraRc(Map<String, Map<String, String>> s) {
94+
public AstraRcParser(Map<String, Map<String, String>> s) {
9595
this.sections = s;
9696
}
9797

@@ -132,7 +132,36 @@ public String read(String sectionName, String key) {
132132
* @return File
133133
*/
134134
public static boolean exists() {
135-
return new File(System.getProperty(ENV_USER_HOME) + File.separator + ASTRARC_FILENAME).exists();
135+
return getDefaultConfigFile().exists();
136+
}
137+
138+
/**
139+
* Provide the configuration {@link File}.
140+
*
141+
* @return
142+
* config file.
143+
*/
144+
public static File getDefaultConfigFile() {
145+
return new File(System.getProperty(ENV_USER_HOME) + File.separator + ASTRARC_FILENAME);
146+
}
147+
148+
/**
149+
* Create configuration file if not exist.
150+
*
151+
* @return
152+
* if the file has been created
153+
*/
154+
public static boolean createIfNotExists() {
155+
File f = new File(System.getProperty(ENV_USER_HOME) + File.separator + ASTRARC_FILENAME);
156+
if (!f.exists()) {
157+
try {
158+
return f.createNewFile();
159+
} catch (IOException e) {
160+
throw new IllegalStateException("Cannot save configuration file in home directory " +
161+
System.getProperty(ENV_USER_HOME));
162+
}
163+
}
164+
return false;
136165
}
137166

138167
/**
@@ -235,22 +264,23 @@ public static void save(Map<String, Map<String, String>> astraRc) {
235264
*
236265
* @return AstraRc
237266
*/
238-
public static AstraRc load() {
239-
return load(System.getProperty(ENV_USER_HOME) + File.separator + ASTRARC_FILENAME);
267+
public static AstraRcParser load() {
268+
createIfNotExists();
269+
return load(getDefaultConfigFile().getAbsolutePath());
240270
}
241271

242272
/**
243-
* Loading ~/.astrarc (if present). Key = block name (dbname of default), then key/value
244-
*
245-
* @param fileName
246-
* String
247-
* @return AstraRc
273+
* Load configuration file.
274+
*
275+
* @param file
276+
* configuration file
277+
* @return
278+
* parser.
248279
*/
249-
public static AstraRc load(String fileName) {
280+
public static AstraRcParser load(File file) {
250281
Map<String, Map<String, String>> sections = new HashMap<>();
251-
File current = new File(fileName);
252-
try (Scanner scanner = new Scanner(current)) {
253-
if (current.exists()) {
282+
try (Scanner scanner = new Scanner(file)) {
283+
if (file.exists()) {
254284
String sectionName = "";
255285
while (scanner.hasNextLine()) {
256286
String line = scanner.nextLine();
@@ -262,7 +292,7 @@ public static AstraRc load(String fileName) {
262292
int off = line.indexOf("=");
263293
if (off < 0) {
264294
throw new IllegalArgumentException(
265-
"Cannot parse file " + fileName + ", line '" + line + "' invalid format expecting key=value");
295+
"Cannot parse file " + file.getName() + ", line '" + line + "' invalid format expecting key=value");
266296
}
267297
String key = line.substring(0, off);
268298
String val = line.substring(off + 1);
@@ -274,7 +304,19 @@ public static AstraRc load(String fileName) {
274304
} catch (FileNotFoundException e) {
275305
throw new IllegalArgumentException("Cannot read configuration file", e);
276306
}
277-
return new AstraRc(sections);
307+
return new AstraRcParser(sections);
308+
}
309+
310+
/**
311+
* Loading ~/.astrarc (if present). Key = block name (dbname of default), then key/value
312+
*
313+
* @param fileName
314+
* String
315+
* @return AstraRc
316+
*/
317+
public static AstraRcParser load(String fileName) {
318+
return load(new File(fileName));
319+
278320
}
279321

280322
/**
@@ -349,7 +391,7 @@ private static Map<String, String> dbKeys(Database db, String token) {
349391
* section Name
350392
* @return if the value is there
351393
*/
352-
public static Optional<String> readRcVariable(AstraRc arc, String key, String sectionName) {
394+
public static Optional<String> readRcVariable(AstraRcParser arc, String key, String sectionName) {
353395
Map<String, String> section = arc.getSections().get(sectionName);
354396
if (section != null && section.containsKey(key) && Utils.hasLength(section.get(key))) {
355397
return Optional.ofNullable(section.get(key));

astra-shell/pom.xml

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
</properties>
2323

2424
<dependencies>
25-
26-
<!-- Parsing CLI -->
25+
26+
<!-- Parsing CLI -->
2727
<dependency>
2828
<groupId>com.github.rvesse</groupId>
2929
<artifactId>airline</artifactId>
@@ -34,7 +34,7 @@
3434
<artifactId>airline-help-bash</artifactId>
3535
<version>${airline.version}</version>
3636
</dependency>
37-
37+
3838
<!-- Leveraging SDK for Astra Accesses -->
3939
<dependency>
4040
<groupId>com.datastax.astra</groupId>
@@ -46,7 +46,7 @@
4646
<artifactId>jansi</artifactId>
4747
<version>${jansi.version}</version>
4848
</dependency>
49-
49+
5050
<!-- Commons -->
5151
<dependency>
5252
<groupId>org.apache.commons</groupId>
@@ -132,6 +132,41 @@
132132
</execution>
133133
</executions>
134134
</plugin>
135+
136+
<plugin>
137+
<groupId>com.github.rvesse</groupId>
138+
<artifactId>airline-maven-plugin</artifactId>
139+
<version>${airline.version}</version>
140+
<configuration>
141+
<formats>
142+
<format>MAN</format>
143+
<format>CLI</format>
144+
<format>MARKDOWN</format>
145+
</formats>
146+
<sources>
147+
<source>
148+
<classes>
149+
<class>com.datastax.astra.shell.AstraCli</class>
150+
</classes>
151+
</source>
152+
<source>
153+
<classes>
154+
<class>com.datastax.astra.shell.AstraShell</class>
155+
</classes>
156+
</source>
157+
</sources>
158+
</configuration>
159+
<executions>
160+
<execution>
161+
<phase>package</phase>
162+
<goals>
163+
<goal>validate</goal>
164+
<goal>generate</goal>
165+
</goals>
166+
</execution>
167+
</executions>
168+
</plugin>
169+
135170
</plugins>
136171
</build>
137172
</project>

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.datastax.astra.shell;
22

33
import com.datastax.astra.shell.cmd.HelpCustomCommand;
4+
import com.datastax.astra.shell.cmd.ConfigCommand;
45
import com.datastax.astra.shell.cmd.db.ShowDatabasesCommand;
56
import com.datastax.astra.shell.cmd.org.ShowOrganizationsCommand;
6-
import com.datastax.astra.shell.cmd.repl.ReplCommand;
7+
import com.datastax.astra.shell.cmd.shell.ShellCommand;
8+
import com.datastax.astra.shell.jansi.Out;
9+
import com.datastax.astra.shell.jansi.TextColor;
710
import com.datastax.astra.shell.utils.ShellPrinter;
811
import com.github.rvesse.airline.annotations.Cli;
912
import com.github.rvesse.airline.annotations.Group;
13+
import com.github.rvesse.airline.parser.errors.ParseArgumentsUnexpectedException;
1014

1115
/**
1216
* Main class for the program. Will route commands to proper class
@@ -16,10 +20,11 @@
1620
@Cli(
1721
name = "astra",
1822
description = "CLI for DataStax Astra™ including an interactive mode",
19-
defaultCommand = ReplCommand.class, // no command provided => REPL
23+
defaultCommand = ShellCommand.class, // no command => interactive
2024
commands = {
21-
ReplCommand.class,
22-
HelpCustomCommand.class
25+
ShellCommand.class,
26+
HelpCustomCommand.class,
27+
ConfigCommand.class
2328
},
2429

2530
groups = {
@@ -41,13 +46,21 @@ public class AstraCli {
4146
*/
4247
public static void main(String[] args) {
4348

44-
// Show Banner
45-
ShellPrinter.banner();
46-
47-
// Command Line Interface
48-
new com.github.rvesse.airline.Cli<Runnable>(AstraCli.class)
49-
.parse(args) // Find the processor for the command
50-
.run(); // Run the command
49+
try {
50+
51+
// Show Banner
52+
ShellPrinter.banner();
53+
54+
// Command Line Interface
55+
new com.github.rvesse.airline.Cli<Runnable>(AstraCli.class)
56+
.parse(args) // Find the processor for the command
57+
.run(); // Run the command
58+
59+
} catch(ParseArgumentsUnexpectedException ex) {
60+
Out.println("Invalid command: " + ex.getMessage(), TextColor.RED);
61+
} catch(Exception e) {
62+
Out.println("Execution error:" + e.getMessage(), TextColor.RED);
63+
}
5164
}
5265

5366
/**

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

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

3+
import com.datastax.astra.shell.cmd.ExitCommand;
34
import com.datastax.astra.shell.cmd.HelpCustomCommand;
45
import com.datastax.astra.shell.cmd.db.ShowDatabasesCommand;
56
import com.datastax.astra.shell.cmd.org.ConnectCommand;
67
import com.datastax.astra.shell.cmd.org.ShowOrganizationsCommand;
7-
import com.datastax.astra.shell.cmd.repl.EmptyCommand;
8-
import com.datastax.astra.shell.cmd.repl.ExitCommand;
8+
import com.datastax.astra.shell.cmd.shell.EmptyCommand;
99
import com.datastax.astra.shell.jansi.Out;
1010
import com.datastax.astra.shell.jansi.TextColor;
1111
import com.github.rvesse.airline.annotations.Cli;
@@ -17,7 +17,7 @@
1717
* @author Cedrick LUNVEN (@clunven)
1818
*/
1919
@Cli(
20-
name = "$prompt:",
20+
name = "shell",
2121
description = "Interactive Shell for DataStax Astra",
2222
defaultCommand =
2323
EmptyCommand.class,
@@ -42,12 +42,10 @@ public static void main(String[] args) {
4242
cmd.run();
4343

4444
} catch(ParseArgumentsUnexpectedException ex) {
45-
Out.println("Invalid command", TextColor.RED);
46-
ex.printStackTrace();
45+
Out.println("Invalid command: " + ex.getMessage(), TextColor.RED);
4746

4847
} catch(Exception e) {
4948
Out.println("An error occured during exection " + e.getMessage(), TextColor.RED);
50-
e.printStackTrace();
5149

5250
}
5351
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.datastax.astra.sdk.config.AstraClientConfig;
99
import com.datastax.astra.sdk.organizations.OrganizationsClient;
1010
import com.datastax.astra.sdk.organizations.domain.Organization;
11-
import com.datastax.astra.sdk.utils.AstraRc;
11+
import com.datastax.astra.sdk.utils.AstraRcParser;
1212
import com.datastax.astra.shell.jansi.Out;
1313
import com.datastax.oss.driver.api.core.CqlSession;
1414

@@ -96,7 +96,7 @@ public void connect(String token) {
9696
}
9797

9898
// Update ~/.astrarc
99-
AstraRc.save(
99+
AstraRcParser.save(
100100
getOrganization().getName(),
101101
AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN,
102102
token);

0 commit comments

Comments
 (0)