2828import static com .dtsx .astra .cli .utils .CollectionUtils .sequencedMapOf ;
2929import static com .dtsx .astra .cli .utils .StringUtils .NL ;
3030import static com .dtsx .astra .cli .utils .StringUtils .trimIndent ;
31+ import static com .dtsx .astra .sdk .utils .AstraEnvironment .PROD ;
3132
3233@ Command (
3334 name = "create" ,
@@ -67,19 +68,25 @@ public class ConfigCreateCmd extends AbstractConfigCmd<ConfigCreateResult> {
6768 @ Option (
6869 names = { $Token .LONG , $Token .SHORT },
6970 description = "Astra token (@|code AstraCS:...|@) or @|code @<file>|@ to read from file" ,
70- paramLabel = $Token .LABEL ,
71- required = true
71+ paramLabel = $Token .LABEL
7272 )
73- public AstraToken $token ;
73+ public Optional < AstraToken > $token ;
7474
7575 @ Option (
7676 names = { $Env .LONG , $Env .SHORT },
77- description = "Astra environment the token belongs to: prod (default), dev, or test . Leave unset unless you were issued a non-prod token." ,
77+ description = "Astra environment the token belongs to: prod (default), dev, test, or local . Leave unset unless you were issued a non-prod token." ,
7878 completionCandidates = AstraEnvCompletion .class ,
7979 defaultValue = $Env .DEFAULT ,
8080 paramLabel = $Env .LABEL
8181 )
82- public AstraEnvironment $env ;
82+ public Optional <String > $env ;
83+
84+ @ Option (
85+ names = { "--local-endpoint" },
86+ description = "The endpoint URL for local Astra environments (required when --env local)" ,
87+ paramLabel = "URL"
88+ )
89+ public Optional <String > $localEndpoint ;
8390
8491 @ Option (
8592 names = { "-d" , "--default" },
@@ -94,6 +101,15 @@ public class ConfigCreateCmd extends AbstractConfigCmd<ConfigCreateResult> {
94101 )
95102 private Optional <Boolean > $overwrite ;
96103
104+ @ Option (
105+ names = { "--validate" },
106+ description = "Validate the token by making a request to the Astra" ,
107+ defaultValue = "true" ,
108+ fallbackValue = "true" ,
109+ negatable = true
110+ )
111+ public boolean $validate ;
112+
97113 @ Override
98114 public final OutputAll execute (Supplier <ConfigCreateResult > resultSupplier ) {
99115 val result = resultSupplier .get ();
@@ -103,6 +119,8 @@ public final OutputAll execute(Supplier<ConfigCreateResult> resultSupplier) {
103119 case ProfileIllegallyExists (var profileName ) -> throwProfileAlreadyExists (profileName );
104120 case ViolatedFailIfExists () -> throwAttemptedToSetDefault ();
105121 case InvalidToken (var hint ) -> throwInvalidToken (hint );
122+ case MissingToken _ -> throwMissingToken ();
123+ case NameRequiredIfNotValidated _ -> throwNameRequiredIfNotValidated ();
106124 };
107125 }
108126
@@ -168,10 +186,10 @@ private <T> T throwAttemptedToSetDefault() {
168186
169187 private <T > T throwInvalidToken (Optional <AstraEnvironment > hint ) {
170188 if (hint .isPresent ()) {
171- val currentEnvName = $env .name ().toLowerCase ();
189+ val currentEnvName = $env .orElse ( PROD . name () ).toLowerCase ();
172190 val validEnvName = hint .get ().name ().toLowerCase ();
173191
174- val fixAction = hint .get () == AstraEnvironment . PROD
192+ val fixAction = hint .get () == PROD
175193 ? "drop @'!--env!@ (prod is the default) or pass @'!--env prod!@"
176194 : "pass @'!--env " + validEnvName + "!@" ;
177195
@@ -191,14 +209,30 @@ private <T> T throwInvalidToken(Optional<AstraEnvironment> hint) {
191209 """ );
192210 }
193211
212+ private <T > T throwMissingToken () {
213+ throw new AstraCliException (VALIDATION_ISSUE , """
214+ @|bold,red An explicit astra token must be provided if '--env' is not 'local'|@
215+ """ );
216+ }
217+
218+ private <T > T throwNameRequiredIfNotValidated () {
219+ throw new AstraCliException (VALIDATION_ISSUE , """
220+ @|bold,red An explicit profile name must be provided if --validate=false.|@
221+ """ );
222+ }
223+
194224 @ Override
195225 public Operation <ConfigCreateResult > mkOperation () {
196- return new ConfigCreateOperation (ctx , config (true ), ctx .gateways ().mkOrgGateway ($token , $env ), ctx .gateways ().mkOrgGatewayStateless (), new CreateConfigRequest (
226+ val env = AstraEnvironment .resolve ($env , $localEndpoint );
227+
228+ return new ConfigCreateOperation (ctx , config (true ), t -> ctx .gateways ().mkOrgGateway (t , env ), ctx .gateways ().mkOrgGatewayStateless (), new CreateConfigRequest (
197229 $profileName ,
198230 $token ,
199- $ env ,
231+ env ,
200232 $overwrite ,
201233 $setDefault ,
234+ $validate ,
235+ $localEndpoint ,
202236 this ::assertCanOverwriteProfile
203237 ));
204238 }
0 commit comments