Skip to content

Commit a1aae7b

Browse files
committed
ShowConfig, SetDefault, logging
1 parent 4b92804 commit a1aae7b

File tree

16 files changed

+258
-181
lines changed

16 files changed

+258
-181
lines changed

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

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

3-
import com.datastax.astra.shell.cmd.HelpCustomCommand;
43
import com.datastax.astra.shell.cmd.ConfigCommand;
4+
import com.datastax.astra.shell.cmd.HelpCustomCommand;
5+
import com.datastax.astra.shell.cmd.SetDefaultOrgCommand;
6+
import com.datastax.astra.shell.cmd.ShowConfigCommand;
57
import com.datastax.astra.shell.cmd.db.ShowDatabasesCommand;
6-
import com.datastax.astra.shell.cmd.org.ShowOrganizationsCommand;
78
import com.datastax.astra.shell.cmd.shell.ShellCommand;
89
import com.datastax.astra.shell.jansi.Out;
910
import com.datastax.astra.shell.jansi.TextColor;
@@ -24,14 +25,15 @@
2425
commands = {
2526
ShellCommand.class,
2627
HelpCustomCommand.class,
27-
ConfigCommand.class
28+
ConfigCommand.class,
29+
SetDefaultOrgCommand.class
2830
},
2931

3032
groups = {
3133
@Group(
3234
name = "show",
33-
description = "Listing details of an entity or entity list",
34-
commands = { ShowOrganizationsCommand.class, ShowDatabasesCommand.class }
35+
description = "Display entity or group of entities",
36+
commands = { ShowConfigCommand.class, ShowDatabasesCommand.class, }
3537
)
3638
})
3739
public class AstraCli {

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import com.datastax.astra.shell.cmd.ExitCommand;
44
import com.datastax.astra.shell.cmd.HelpCustomCommand;
5+
import com.datastax.astra.shell.cmd.ShowConfigCommand;
56
import com.datastax.astra.shell.cmd.db.ShowDatabasesCommand;
6-
import com.datastax.astra.shell.cmd.org.ConnectCommand;
7-
import com.datastax.astra.shell.cmd.org.ShowOrganizationsCommand;
7+
import com.datastax.astra.shell.cmd.shell.ConnectCommand;
88
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;
12+
import com.github.rvesse.airline.annotations.Group;
1213
import com.github.rvesse.airline.parser.errors.ParseArgumentsUnexpectedException;
1314

1415
/**
@@ -24,11 +25,16 @@
2425
commands = {
2526
ConnectCommand.class,
2627
EmptyCommand.class,
27-
ShowDatabasesCommand.class,
28-
ShowOrganizationsCommand.class,
2928
HelpCustomCommand.class,
3029
ExitCommand.class
31-
})
30+
},
31+
groups = {
32+
@Group(
33+
name = "show",
34+
description = "Listing details of an entity or entity list",
35+
commands = { ShowConfigCommand.class, ShowDatabasesCommand.class }
36+
)
37+
})
3238
public class AstraShell {
3339

3440
public static void main(String[] args) {

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import static com.datastax.astra.shell.ExitCode.INVALID_PARAMETER;
44

5-
import javax.inject.Inject;
6-
75
import org.apache.pulsar.shade.org.apache.commons.lang.StringUtils;
86

97
import com.datastax.astra.sdk.config.AstraClientConfig;
108
import com.datastax.astra.sdk.utils.AstraRcParser;
9+
import com.datastax.astra.shell.ExitCode;
1110
import com.datastax.astra.shell.ShellContext;
1211
import com.datastax.astra.shell.jansi.Out;
13-
import com.github.rvesse.airline.HelpOption;
1412
import com.github.rvesse.airline.annotations.Option;
1513
import com.github.rvesse.airline.annotations.restrictions.MutuallyExclusiveWith;
1614

@@ -38,9 +36,6 @@ public abstract class BaseCommand<CHILD extends BaseCommand<?>> implements Runna
3836
@MutuallyExclusiveWith(tag = "authentication")
3937
protected String organization;
4038

41-
@Inject
42-
protected HelpOption<CHILD> help;
43-
4439
/** If no Organization provided we will look in ~.astrarc. */
4540
public static final String DEFAULT_ORG = "default";
4641

@@ -65,29 +60,35 @@ protected String getAstraToken() {
6560
// -org is provided lookup for token in config file
6661
if (!StringUtils.isEmpty(organization)) {
6762
lookupOrg = organization;
63+
if(!config.getSections().containsKey(lookupOrg)) {
64+
Out.error("Organization '" + lookupOrg + "' not found in config file.");
65+
INVALID_PARAMETER.exit();
66+
}
6867
}
69-
68+
7069
// Organization name is not in config file => error
71-
if(!config.getSections().containsKey(lookupOrg)) {
72-
Out.error("Organization '" + lookupOrg + "' not found in config file.");
73-
74-
INVALID_PARAMETER.exit();
75-
} else {
76-
// Org found, loading token
70+
if(config.getSections().containsKey(lookupOrg)) {
7771
astraToken = config.getSections()
7872
.get(lookupOrg)
7973
.get(AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN);
8074
}
8175
}
82-
8376
return astraToken;
8477
}
8578

8679
/** {@inheritDoc} */
8780
public void run() {
88-
// Connect to Astra first
81+
String astraToken = getAstraToken();
82+
if (null == astraToken) {
83+
System.out.println("");
84+
Out.warning("There is no token option (-t) and configuration file is empty.");
85+
Out.info("To setup the cli: astra config");
86+
Out.info("To list commands: astra help");
87+
ExitCode.INVALID_PARAMETER.exit();
88+
}
89+
8990
ShellContext ctx = ShellContext.getInstance();
90-
if (!ctx.initialized()) ctx.connect(getAstraToken());
91+
if (!ctx.initialized()) ctx.connect(astraToken);
9192

9293
// Execute custom code
9394
execute();
Lines changed: 11 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.datastax.astra.shell.cmd;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
import java.util.Map;
6-
import java.util.Optional;
7-
import java.util.Map.Entry;
83
import java.util.Scanner;
94

105
import com.datastax.astra.sdk.AstraClient;
@@ -13,14 +8,15 @@
138
import com.datastax.astra.sdk.utils.AstraRcParser;
149
import com.datastax.astra.shell.jansi.Out;
1510
import com.datastax.astra.shell.jansi.TextColor;
11+
import com.datastax.astra.shell.utils.CommandLineUtils;
1612
import com.github.rvesse.airline.annotations.Command;
1713

1814
/**
1915
* Setup the configuration
2016
*
2117
* @author Cedrick LUNVEN (@clunven)
2218
*/
23-
@Command(name = "config", description = "Ask for token and persist config in ~/.astrarc")
19+
@Command(name = "config", description = "Init configuration file ~/.astrarc")
2420
public class ConfigCommand implements Runnable {
2521

2622
/** {@inheritDoc} */
@@ -39,7 +35,7 @@ public void run() {
3935
Out.print("ENTER A TOKEN ? ", TextColor.CYAN);
4036
token = scanner.nextLine();
4137
if (!token.startsWith("AstraCS:")) {
42-
Out.error("Your token should start with 'AstraCS:'\n");
38+
Out.error("Your token should start with 'AstraCS:'");
4339
} else {
4440
try {
4541
Organization o = AstraClient
@@ -49,107 +45,31 @@ public void run() {
4945
.apiDevopsOrganizations()
5046
.organization();
5147
valid_token = true;
52-
Out.success("Valid Token, Organization '" + o.getName() + "'");
48+
Out.success("Valid Token, Organization is '" + o.getName() + "'");
5349
AstraRcParser.save(o.getName(), AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN, token);
54-
Out.success("Configuration Saved'\n");
50+
Out.success("Configuration Saved");
5551
} catch(IllegalArgumentException iexo) {
56-
Out.error("Your token seems invalid, it was not possible to connect to Astra.\n");
52+
Out.error("Your token seems invalid, it was not possible to connect to Astra.");
5753
}
5854
}
5955
}
6056

61-
showConfiguration();
57+
new ShowConfigCommand().run();
6258
System.out.println("");
6359

64-
if (askForConfirmation(scanner, "Do you want it to be your default organization")) {
60+
if (CommandLineUtils.askForConfirmation(scanner,
61+
"Do you want it to be your default organization")) {
6562
AstraRcParser.save(AstraRcParser.ASTRARC_DEFAULT,
6663
AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN, token);
6764
}
6865

6966
System.out.println("");
70-
showConfiguration();
67+
new ShowConfigCommand().run();
7168

72-
System.out.println("\nTo change default organization use: astra set-default-org <orgName>");
69+
System.out.println("\nTo change default organization: astra default-org <orgName>");
7370
}
7471
}
7572

76-
/**
77-
* This behaviour will happen again and again.
78-
*
79-
* @param message
80-
* question asked
81-
* @return
82-
* response of user
83-
*/
84-
private boolean askForConfirmation(Scanner scanner, String message) {
85-
String response = null;
86-
while (!"y".equalsIgnoreCase(response) && !"n".equalsIgnoreCase(response)) {
87-
Out.print(message + " (y/n) ? ", TextColor.CYAN);
88-
response = scanner.nextLine();
89-
}
90-
return "y".equalsIgnoreCase(response);
91-
}
92-
93-
/**
94-
* Display configuration in the shell
95-
*/
96-
private void showConfiguration() {
97-
Map<String, Map<String, String>> sections = AstraRcParser.load().getSections();
98-
List<String> orgs = listOrganizations(sections);
99-
System.out.println("There are now " + orgs.size() + " organization(s) in your configuration.");
100-
int idx = 1;
101-
for (String org : orgs) {
102-
System.out.println(idx + ": " + org);
103-
idx++;
104-
}
105-
}
106-
107-
/**
108-
* Build List as expected on screen.
109-
*
110-
* @param sections
111-
* section in AstraRc.
112-
* @return
113-
* organization list
114-
*/
115-
private List<String> listOrganizations(Map<String, Map<String, String>> sections) {
116-
List<String> returnedList = new ArrayList<>();
117-
Optional<String> defaultOrg = findDefaultOrganizationName(sections);
118-
for (Entry<String, Map<String, String>> section : sections.entrySet()) {
119-
if (AstraRcParser.ASTRARC_DEFAULT.equalsIgnoreCase(section.getKey()) && defaultOrg.isPresent()) {
120-
returnedList.add(AstraRcParser.ASTRARC_DEFAULT + " (" + defaultOrg.get() + ")");
121-
} else {
122-
returnedList.add(section.getKey());
123-
}
124-
}
125-
return returnedList;
126-
}
12773

128-
/**
129-
* Find the default org name in the configuration file.
130-
*
131-
* @param sections
132-
* list of sections
133-
* @return
134-
*/
135-
private Optional<String> findDefaultOrganizationName(Map<String, Map<String, String>> sections) {
136-
String defaultOrgName = null;
137-
if (sections.containsKey(AstraRcParser.ASTRARC_DEFAULT)) {
138-
String defaultToken = sections
139-
.get(AstraRcParser.ASTRARC_DEFAULT)
140-
.get(AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN);
141-
if (defaultToken !=null) {
142-
for (String sectionName : sections.keySet()) {
143-
if (!sectionName.equals(AstraRcParser.ASTRARC_DEFAULT)) {
144-
if (defaultToken.equalsIgnoreCase(
145-
sections.get(sectionName).get(AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN))) {
146-
defaultOrgName = sectionName;
147-
}
148-
}
149-
}
150-
}
151-
}
152-
return Optional.ofNullable(defaultOrgName);
153-
}
15474

15575
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.datastax.astra.shell.cmd;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import com.datastax.astra.sdk.config.AstraClientConfig;
8+
import com.datastax.astra.sdk.utils.AstraRcParser;
9+
import com.datastax.astra.shell.jansi.Out;
10+
import com.datastax.astra.shell.jansi.TextColor;
11+
import com.github.rvesse.airline.annotations.Arguments;
12+
import com.github.rvesse.airline.annotations.Command;
13+
import com.github.rvesse.airline.annotations.restrictions.Required;
14+
15+
/**
16+
* Class to TODO
17+
*
18+
* @author Cedrick LUNVEN (@clunven)
19+
*/
20+
@Command(
21+
name="default-org",
22+
description="Edit default organization in configuration")
23+
public class SetDefaultOrgCommand implements Runnable {
24+
25+
@Required
26+
@Arguments(title = "Organization Name", description = "The organization name to connect to")
27+
public List<String> arguments = new ArrayList<>();
28+
29+
/** {@inheritDoc} */
30+
public void run() {
31+
if (arguments.size() != 1) {
32+
Out.print("Invalid arguments, please use 'default-org <orgName>'", TextColor.RED);
33+
} else {
34+
String orgname = arguments.get(0);
35+
Map<String, Map<String, String > > sections = AstraRcParser.load().getSections();
36+
Map<String, String> section = sections.get(orgname);
37+
if (section == null) {
38+
Out.error("Organization name not found.");
39+
} else {
40+
String token = section.get(AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN);
41+
AstraRcParser.save(AstraRcParser.ASTRARC_DEFAULT,
42+
AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN, token);
43+
44+
Out.success("Configuration has been updated\n");
45+
new ShowConfigCommand().run();
46+
}
47+
}
48+
}
49+
50+
}

0 commit comments

Comments
 (0)