|
1 | 1 | package com.datastax.astra.shell.cmd.config;
|
2 | 2 |
|
| 3 | +import static org.fusesource.jansi.Ansi.ansi; |
| 4 | + |
3 | 5 | import java.util.Scanner;
|
4 | 6 |
|
5 | 7 | import org.fusesource.jansi.Ansi;
|
| 8 | +import org.fusesource.jansi.Ansi.Color; |
6 | 9 |
|
7 |
| -import com.datastax.astra.sdk.AstraClient; |
8 |
| -import com.datastax.astra.sdk.config.AstraClientConfig; |
| 10 | +import com.datastax.astra.sdk.organizations.OrganizationsClient; |
9 | 11 | import com.datastax.astra.shell.cmd.show.ShowConfigsCommand;
|
10 | 12 | import com.datastax.astra.shell.utils.LoggerShell;
|
11 | 13 | import com.datastax.astra.shell.utils.ShellPrinter;
|
12 |
| -import com.github.rvesse.airline.annotations.Arguments; |
13 | 14 | import com.github.rvesse.airline.annotations.Command;
|
14 | 15 |
|
15 | 16 | /**
|
|
22 | 23 | description = "Intialize configuration")
|
23 | 24 | public class Setup extends BaseConfigCommand implements Runnable {
|
24 | 25 |
|
25 |
| - /** |
26 |
| - * Section in configuration file to as as default. |
27 |
| - */ |
28 |
| - @Arguments( |
29 |
| - title = "section", |
30 |
| - description = "Section in configuration file to as as defulat.") |
31 |
| - protected String sectionName; |
32 |
| - |
33 | 26 | /** {@inheritDoc} */
|
34 | 27 | @Override
|
35 | 28 | public void run() {
|
| 29 | + System.out.print(ansi().eraseScreen().reset()); |
36 | 30 | ShellPrinter.banner();
|
37 |
| - System.out.println("+-------------------------------+"); |
38 |
| - System.out.println("+- Setup. -+"); |
39 |
| - System.out.println("+-------------------------------+"); |
40 |
| - |
41 |
| - System.out.println("\nWelcome to Astra Shell/CLI. We will guide you to start."); |
42 |
| - |
43 |
| - LoggerShell.println("\nHow it works ?\n", Ansi.Color.CYAN); |
44 |
| - System.out.println("Astra Cli and shell (interactive) leverage a configuration file (~/.astrarc) avoiding users to have to enter credentials each time. " |
45 |
| - + "The file is divided in sections identified by a name. If user does not specify section name the [default] is used." |
46 |
| - + " For each section, key 'ASTRA_DB_APPLICATION_TOKEN' is mandatory: it is the authentication token " |
47 |
| - + " used to invoke Astra Apis. More keys can be added to change scope or settings. Here is a sample file:"); |
48 |
| - |
49 |
| - System.out.println("\n[default]"); |
50 |
| - System.out.println(AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN + "=AstraCS:aaaaa......"); |
51 |
| - System.out.println("\n[my_dev_env]"); |
52 |
| - System.out.println(AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN + "=AstraCS:abcde......"); |
53 |
| - System.out.println(AstraClientConfig.ASTRA_DB_ID + "=924e6ab3-eeb5-45e1-9861-5abcdc62f343"); |
54 |
| - System.out.println(AstraClientConfig.ASTRA_DB_REGION + "=europe-west-1"); |
55 |
| - System.out.println("\n[my_prod_env]"); |
56 |
| - System.out.println(AstraClientConfig.ASTRA_DB_APPLICATION_TOKEN + "=AstraCS:12345......"); |
57 |
| - System.out.println(AstraClientConfig.ASTRA_DB_ID + "=924e6ab3-eeb5-45e1-9861-5abcdc62f34444"); |
58 |
| - System.out.println(AstraClientConfig.ASTRA_DB_REGION + "=europe-west-1"); |
59 |
| - |
60 |
| - LoggerShell.println("\nGetting Started\n", Ansi.Color.CYAN); |
61 |
| - System.out.println("You need an Astra token, here is the procedure to create one :\nhttps://awesome-astra.github.io/docs/pages/astra/create-token/"); |
| 31 | + System.out.println(); |
| 32 | + LoggerShell.print("+-------------------------------+\n", Color.CYAN); |
| 33 | + LoggerShell.print("+- Setup. -+\n", Color.CYAN); |
| 34 | + LoggerShell.print("+-------------------------------+\n", Color.CYAN); |
| 35 | + System.out.println("\nWelcome to Astra Shell. We will guide you to start."); |
62 | 36 |
|
63 |
| - System.out.println("\nWe will now create a section. " |
64 |
| - + "(if first, will be set as default)."); |
| 37 | + LoggerShell.println("\n[Astra Setup]\n", Ansi.Color.CYAN); |
| 38 | + System.out.println("To use the cli you have to:"); |
| 39 | + System.out.println(" • Create an Astra account on : https://astra.datastax.com"); |
| 40 | + System.out.println(" • Create an authentication token following: https://dtsx.io/create-astra-token"); |
65 | 41 |
|
| 42 | + LoggerShell.println("\n[Cli Setup]\n", Ansi.Color.CYAN); |
| 43 | + System.out.println("You will be asked to enter your token, it will be saved locally."); |
66 | 44 | String token = null;
|
67 | 45 | try(Scanner scanner = new Scanner(System.in)) {
|
68 | 46 | boolean valid_token = false;
|
69 | 47 | while (!valid_token) {
|
70 |
| - LoggerShell.print("\n - Enter a token (eg: AstraCS...) : ", Ansi.Color.CYAN); |
| 48 | + LoggerShell.print("\n• Enter your token (AstraCS...) : ", Ansi.Color.MAGENTA); |
71 | 49 | token = scanner.nextLine();
|
72 | 50 | if (!token.startsWith("AstraCS:")) {
|
73 | 51 | LoggerShell.error("Your token should start with 'AstraCS:'");
|
74 | 52 | } else {
|
75 | 53 | try {
|
76 |
| - AstraClient.builder() |
77 |
| - .withToken(token) |
78 |
| - .build() |
79 |
| - .apiDevopsOrganizations() |
80 |
| - .organization(); |
81 |
| - valid_token = true; |
| 54 | + ; |
82 | 55 | ConfigCreate ccc = new ConfigCreate();
|
83 | 56 | ccc.token = token;
|
| 57 | + ccc.sectionName = new OrganizationsClient(token).organization().getName(); |
84 | 58 | ccc.run();
|
| 59 | + valid_token = true; |
| 60 | + |
| 61 | + ShowConfigsCommand configs = new ShowConfigsCommand(); |
| 62 | + configs.astraRc = this.astraRc; |
| 63 | + configs.configFilename = this.configFilename; |
| 64 | + configs.run(); |
85 | 65 |
|
86 |
| - } catch(IllegalArgumentException iexo) { |
87 |
| - LoggerShell.error("Your token seems invalid, it was not possible to connect to Astra."); |
| 66 | + } catch(Exception e) { |
| 67 | + LoggerShell.error("Token provided is invalid. Please enter a valid token or quit with CTRL+C"); |
88 | 68 | }
|
89 | 69 | }
|
90 | 70 | }
|
91 |
| - |
92 |
| - ShowConfigsCommand configs = new ShowConfigsCommand(); |
93 |
| - configs.astraRc = this.astraRc; |
94 |
| - configs.configFilename = this.configFilename; |
95 |
| - configs.run(); |
96 |
| - System.out.println(""); |
97 | 71 |
|
98 |
| - System.out.println("\nTo change default organization: astra config default <section>"); |
| 72 | + LoggerShell.println("\n[What's NEXT ?]\n", Ansi.Color.CYAN); |
| 73 | + System.out.println("You are all set you can now:"); |
| 74 | + System.out.println(" • Use any command, 'astra help' will get you the list"); |
| 75 | + System.out.println(" • Try with 'astra db list'"); |
| 76 | + System.out.println(" • Enter interactive mode using 'astra'"); |
| 77 | + System.out.println("\nHappy Coding !"); |
| 78 | + System.out.println(""); |
99 | 79 | }
|
100 | 80 | }
|
101 | 81 |
|
|
0 commit comments