From f4e30eee155481c02a0842291c2d135b80f0ed14 Mon Sep 17 00:00:00 2001
From: Murtaza Sultani <sultani@data-quest.de>
Date: Fri, 28 Feb 2025 19:16:33 +0000
Subject: [PATCH] Add db:port command

---
 lib/Commands/Compile.php |  4 +++-
 lib/Commands/DB/Port.php | 40 ++++++++++++++++++++++++++++++++++++++++
 studip-docker            |  2 ++
 3 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 lib/Commands/DB/Port.php

diff --git a/lib/Commands/Compile.php b/lib/Commands/Compile.php
index a8676c3..770adaf 100644
--- a/lib/Commands/Compile.php
+++ b/lib/Commands/Compile.php
@@ -164,10 +164,12 @@ final class Compile extends Command
         ]);
 
         // DB
+        $dbExposingPort = (int) (Config::getInstance()->get('db')['port'] ?? 3306);
+
         $creator->addService('db', 'mariadb', [
             'container_name' => 'db',
             'command'        => '--sql_mode="NO_ENGINE_SUBSTITUTION"',
-            'ports'          => ['3306:3306'],
+            'ports'          => [$dbExposingPort . ':3306'],
             'restart'        => 'unless-stopped',
             'volumes'        => ['db-data:/var/lib/mysql'],
             'environment'    => [
diff --git a/lib/Commands/DB/Port.php b/lib/Commands/DB/Port.php
new file mode 100644
index 0000000..e523a79
--- /dev/null
+++ b/lib/Commands/DB/Port.php
@@ -0,0 +1,40 @@
+<?php
+namespace Studip\Dockerized\Commands\DB;
+
+use Studip\Dockerized\Config;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+
+final class Port extends Command
+{
+    protected function configure()
+    {
+        $this->setName('db:port');
+        $this->setDescription('Change exposing port for Database (needs recompile)');
+        $this->addArgument(
+            'port',
+            InputArgument::REQUIRED,
+            'Port to use'
+        );
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $port = (int) $input->getArgument('port');
+
+        $config = Config::getInstance();
+        $io = new SymfonyStyle($input, $output);
+
+        $dbPort = $config->get('db');
+        $dbPort['port'] = $port;
+        $config->set('db', $dbPort);
+        $config->store();
+
+        $io->success("Exposing ports of DB were changed to {$port}");
+
+        return Command::SUCCESS;
+    }
+}
diff --git a/studip-docker b/studip-docker
index 7f8162f..4ad569e 100755
--- a/studip-docker
+++ b/studip-docker
@@ -30,6 +30,8 @@ if (file_exists(CONFIG_FILE)) {
     $application->add(new Commands\Docker\Stop());
 
     $application->add(new Commands\Keys\Create());
+
+    $application->add(new Commands\DB\Port());
 }
 
 $application->run();
-- 
GitLab