Skip to content

Commit

Permalink
streamline publish command
Browse files Browse the repository at this point in the history
  • Loading branch information
braceyourself committed Feb 2, 2025
1 parent 3365d93 commit 62513f0
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 77 deletions.
17 changes: 13 additions & 4 deletions bin/compose
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ display_help(){

#publish the compose file using ./artisan compose:publish
publishFiles(){

echo "Publishing files"
# set -e
set -e

docker run --rm -it \
-u "$(id -u):$(id -g)" \
Expand All @@ -86,7 +84,16 @@ publishFiles(){
-w /var/www/html \
--env-file .env \
-e APP_ENV=${APP_ENV} \
ethanabrace/php:8.3 /var/www/html/artisan compose:publish --files docker-compose.yml --files build ${APP_ENV}
ethanabrace/php:8.3 /var/www/html/artisan compose:publish \
--files docker-compose.yml \
--files build \
--env ${APP_ENV} \
--user_id "$(id -u)" \
--group_id "$(id -g)" \
"$(pwd)"

# sleep for a second
sleep 2
}

dockerCompose(){
Expand Down Expand Up @@ -119,6 +126,8 @@ dockerCompose(){
}

setupTraefik(){
source .env

# if $COMPOSE_NETWORK is not set throw error
if [ -z "$COMPOSE_NETWORK" ]; then
echo "COMPOSE_NETWORK is not set. Please add it to your .env file."
Expand Down
2 changes: 2 additions & 0 deletions config/compose.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

return [
'name' => env('COMPOSE_NAME'),
'domain' => env('COMPOSE_DOMAIN'),
'user_id' => env('COMPOSE_USER_ID', 1000),
'group_id' => env('COMPOSE_GROUP_ID', 1000),
'profiles' => env('COMPOSE_PROFILES', 'local'),
'setup_complete' => env('COMPOSE_SETUP_COMPLETE', false),

/***
* wait for the database to be ready before starting the application
Expand Down
135 changes: 90 additions & 45 deletions src/Commands/ComposePublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Braceyourself\Compose\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Artisan;
use Braceyourself\Compose\Facades\Compose;
Expand All @@ -25,19 +26,105 @@ class ComposePublishCommand extends Command
protected $signature = 'compose:publish
{--publish-path=}
{--files=* : Files to publish}
{env? : The environment to publish to}';
{--user_id=}
{--group_id=}
{path}
';

protected $description = 'Publish the docker compose files.';

public function info($string, $verbosity = null)
{
if ($this->option('verbose')) {
return parent::info($string, $verbosity);
}
}

public function handle()
{
$env = $this->argument('env') ?? 'local';
$user_id = $this->option('user_id');
$group_id = $this->option('group_id');
$env = $this->option('env') ?? 'local';
$dir_name = str($this->argument('path'))->basename()->slug()->value();
$publish_path = $this->option('publish-path') ?: '.';
$compose_build_dir = __DIR__ . '/../../build';
$files = $this->hasOption('files')
? $this->option('files')
: $this->askForFileInput();

if(config('compose.setup_complete') == false){
$connection_name = $this->setEnv('DB_CONNECTION', function () {
$name = select(
'Select the database connection',
collect(config('database.connections'))->keys()->push('custom')->toArray(),
config('database.default')
);

if ($name == 'custom') {
$name = text('Enter the custom connection name');
}

return $name;

}, 'database.default');


if($connection_name !== 'sqlite'){
$this->setEnv('DB_HOST', function () use ($connection_name) {
return text("Enter the host address for your {$connection_name} database", default: config("database.connections.{$connection_name}.host", $connection_name));
}, "database.connections.{$connection_name}.host");

$this->setEnv('DB_PORT', function () use ($connection_name) {
return text("Enter the database port", default: config("database.connections.{$connection_name}.port"));
}, "database.connections.{$connection_name}.port");

$this->setEnv('DB_DATABASE', function () use ($connection_name) {
return text("Enter the database name", default: config("database.connections.{$connection_name}.database"));
}, "database.connections.{$connection_name}.database");

$this->setEnv('DB_USERNAME', function () use ($connection_name) {
return text("Enter the database username", default: config("database.connections.{$connection_name}.username"));
}, "database.connections.{$connection_name}.username");

$this->setEnv('DB_PASSWORD', function () use ($connection_name) {
return text("Enter the database password", default: config("database.connections.{$connection_name}.password"));
}, "database.connections.{$connection_name}.password");

}

$this->setEnv('USER_ID', $user_id);
$this->setEnv('GROUP_ID', $group_id);
$this->setEnv('COMPOSE_NAME', $dir_name);
$this->setEnv('COMPOSE_ROUTER', $dir_name);
$this->setEnv('COMPOSE_PROFILES', 'local');
$this->setEnv('COMPOSE_NETWORK', fn() => text('Enter the compose network', default: 'traefik_default'));
$this->setEnv('COMPOSE_PHP_IMAGE', function () {
$choice = select("Enter your PHP image", [
'match' => "Use the image matching your php version ({$this->getPhpVersion()})",
'text' => 'Enter Manually',
'choose' => 'Choose from list',
//'search' => "Search for an image",
], default: 'match');

return match($choice){
'search' => search("Search docker hub for an image", function ($value) {

}),
'text' => text("Enter the image name to user for the php service:", default: "php:{$this->getPhpVersion()}", hint: "This can be whatever you like."),
'choose' => select("Choose from the list of images", collect($this->getPhpVersions())->map(fn($version) => "php:$version")),
'match' => "php:{$this->getPhpVersion()}"
};
}, 'services.php.image');

$this->setEnv('COMPOSE_SETUP_COMPLETE', true);
}


if (in_array('docker-compose.yml', $files)) {
file_put_contents("{$publish_path}/docker-compose.yml", $this->getComposeYaml($env));
$this->info("docker-compose.yml published.");
}

if (in_array('build', $files)) {
$this->createDockerfile();

Expand All @@ -51,41 +138,9 @@ public function handle()
}
copy("{$compose_build_dir}/{$file}", "{$publish_path}/build/" . basename($file));
}
}

if (in_array('docker-compose.yml', $files)) {
file_put_contents("{$publish_path}/docker-compose.yml", $this->getComposeYaml($env));
$this->info("Build files published.");
}

$this->setEnvIfMissing('COMPOSE_PROFILES', 'local');
$this->setEnvIfMissing('COMPOSE_NETWORK', fn() => text('Enter the compose network', default: 'traefik_default'));
$this->setEnvIfMissing('USER_ID', fn() => text('What is your system user_id?', default: '1000'));
$this->setEnvIfMissing('GROUP_ID', fn() => text('What is your system group_id?', default: '1000'));


// choose from list of images and allow for searching
//default: 'php:8.0-fpm'


$this->setEnvIfMissing('COMPOSE_PHP_IMAGE', function () {
$choice = select("Enter your PHP image", [
'match' => "Use the image matching your php version ({$this->getPhpVersion()})",
'text' => 'Enter Manually',
'choose' => 'Choose from list',
//'search' => "Search for an image",
], default: 'match');

return match($choice){
'search' => search("Search docker hub for an image", function ($value) {

}),
'text' => text("Enter the image name to user for the php service:", default: "php:{$this->getPhpVersion()}", hint: "This can be whatever you like."),
'choose' => select("Choose from the list of images", collect($this->getPhpVersions())->map(fn($version) => "php:$version")),
'match' => "php:{$this->getPhpVersion()}"
};
});

$this->info("Compose files published.");
}

private function askForFileInput()
Expand All @@ -97,14 +152,4 @@ private function askForFileInput()
);
}

private function setEnvIfMissing($key, $value)
{
if (!$this->localEnv($key)) {

$value = is_callable($value) ? $value() : $value;

$this->setEnv($key, $value);

}
}
}
2 changes: 1 addition & 1 deletion src/Concerns/HasDatabaseServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private function databaseServiceDefinition($config = []): array
return collect([
'image' => 'mysql',
'restart' => 'always',
'container_name' => str(config('app.name'))->slug() . '-mysql',
'container_name' => str(config('compose.name'))->slug() . '-mysql',
'healthcheck' => [
'test' => ['CMD', 'mysqladmin', 'ping', '-h', 'localhost'],
'interval' => '15s',
Expand Down
Loading

0 comments on commit 62513f0

Please sign in to comment.