Skip to content

Commit

Permalink
allow streamlined setup without prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
braceyourself committed Feb 2, 2025
1 parent 05cb6b6 commit 452d363
Showing 1 changed file with 115 additions and 56 deletions.
171 changes: 115 additions & 56 deletions src/Commands/ComposePublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ComposePublishCommand extends Command
';

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

public function info($string, $verbosity = null)
{
Expand All @@ -52,69 +53,30 @@ public function handle()
? $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 (config('compose.setup_complete') == false) {

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->setup_type = select('Would you like to use the compose defaults or choose your own?', [
'default' => 'Use the default compose setup',
'custom' => 'Choose my own setup'
], default: 'default');

$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_NETWORK', 'traefik_default');
$this->setEnv('COMPOSE_PHP_IMAGE', "ethanabrace/php:{$this->getPhpVersion()}", 'services.php.image');

$connection_name = $this->setEnv('DB_CONNECTION', $this->getDatabaseConnectionName(), 'database.default');

if ($connection_name !== 'sqlite') {
$this->setEnv('DB_HOST', $this->getDbHost($connection_name), "database.connections.{$connection_name}.host");
$this->setEnv('DB_PORT', $this->getDbPort($connection_name), "database.connections.{$connection_name}.port");
$this->setEnv('DB_DATABASE', $this->getDbDatabase($connection_name), "database.connections.{$connection_name}.database");
$this->setEnv('DB_USERNAME', $this->getDbUsername($connection_name), "database.connections.{$connection_name}.username");
$this->setEnv('DB_PASSWORD', $this->getDbPassword($connection_name), "database.connections.{$connection_name}.password");
}

$this->setEnv('COMPOSE_SETUP_COMPLETE', true);
}
Expand Down Expand Up @@ -152,4 +114,101 @@ private function askForFileInput()
);
}

private function promptForPhpImageName(): string
{
$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()}"
};
}

private function getDatabaseConnectionName()
{
if ($this->setup_type == 'default') {
return 'mysql';
}

return 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;

};

}

private function getDbHost($connection_name)
{
if ($this->setup_type == 'default') {
return 'database';
}

return function () use ($connection_name) {
return text("Enter the host address for your {$connection_name} database", default: config("database.connections.{$connection_name}.host", $connection_name));
};
}

private function getDbPort(mixed $connection_name)
{
if ($this->setup_type == 'default') {
return '3306';
}

return function () use ($connection_name) {
return text("Enter the database port", default: config("database.connections.{$connection_name}.port"));
};
}

private function getDbDatabase(mixed $connection_name)
{
if ($this->setup_type == 'default') {
return str(config('compose.name'))->snake()->value();
}

return function () use ($connection_name) {
return text("Enter the database name", default: config("database.connections.{$connection_name}.database"));
};
}

private function getDbUsername(mixed $connection_name)
{
if ($this->setup_type == 'default') {
return 'admin';
}

return function () use ($connection_name) {
return text("Enter the database username", default: config("database.connections.{$connection_name}.username"));
};
}

private function getDbPassword(mixed $connection_name)
{
if ($this->setup_type == 'default') {
return 'password';
}

return function () use ($connection_name) {
return text("Enter the database password", default: config("database.connections.{$connection_name}.password"));
};
}

}

0 comments on commit 452d363

Please sign in to comment.