1
1
package com .datastax .astra .shell .cmd ;
2
2
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 ;
8
3
import java .util .Scanner ;
9
4
10
5
import com .datastax .astra .sdk .AstraClient ;
13
8
import com .datastax .astra .sdk .utils .AstraRcParser ;
14
9
import com .datastax .astra .shell .jansi .Out ;
15
10
import com .datastax .astra .shell .jansi .TextColor ;
11
+ import com .datastax .astra .shell .utils .CommandLineUtils ;
16
12
import com .github .rvesse .airline .annotations .Command ;
17
13
18
14
/**
19
15
* Setup the configuration
20
16
*
21
17
* @author Cedrick LUNVEN (@clunven)
22
18
*/
23
- @ Command (name = "config" , description = "Ask for token and persist config in ~/.astrarc" )
19
+ @ Command (name = "config" , description = "Init configuration file ~/.astrarc" )
24
20
public class ConfigCommand implements Runnable {
25
21
26
22
/** {@inheritDoc} */
@@ -39,7 +35,7 @@ public void run() {
39
35
Out .print ("ENTER A TOKEN ? " , TextColor .CYAN );
40
36
token = scanner .nextLine ();
41
37
if (!token .startsWith ("AstraCS:" )) {
42
- Out .error ("Your token should start with 'AstraCS:'\n " );
38
+ Out .error ("Your token should start with 'AstraCS:'" );
43
39
} else {
44
40
try {
45
41
Organization o = AstraClient
@@ -49,107 +45,31 @@ public void run() {
49
45
.apiDevopsOrganizations ()
50
46
.organization ();
51
47
valid_token = true ;
52
- Out .success ("Valid Token, Organization '" + o .getName () + "'" );
48
+ Out .success ("Valid Token, Organization is '" + o .getName () + "'" );
53
49
AstraRcParser .save (o .getName (), AstraClientConfig .ASTRA_DB_APPLICATION_TOKEN , token );
54
- Out .success ("Configuration Saved' \n " );
50
+ Out .success ("Configuration Saved" );
55
51
} 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." );
57
53
}
58
54
}
59
55
}
60
56
61
- showConfiguration ();
57
+ new ShowConfigCommand (). run ();
62
58
System .out .println ("" );
63
59
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" )) {
65
62
AstraRcParser .save (AstraRcParser .ASTRARC_DEFAULT ,
66
63
AstraClientConfig .ASTRA_DB_APPLICATION_TOKEN , token );
67
64
}
68
65
69
66
System .out .println ("" );
70
- showConfiguration ();
67
+ new ShowConfigCommand (). run ();
71
68
72
- System .out .println ("\n To change default organization use : astra set- default-org <orgName>" );
69
+ System .out .println ("\n To change default organization: astra default-org <orgName>" );
73
70
}
74
71
}
75
72
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
- }
127
73
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
- }
154
74
155
75
}
0 commit comments