package io.quarkus.devservices.postgresql.deployment;

import java.net.URI;
import java.net.URISyntaxException;

import org.aesh.command.Command;
import org.aesh.command.CommandDefinition;
import org.aesh.command.CommandException;
import org.aesh.command.CommandResult;
import org.aesh.command.invocation.CommandInvocation;

import io.quarkus.deployment.builditem.DevServicesLauncherConfigResultBuildItem;

@CommandDefinition(name = "print-command", description = "Outputs the psql command to connect to the database")
public class PsqlCommand implements Command {

    private final DevServicesLauncherConfigResultBuildItem devServicesLauncherConfigResultBuildItem;

    public PsqlCommand(DevServicesLauncherConfigResultBuildItem devServicesLauncherConfigResultBuildItem) {
        this.devServicesLauncherConfigResultBuildItem = devServicesLauncherConfigResultBuildItem;
    }

    @Override
    public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
        //todo: should this work for non-dev services stuff as well? What about non-default datasources
        try {
            URI url = new URI(
                    devServicesLauncherConfigResultBuildItem.getConfig().get("quarkus.datasource.jdbc.url").substring(5));
            int port = url.getPort();
            String host = url.getHost();
            String pw = devServicesLauncherConfigResultBuildItem.getConfig().get("quarkus.datasource.password");
            commandInvocation.println("PGPASSWORD=" + pw + " psql --host=" + host + " --port=" + port + " --username="
                    + devServicesLauncherConfigResultBuildItem.getConfig().get("quarkus.datasource.username") + " "
                    + url.getPath().substring(1));
            return CommandResult.SUCCESS;
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -559,16 +559,204 @@
 mand %7B%0A%0A
+    private static final String JDBC_DATASOURCE__URL_KEY = %22quarkus.datasource.jdbc.url%22;%0A    private static final String REACTIVE_DATASOURCE__URL_KEY = %22quarkus.datasource.reactive.url%22;%0A
     priv
@@ -848,16 +848,16 @@
 ldItem;%0A
-
 %0A    pub
@@ -1330,17 +1330,148 @@
   URI ur
-l
+i;%0A            if (devServicesLauncherConfigResultBuildItem.getConfig().containsKey(JDBC_DATASOURCE__URL_KEY)) %7B%0A                uri
  = new U
@@ -1474,16 +1474,20 @@
 ew URI(%0A
+    
         
@@ -1559,53 +1559,550 @@
 get(
-%22quarkus.datasource.jdbc.url%22).substring(5));
+JDBC_DATASOURCE__URL_KEY)%0A                                .substring(%22jdbc:%22.length()));%0A            %7D else if (devServicesLauncherConfigResultBuildItem.getConfig().containsKey(REACTIVE_DATASOURCE__URL_KEY)) %7B%0A                uri = new URI(%0A                        devServicesLauncherConfigResultBuildItem.getConfig().get(REACTIVE_DATASOURCE__URL_KEY)%0A                                .substring(%22vertx-reactive:%22.length()));%0A            %7D else %7B%0A                throw new RuntimeException(%22Unable to determine datasource URL%22);%0A            %7D%0A
 %0A   
@@ -2119,25 +2119,25 @@
 nt port = ur
-l
+i
 .getPort();%0A
@@ -2164,17 +2164,17 @@
 ost = ur
-l
+i
 .getHost
@@ -2549,17 +2549,17 @@
     + ur
-l
+i
 .getPath

Make 'postgres print-command' quarkus command work with reactive datasources