28
28
import com .github .rvesse .airline .annotations .Command ;
29
29
import com .github .rvesse .airline .annotations .Option ;
30
30
31
+ import java .io .Console ;
32
+ import java .util .Arrays ;
31
33
import java .util .Scanner ;
32
34
33
35
import static com .dtsx .astra .cli .core .out .AstraAnsiColors .CYAN_400 ;
@@ -51,19 +53,29 @@ public class SetupCmd extends AbstractCmd {
51
53
public void execute () {
52
54
if (tokenParam == null || tokenParam .isBlank ()) {
53
55
verbose = true ;
54
- String token ;
56
+ String token = null ;
55
57
AstraCliConsole .banner ();
56
- try (Scanner scanner = new Scanner (System .in )) {
57
- boolean validToken = false ;
58
- while (!validToken ) {
59
- AstraCliConsole .println ("$ Enter an Astra token:" , CYAN_400 );
60
- token = removeQuotesIfAny (scanner .nextLine ());
61
- try {
62
- createDefaultSection (token );
63
- validToken = true ;
64
- } catch (InvalidTokenException ite ) {
65
- LoggerShell .error ("Your token in invalid please retry " + ite .getMessage ());
58
+ boolean validToken = false ;
59
+ Console cons ;
60
+ char [] tokenFromConsole ;
61
+ while (!validToken ) {
62
+ try {
63
+ if ((cons = System .console ()) != null &&
64
+ (tokenFromConsole = cons .readPassword ("[%s]" , "$ Enter an Astra token:" )) != null ) {
65
+ token = String .valueOf (tokenFromConsole );
66
+
67
+ // Clear the password from memory immediately when done
68
+ Arrays .fill (tokenFromConsole , ' ' );
69
+ } else {
70
+ try (Scanner scanner = new Scanner (System .in )) {
71
+ AstraCliConsole .println ("$ Enter an Astra token:" , CYAN_400 );
72
+ token = scanner .nextLine ();
73
+ }
66
74
}
75
+ createDefaultSection (removeQuotesIfAny (token ));
76
+ validToken = true ;
77
+ } catch (InvalidTokenException ite ) {
78
+ LoggerShell .error ("Your token in invalid please retry " + ite .getMessage ());
67
79
}
68
80
}
69
81
} else {
0 commit comments