From df3e1211aff40d06e24ba2cf0c43ff16a1f8cba0 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sat, 2 Mar 2024 17:10:30 +0100 Subject: [PATCH] Misc fixes, templates --- registry/mailhog.json | 1 + registry/mariadb.json | 11 ++++++----- registry/mongo.json | 3 ++- registry/mysql.json | 2 +- src/Commands/ExecCommand.php | 8 ++++++-- src/Commands/FindCommand.php | 7 +++++++ src/Container/ContainerManager.php | 4 ++++ 7 files changed, 27 insertions(+), 9 deletions(-) diff --git a/registry/mailhog.json b/registry/mailhog.json index b36abee..54fea54 100644 --- a/registry/mailhog.json +++ b/registry/mailhog.json @@ -2,6 +2,7 @@ "$type": "service", "name": "mailhog", "description": "SMTP server that holds e-mail for debugging", + "info": "Mailhog accepts e-mails from any configured app over SMTP for use during development. Received messages can be displayed in the web interface.", "tags": [ "app", "mail", "smtp" ], "author": null, "image": "mailhog/mailhog", diff --git a/registry/mariadb.json b/registry/mariadb.json index 15c04e8..1922331 100644 --- a/registry/mariadb.json +++ b/registry/mariadb.json @@ -2,6 +2,7 @@ "$type": "service", "name": "mariadb", "description": "MariaDB RDBMS (MySQL fork)", + "info": "MariaDB is a mostly MySQL compatible relational database server.", "help": "Default credentials are root/toor", "tags": [ "rdbms", "mysql" ], "author": null, @@ -16,17 +17,17 @@ { "path": "/data", "hint": "data" } ], "scripts": { - "mongosh": { - "info": "Open the mongo shell", - "execute": "mongosh" + "shell": { + "info": "Open the MariaDB shell", + "execute": [ "mysql", "-uroot", "-p${MARIADB_ROOT_PASSWORD}" ] }, "export": { "info": "Export the database", - "execute": "mysqldump " + "execute": "mysqldump -uroot -p${MARIADB_ROOT_PASSWORD} --all-databases" }, "import": { "info": "Import the database", - "execute": "mongoimport" + "execute": "mysql -uroot -p${MARIADB_ROOT_PASSWORD}" } } } diff --git a/registry/mongo.json b/registry/mongo.json index 3dd9c08..150e82a 100644 --- a/registry/mongo.json +++ b/registry/mongo.json @@ -18,7 +18,8 @@ "scripts": { "shell": { "info": "Open the mongo shell", - "execute": "mongosh" + "execute": "mongosh", + "arguments": [ "-u", "root", "-p", "toor" ] }, "export": { "info": "Export the database", diff --git a/registry/mysql.json b/registry/mysql.json index 2847805..a260308 100644 --- a/registry/mysql.json +++ b/registry/mysql.json @@ -17,7 +17,7 @@ ], "scripts": { "shell": { - "info": "Open the mongo shell", + "info": "Open the MySQL shell", "execute": "mysql -uroot -p%{MARIADB_ROOT_PASSWORD}" }, "export": { diff --git a/src/Commands/ExecCommand.php b/src/Commands/ExecCommand.php index c1caa00..4c75057 100644 --- a/src/Commands/ExecCommand.php +++ b/src/Commands/ExecCommand.php @@ -41,12 +41,16 @@ class ExecCommand extends Command return self::SUCCESS; } elseif ($input->getOption("script")) { $script = $serviceInfo['scripts'][$command]['execute']??null; + $scriptArgs = $serviceInfo['scripts'][$command]['arguments']??null; if (!$script) { $output->writeln("No such script"); return self::FAILURE; } - $cmdl = [ is_array($script)?join("; ", $script):$script ]; + if ($scriptArgs) + $cmdl = [ $script, ...$scriptArgs ]; // is_array($script)?join("; ", $script):$script ]; + else + $cmdl = $script; $containerManager->execute($serviceInfo, $instanceName, $cmdl); } else { @@ -58,4 +62,4 @@ class ExecCommand extends Command return self::SUCCESS; } -} \ No newline at end of file +} diff --git a/src/Commands/FindCommand.php b/src/Commands/FindCommand.php index 385d9be..be621a8 100644 --- a/src/Commands/FindCommand.php +++ b/src/Commands/FindCommand.php @@ -17,6 +17,7 @@ class FindCommand extends Command $this->addArgument("service", InputArgument::OPTIONAL, "Search query"); $this->addOption("environment", "E", InputOption::VALUE_NONE, "Show the environment for the services"); $this->addOption("ports", "P", InputOption::VALUE_NONE, "Show the ports for the services"); + $this->addOption("info", "I", InputOption::VALUE_NONE, "Show service information text if available"); } protected function execute(InputInterface $input, OutputInterface $output) @@ -25,10 +26,12 @@ class FindCommand extends Command $printEnvs = $input->getOption("environment"); $printPorts = $input->getOption("ports"); + $showInfo = $input->getOption("info"); $services = $serviceRegistry->findAllServices(); $query = $input->getArgument("service"); + $output->writeln($query?"Services matching {$query}:":"Available services:"); foreach ($services as $service) { $tags = $service['tags']??[]; @@ -42,6 +45,10 @@ class FindCommand extends Command $tags = ""; } $output->writeln(sprintf(" %s: %s %s", $service['name'], $service['description']??"?", $tags)); + if ($showInfo && array_key_exists('info', $service)) { + $info = " ".strtr(wordwrap($service['info']), [ "\n" => "\n " ]); + $output->writeln("".$info.""); + } if ($printEnvs) { foreach ($service['environment']??[] as $env=>$value) { $output->writeln(sprintf(" %s=%s", $env, $value)); diff --git a/src/Container/ContainerManager.php b/src/Container/ContainerManager.php index 341c639..2018b8b 100644 --- a/src/Container/ContainerManager.php +++ b/src/Container/ContainerManager.php @@ -251,6 +251,10 @@ class ContainerManager $cmdl = 'docker '.join(' ',array_map('escapeshellarg', $args)); + $cmdl = preg_replace_callback('<\$\{(.+?)\}>', function ($v) use ($service) { + return $service['environment'][$v[1]]??null; + }, $cmdl); + echo "$ {$cmdl}\n"; passthru($cmdl);